59 lines
1.2 KiB
Python
59 lines
1.2 KiB
Python
from gradio_client import Client
|
|
|
|
from deep_translator import GoogleTranslator
|
|
|
|
def predict(prompt, client, model = "0.1"):
|
|
global iddb
|
|
if model == "0.1":
|
|
result = client.predict(
|
|
prompt,
|
|
"Default",
|
|
api_name="/chat"
|
|
)
|
|
elif model == "0.2":
|
|
result = client.predict(
|
|
prompt,
|
|
0.05, # 'Temperature'
|
|
128, # 'Max new tokens'
|
|
0.8, # 'Top-p (nucleus sampling)'
|
|
1.8, # 'Repetition penalty'
|
|
api_name="/chat"
|
|
)
|
|
|
|
return result
|
|
|
|
# text IN language IN
|
|
def translate(text, source):
|
|
if source == "ru":
|
|
target = "en"
|
|
elif source == "en":
|
|
target = "ru"
|
|
out = GoogleTranslator(source = source, target = target).translate(text)
|
|
return out
|
|
|
|
iddb = {}
|
|
|
|
def gen(text, id, model):
|
|
global iddb
|
|
|
|
if str(id) not in iddb:
|
|
if model == "0.1":
|
|
client = Client("https://afischer1985-ai-interface.hf.space/")
|
|
elif model == "0.2":
|
|
client = Client("https://skier8402-mistral-super-fast.hf.space/")
|
|
iddb[str(id)] = client
|
|
else:
|
|
client = iddb[str(id)]
|
|
|
|
prompt = translate(text, "ru")
|
|
|
|
success = False
|
|
while not success:
|
|
try:
|
|
predicted = predict(prompt, client, model).replace("</s>", "")
|
|
success = True
|
|
except:
|
|
pass
|
|
|
|
return translate(predicted, "en")
|