You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.1 KiB

10 months ago
from gradio_client import Client
from chat_api import *
10 months ago
from deep_translator import GoogleTranslator
def predict(prompt, client, model = "0.1"):
10 months ago
global iddb
if model == "0.1":
result = client.predict(
prompt,
"Default",
api_name="/chat"
)
elif model == "0.2":
result = client.predict(
prompt,
0.5, # 'Temperature'
128, # 'Max new tokens'
0.8, # 'Top-p (nucleus sampling)'
1.8, # 'Repetition penalty'
api_name="/chat"
)
10 months ago
return result
# Detect code
def iscode(text):
langs = ['sql','php','js','java','c','cpp','python','go']
is_code = False
for i in langs:
if i + r'\n' in text:
is_code = True
break
spec = ['section .']
if not is_code:
for i in spec:
if i in text:
is_code = True
break
return is_code
10 months ago
# text IN language IN
def translate(text, source):
if source == "ru":
target = "en"
elif source == "en":
target = "ru"
# Fix code translate
if '```' in text:
out = ''
for i in text.split('```'):
if iscode(i):
out += '```' + i + '```'
else:
out += GoogleTranslator(source = source, target = target).translate(i)
else:
out = GoogleTranslator(source = source, target = target).translate(text)
10 months ago
return out
iddb = {}
def gen(text, id, model):
10 months ago
global iddb
10 months ago
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/")
if not model == "3.5":
iddb[str(id)] = client
10 months ago
else:
if not model == "3.5":
client = iddb[str(id)]
success = False
try:
if model == "0.1" or model == "0.2":
prompt = translate(text, "ru")
predicted = predict(prompt, client, model).replace("</s>", "")
predicted = translate(predicted, "en")
success = True
elif model == "3.5":
# ChatGPT
global setted_models
prompt = text
try:
inst = setted_models[id]
except:
inst = None
predicted = chat_predict(prompt, id, inst)
success = True
except:
pass
fixed = predicted.replace(r'\n', '\n').replace('\\ n', '\n')
return fixed