diff --git a/api.py b/api.py index 201b724..f9fbc41 100644 --- a/api.py +++ b/api.py @@ -58,6 +58,7 @@ def translate(text, source): out = GoogleTranslator(source = source, target = target).translate(text) return out + iddb = {} def gen(text, id, model): @@ -67,20 +68,21 @@ def gen(text, id, model): 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/") +# client = Client("https://skier8402-mistral-super-fast.hf.space/") + client = Client("https://nonamed33-minigpt-api.hf.space/") + # Changed to private space if not model == "3.5": iddb[str(id)] = client else: if not model == "3.5": client = iddb[str(id)] - success = False try: +# if True: if model == "0.1" or model == "0.2": prompt = translate(text, "ru") predicted = predict(prompt, client, model).replace("", "") predicted = translate(predicted, "en") - success = True elif model == "3.5": # ChatGPT global setted_models @@ -90,7 +92,6 @@ def gen(text, id, model): except: inst = None predicted = chat_predict(prompt, id, inst) - success = True except: pass diff --git a/minigpt.py b/minigpt.py index 5ea6886..ebcb02f 100755 --- a/minigpt.py +++ b/minigpt.py @@ -1,4 +1,5 @@ import telebot +from telebot.types import InlineKeyboardMarkup, InlineKeyboardButton ### LOAD TOKEN ### import json, os @@ -24,17 +25,18 @@ def send_welcome(message): bot.reply_to(message, f"""Это подобие ChatGPT на минималках. __ Есть 3 версии: -{telebot.formatting.hcode("/m 0.1")} - Простейшая, быстрейшая, краткая, не помнит что вы говорили. -{telebot.formatting.hcode("/m 0.2")} - Умнее, относительно быстрая, помнит что вы говорили. -{telebot.formatting.hcode("/m 3.5")} - Самая умная, есть цензура, помнит что вы говорили. -(1,2 - Mistral, 3 - ChatGPT) - -__ Список кратких команд: -/info - /i -/model - /m -/ccontext - /cc -/prompt - /p -/cprompt - /cp +0.1 - Простейшая, быстрейшая, краткая, не помнит что вы говорили. +0.2 - Умнее, относительно быстрая, помнит что вы говорили. +3.5 - Самая умная, есть цензура, помнит что вы говорили. +/m - Выбор модели + +__ Список команд: +help - Справка +i - Информация о конфигурации +m - Сменить модель +cc - Очистить контекст +p - Задать инструкцию (системную) +cp - Сбросить инструкцию Ещё проекты: @just_openbots (тут много интересного) {telebot.formatting.hlink("Исходный код","https://gitea.gulyaipole.fun/justuser/just_minigpt")} @@ -70,26 +72,45 @@ def info(message): System-prompt: {telebot.formatting.hcode(prompt)} """, parse_mode="HTML") -@bot.message_handler(commands=['model','m']) + +############ MODEL SELECTION ############ + +@bot.message_handler(commands=['m']) def set_model(message): + bot.send_message(message.chat.id, "Выберите новую модель:", reply_markup=gen_markup(str(message.chat.id))) + +def gen_markup(id): + markup = InlineKeyboardMarkup() + markup.row_width = 3 + markup.add(InlineKeyboardButton("0.1", callback_data=id+"_0.1"), + InlineKeyboardButton("0.2", callback_data=id+"_0.2"), + InlineKeyboardButton("3.5", callback_data=id+"_3.5") ) + return markup + +@bot.callback_query_handler(func=lambda call: True) +def callback_query(call): global setted_models, iddb + id = call.data.split("_")[0] ; m = call.data.split("_")[1] + try: - iddb.pop(str(message.chat.id)) + iddb.pop(id) except: pass - model = message.text.split()[1] - if model == "0.1" or model == "0.2" or model == "3.5": - setted_models[str(message.chat.id)] = model - bot.reply_to(message, "Установлена новая модель 🤖") - else: - bot.reply_to(message, "Неизвестная модель") + if m == "0.1": + setted_models[id] = "0.1" + elif m == "0.2": + setted_models[id] = "0.2" + elif m == "3.5": + setted_models[id] = "3.5" + bot.send_message(int(id), "Успешно установлена новая модель 🤖") +########################################## -@bot.message_handler(commands=['prompt','p']) +@bot.message_handler(commands=['p']) def set_prompt(message): global system_prompts - system_prompts[str(message.chat.id)] = message.text[8:] + system_prompts[str(message.chat.id)] = message.text[3:] bot.reply_to(message, "Установлен новый system-prompt 🤖") @bot.message_handler(commands=['cprompt','cp']) def clear_prompt(message): @@ -101,7 +122,7 @@ def clear_prompt(message): bot.reply_to(message, "System-prompt очищен 🤖") -@bot.message_handler(commands=['ccontext','cc']) +@bot.message_handler(commands=['cc']) def clear_context(message): global iddb, history try: @@ -117,31 +138,38 @@ def clear_context(message): @bot.message_handler(func=lambda message: True) def echo_message(message): - global setted_models, system_prompts - - id = str(message.chat.id) - if id not in setted_models: - setted_models[id] = "0.1" - - if id in system_prompts: - if setted_models[id] != 3.5: - prompt = '[INST]' + system_prompts[id] + '[/INST]\n\n' + message.text - else: - prompt = message.text - - st = bot.send_message(message.chat.id, "Печатает...") - try: - predicted = gen(prompt, message.chat.id, setted_models[id]) - except: - bot.send_message(message.chat.id, "Извините, возникла непредвиденная ошибка") - try: - bot.reply_to(message, predicted, parse_mode="Markdown") - except Exception as e: - bot.reply_to(message, predicted, parse_mode="HTML") - logging.error(traceback.format_exc()) - print(predicted) - - bot.delete_message(message.chat.id, st.id) + if bot.get_chat(message.chat.id).type == "private" or message.text[:2] == "/a": + global setted_models, system_prompts + + if message.text[:2] == "/a": + text = message.text[3:] + else: + text = message.text + + id = str(message.chat.id) + if id not in setted_models: + setted_models[id] = "0.1" + + if id in system_prompts: + if setted_models[id] != 3.5: + prompt = '[INST]' + system_prompts[id] + '[/INST]\n\n' + text + else: + prompt = text + + st = bot.send_message(message.chat.id, "Печатает...") + try: + predicted = gen(prompt, message.chat.id, setted_models[id]) + except: + bot.send_message(message.chat.id, "Извините, возникла непредвиденная ошибка") + + try: + bot.reply_to(message, predicted, parse_mode="Markdown") + except Exception as e: + bot.reply_to(message, predicted, parse_mode="HTML") + logging.error(traceback.format_exc()) + print(predicted) + + bot.delete_message(message.chat.id, st.id) ############