File size: 2,493 Bytes
353506f
912f8e2
 
 
 
 
 
 
 
 
 
 
 
 
353506f
912f8e2
 
 
 
 
353506f
 
 
a1fcc37
353506f
 
912f8e2
 
 
 
 
 
 
 
aeb9ee6
912f8e2
 
 
 
 
a1fcc37
 
912f8e2
 
353506f
912f8e2
 
 
aeb9ee6
 
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
44
45
46
47
48
49
50
import torch
import transformers
import gradio as gr

# Check if CUDA is available and set the device
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")

# Load the tokenizer and model
t5_tokenizer = transformers.GPT2Tokenizer.from_pretrained("SiberiaSoft/SiberianPersonaFred-2")
t5_model = transformers.T5ForConditionalGeneration.from_pretrained("SiberiaSoft/SiberianPersonaFred-2").to(device)

# Define the function to generate responses
def generate_response(dialog, prompt_choice):
    dialog = dialog.strip()
    if len(dialog) == 0:
        return "Please enter a message."
    
    dialog = dialog[0].upper() + dialog[1:]
    
    # Choose the prompt based on user selection
    if prompt_choice == "Siberian Persona":
        prompt = '<SC6>Я культурный гопник, ем семечки, люблю играть в доту 2 на любимом герое инвокере, говорю без матов и маты не люблю, недавно у меня был такой диалог:' + '\nТы: ' + dialog + '\nЯ: <extra_id_0>'
    else:  # "Poet Persona"
        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

# Create a Gradio interface
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.")

# Launch the interface
iface.launch()