|
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) |
|
|
|
|
|
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) |
|
|
|
|
|
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) |
|
|