File size: 1,053 Bytes
e787c78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "sberbank-ai/rugpt3small_based_on_gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

chat_history = []

def chat_with_ai(user_input):
    global chat_history
    context = " ".join(chat_history) + " " + user_input
    inputs = tokenizer(context, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=200, pad_token_id=tokenizer.eos_token_id)
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    chat_history.append(f"Пользователь: {user_input}")
    chat_history.append(f"ИИ: {response}")
    return response

iface = gr.Interface(
    fn=chat_with_ai,
    inputs=gr.Textbox(lines=2, placeholder="Напиши что-нибудь..."),
    outputs="text",
    title="Русский чат-бот ИИ",
    description="ИИ, который запоминает диалог и отвечает на русском"
)

iface.launch()