From bfb92fe33763770f8601e2de13bd57c4721fc934 Mon Sep 17 00:00:00 2001 From: t Date: Tue, 2 Jan 2024 00:06:13 +0300 Subject: [PATCH] Main init --- api.py | 41 +++++++++++++++++++++++++++++++++++++++++ minigpt.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 api.py create mode 100644 minigpt.py diff --git a/api.py b/api.py new file mode 100644 index 0000000..50c2106 --- /dev/null +++ b/api.py @@ -0,0 +1,41 @@ +from gradio_client import Client +#client = Client("https://skier8402-mistral-super-fast.hf.space/") + +from deep_translator import GoogleTranslator + +def predict(prompt, client): + global iddb + result = client.predict( + prompt, + 0.5, # 'Temperature' + 256, # 'Max new tokens' + 0.9, # 'Top-p (nucleus sampling)' + 1.2, # '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): + global iddb + if str(id) not in iddb: + client = Client("https://skier8402-mistral-super-fast.hf.space/") + iddb[str(id)] = client + else: + client = iddb[str(id)] + + prompt = translate(text, "ru") + predicted = translate( predict(prompt, client), "en" ).replace("", "") + return predicted diff --git a/minigpt.py b/minigpt.py new file mode 100644 index 0000000..d6eb89b --- /dev/null +++ b/minigpt.py @@ -0,0 +1,38 @@ +import telebot + +### LOAD TOKEN ### +import json, os + +if not os.path.exists('db.json'): + db = {"token": "None"} + js = json.dumps(db, indent=2) + with open("db.json", "w") as outfile: + outfile.write(js) + + print('Input token in "None" (db.json)') + exit() +with open('db.json', 'r') as openfile: + db = json.load(openfile) + +API_TOKEN = db["token"] +bot = telebot.TeleBot(API_TOKEN) +################## + +@bot.message_handler(commands=['help', 'start']) +def send_welcome(message): + bot.reply_to(message, "None") + + +### MAIN ### +from api import * + + +@bot.message_handler(func=lambda message: True) +def echo_message(message): + bot.send_chat_action(message.chat.id, "typing", 30) + bot.reply_to(message, gen(message.text, message.chat.id)) + + +############ + +bot.infinity_polling()