|
|
|
|
|
import torch |
|
|
import transformers |
|
|
import gradio as gr |
|
|
|
|
|
|
|
|
use_cuda = torch.cuda.is_available() |
|
|
device = torch.device("cuda" if use_cuda else "cpu") |
|
|
|
|
|
|
|
|
t5_tokenizer = transformers.GPT2Tokenizer.from_pretrained("SiberiaSoft/SiberianPersonaFred-2") |
|
|
t5_model = transformers.T5ForConditionalGeneration.from_pretrained("SiberiaSoft/SiberianPersonaFred-2").to(device) |
|
|
|
|
|
|
|
|
def generate_response(dialog, prompt_choice): |
|
|
dialog = dialog.strip() |
|
|
if len(dialog) == 0: |
|
|
return "Please enter a message." |
|
|
|
|
|
dialog = dialog[0].upper() + dialog[1:] |
|
|
|
|
|
|
|
|
if prompt_choice == "Siberian Persona": |
|
|
prompt = '<SC6>Я культурный гопник, ем семечки, люблю играть в доту 2 на любимом герое инвокере, говорю без матов и маты не люблю, недавно у меня был такой диалог:' + '\nТы: ' + dialog + '\nЯ: <extra_id_0>' |
|
|
else: |
|
|
prompt = '<SC6>я поэт который пишет свои сообщения стихами с использованием мата и разным смыслом в зависимости от сообщений, недавно у меня был такой диалог:' + '\nТы: ' + dialog + '\nЯ: <extra_id_0>' |
|
|
|
|
|
input_ids = t5_tokenizer(prompt, return_tensors='pt').input_ids.to(device) |
|
|
out_ids = t5_model.generate(input_ids=input_ids, do_sample=True, temperature=0.9, max_new_tokens=512, top_p=0.85, top_k=2, repetition_penalty=1.2) |
|
|
|
|
|
t5_output = t5_tokenizer.decode(out_ids[0][1:]) |
|
|
if '</s>' in t5_output: |
|
|
t5_output = t5_output[:t5_output.find('</s>')].strip() |
|
|
t5_output = t5_output.replace('<extra_id_0>', '').strip() |
|
|
t5_output = t5_output.split('Собеседник')[0].strip() |
|
|
|
|
|
return t5_output |
|
|
|
|
|
|
|
|
iface = gr.Interface(fn=generate_response, |
|
|
inputs=[gr.Textbox(label="Your Message"), |
|
|
gr.Radio(["Siberian Persona", "Poet Persona"], label="Choose Prompt")], |
|
|
outputs="text", |
|
|
title="Siberian Persona Chatbot", |
|
|
description="A chatbot that responds with a Siberian persona or as a poet. Choose your prompt.") |
|
|
|
|
|
|
|
|
iface.launch() |
|
|
|
|
|
|