GPT2 / app.py
uhygfd's picture
Update app.py
08240ec verified
raw
history blame
1.63 kB
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]: "
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)