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