File size: 5,096 Bytes
57645e8
66fabfa
f4f65e1
52af11a
6915470
4fb6307
6915470
 
 
4fb6307
66fabfa
240efaf
ef90d3b
 
 
 
 
 
52af11a
ef90d3b
f79a4e6
ef90d3b
f79a4e6
 
 
 
ef90d3b
f79a4e6
ef90d3b
f79a4e6
 
 
 
 
079a9a0
ef90d3b
52af11a
f79a4e6
 
 
 
ff077e4
66fabfa
f79a4e6
f5f461a
f79a4e6
240efaf
f5f461a
f79a4e6
f5f461a
f79a4e6
240efaf
f5f461a
f79a4e6
 
 
 
 
f5f461a
 
66fabfa
6915470
 
 
 
 
 
66fabfa
 
240efaf
60a6dd5
6915470
f5f461a
ef90d3b
f5f461a
 
ff077e4
6915470
f5f461a
 
 
 
 
ff077e4
6915470
f5f461a
 
 
 
 
ff077e4
6915470
f5f461a
 
 
 
 
f79a4e6
f5f461a
 
 
 
a4bd38b
f79a4e6
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
import time
from transformers import pipeline

# Проверенные лёгкие русскоязычные модели, работающие в Hugging Face Spaces
models = {
    "ChatGPT-like (ruGPT3small)": pipeline("text-generation", model="ai-forever/rugpt3small_based_on_gpt2", tokenizer="ai-forever/rugpt3small_based_on_gpt2", device=-1),
    "DeepSeek-like (ruGPT3large)": pipeline("text-generation", model="ai-forever/rugpt3large_based_on_gpt2", tokenizer="ai-forever/rugpt3large_based_on_gpt2", device=-1),
    "GigaChat-like (rubert-tiny2)": pipeline("text-generation", model="cointegrated/rubert-tiny2", tokenizer="cointegrated/rubert-tiny2", device=-1),
}

# Промпты
cot_instruction = """Ты — банковский помощник. Клиент описал проблему.
Проанализируй обращение шаг за шагом:
1. Что случилось?
2. Почему это могло произойти?
3. Как клиенту поступить?
Вывод: укажи категорию обращения (например: доступ, безопасность, платежи, перевод и т.д.)"""

simple_instruction = "Ты — банковский помощник. Определи кратко, к какой категории относится обращение клиента (например: доступ, платежи, безопасность и т.д.)."

# Формирование промптов

def build_prompt(instruction, user_input):
    return f"{instruction}\n\nКлиент: {user_input}"

# Генерация вывода с защитой

def get_output(pipe, prompt, max_tokens=200):
    try:
        output = pipe(prompt, max_new_tokens=max_tokens, truncation=True, do_sample=True, temperature=0.7)[0]
        return output.get("generated_text") or output.get("output_text") or "(нет ответа)"
    except Exception as e:
        return f"Ошибка: {e}"

# Основная функция сравнения

def generate_comparison(user_input):
    result = {}
    prompt_cot = build_prompt(cot_instruction, user_input)
    prompt_simple = build_prompt(simple_instruction, user_input)

    for name, pipe in models.items():
        # CoT ответ
        start_cot = time.time()
        answer_cot = get_output(pipe, prompt_cot)
        time_cot = round(time.time() - start_cot, 2)

        # Обычный ответ
        start_simple = time.time()
        answer_simple = get_output(pipe, prompt_simple)
        time_simple = round(time.time() - start_simple, 2)

        result[name] = {
            "cot_answer": answer_cot.strip(),
            "cot_time": f"{time_cot} сек",
            "simple_answer": answer_simple.strip(),
            "simple_time": f"{time_simple} сек"
        }

    return (
        result["ChatGPT-like (ruGPT3small)"]["cot_answer"], result["ChatGPT-like (ruGPT3small)"]["cot_time"],
        result["ChatGPT-like (ruGPT3small)"]["simple_answer"], result["ChatGPT-like (ruGPT3small)"]["simple_time"],
        result["DeepSeek-like (ruGPT3large)"]["cot_answer"], result["DeepSeek-like (ruGPT3large)"]["cot_time"],
        result["DeepSeek-like (ruGPT3large)"]["simple_answer"], result["DeepSeek-like (ruGPT3large)"]["simple_time"],
        result["GigaChat-like (rubert-tiny2)"]["cot_answer"], result["GigaChat-like (rubert-tiny2)"]["cot_time"],
        result["GigaChat-like (rubert-tiny2)"]["simple_answer"], result["GigaChat-like (rubert-tiny2)"]["simple_time"]
    )

# Интерфейс
with gr.Blocks() as demo:
    gr.Markdown("## Сравнение моделей: ruGPT3small, ruGPT3large, rubert-tiny2 (Классификация обращений)")

    inp = gr.Textbox(label="Вопрос клиента", placeholder="Например: Я не могу войти в личный кабинет", lines=2)
    btn = gr.Button("Сгенерировать")

    # ChatGPT-like
    gr.Markdown("### ChatGPT-like (ruGPT3small)")
    cot1 = gr.Textbox(label="CoT ответ")
    cot1_time = gr.Textbox(label="Время CoT")
    simple1 = gr.Textbox(label="Обычный ответ")
    simple1_time = gr.Textbox(label="Время обычного")

    # DeepSeek-like
    gr.Markdown("### DeepSeek-like (ruGPT3large)")
    cot2 = gr.Textbox(label="CoT ответ")
    cot2_time = gr.Textbox(label="Время CoT")
    simple2 = gr.Textbox(label="Обычный ответ")
    simple2_time = gr.Textbox(label="Время обычного")

    # GigaChat-like
    gr.Markdown("### GigaChat-like (rubert-tiny2)")
    cot3 = gr.Textbox(label="CoT ответ")
    cot3_time = gr.Textbox(label="Время CoT")
    simple3 = gr.Textbox(label="Обычный ответ")
    simple3_time = gr.Textbox(label="Время обычного")

    btn.click(generate_comparison, inputs=[inp], outputs=[
        cot1, cot1_time, simple1, simple1_time,
        cot2, cot2_time, simple2, simple2_time,
        cot3, cot3_time, simple3, simple3_time
    ])

if __name__ == '__main__':
    demo.launch()