File size: 1,590 Bytes
42ccf19
 
deed210
14e67e3
42ccf19
 
9865594
42ccf19
a7e2438
42ccf19
2083406
42ccf19
 
b5e2b48
42ccf19
9865594
 
42ccf19
9865594
42ccf19
 
 
 
b5e2b48
9865594
b5e2b48
42ccf19
9865594
42ccf19
 
 
a121d15
42ccf19
a121d15
2083406
9865594
d1db060
42ccf19
 
a121d15
 
42ccf19
a121d15
a7e2438
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
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import gradio as gr

# Загрузка модели и токенизатора
model_name = "sberbank-ai/rugpt3medium_based_on_gpt2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# Функция генерации ответа
def generate_response(prompt):
    instruction = f"Ответь кратко и понятно на вопрос: {prompt.strip()}"
    input_ids = tokenizer.encode(instruction, return_tensors="pt")

    # Генерация ответа
    output = model.generate(
        input_ids,
        max_length=150,
        do_sample=True,
        top_k=50,
        top_p=0.95,
        temperature=0.8,
        pad_token_id=tokenizer.eos_token_id,
        eos_token_id=tokenizer.eos_token_id
    )

    # Декодирование и удаление излишней части промпта
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    
    # Возвращаем только ответ, без дублирования запроса
    return response.replace(instruction, "").strip()

# Интерфейс Gradio
iface = gr.Interface(
    fn=generate_response,
    inputs=gr.Textbox(label="Введите ваш вопрос"),
    outputs=gr.Textbox(label="Ответ модели"),
    title="Ответ от ruGPT-3 Medium",
    description="Генерация ответа с помощью модели Sberbank ruGPT-3 Medium"
)

# Запуск интерфейса
iface.launch()