File size: 1,758 Bytes
42ccf19
 
deed210
14e67e3
13917a5
42ccf19
13917a5
 
9865594
42ccf19
a7e2438
13917a5
2083406
13917a5
42ccf19
b5e2b48
13917a5
9865594
 
13917a5
9865594
42ccf19
 
13917a5
42ccf19
b5e2b48
9865594
b5e2b48
1a767a0
 
42ccf19
a121d15
42ccf19
a121d15
2083406
9865594
d1db060
42ccf19
 
a121d15
 
42ccf19
a121d15
a7e2438
1a767a0
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
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"Ответь кратко и по существу на вопрос:\n{prompt.strip()}\nОтвет:"
    input_ids = tokenizer.encode(instruction, return_tensors="pt")

    # Параметры генерации для уменьшения времени отклика
    output = model.generate(
        input_ids,
        max_new_tokens=50,  # Уменьшение числа токенов для более короткого ответа
        do_sample=True,
        top_k=50,
        top_p=0.95,
        temperature=0.7,  # Уменьшение случайности
        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()