File size: 1,649 Bytes
9bc5eb3 4c336c5 9bc5eb3 100989a 48beffb 4c336c5 9bc5eb3 deb925a 4c336c5 deb925a 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 |
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):
random_phrase = random.choice(random_phrases)
user_input = f"[USER]: {prompt}\n[BOT]: {random_phrase},"
inputs = tokenizer.encode(user_input, return_tensors="pt")
outputs = model.generate(inputs, max_length=25, num_return_sequences=1, pad_token_id=tokenizer.eos_token_id)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
edit_response = response.split(f"[BOT]: ")[-1].strip()
if "[" in edit_response:
edit_response = edit_response.split("[")[0]
bot_message = f"[BOT!]: {edit_response}"
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)
|