File size: 1,655 Bytes
9bc5eb3 9ceca41 9bc5eb3 9ceca41 9bc5eb3 9ceca41 9bc5eb3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import gradio as gr
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import random
# Загрузка модели и токенизатора
model_name = "ai-forever/rugpt3large_based_on_gpt2"
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)
# Загрузка случайных фраз из файла dialogues.txt
with open("dialogues.txt", "r", encoding="utf-8") as file:
random_phrases = [line.strip() for line in file.readlines() if line.strip()]
# Функция генерации ответа
def generate(prompt, _=None):
user_input = f"[USER]: {prompt}\n[BOT]: {random.choice(random_phrases)},"
inputs = tokenizer.encode(prompt, return_tensors="pt")
outputs = model.generate(inputs, max_length=50, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Прекращаем генерацию на строке [USER]
if "[" in response:
response = response.split("[")[0]
bot_message = f"[BOT]: {random.choice(random_phrases)}, {response.strip()}"
return bot_message
# Настройка интерфейса чат-бота
mychatbot = gr.Chatbot(
avatar_images=["./user.png", "./botm.png"], bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True
)
# Создание интерфейса для чат-бота
demo = gr.ChatInterface(
fn=generate,
chatbot=mychatbot,
title="🤬НЕАДЕКВАТ🤬",
retry_btn=None,
undo_btn=None
)
if __name__ == "__main__":
demo.launch(share=True)
|