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