File size: 4,643 Bytes
57645e8
66fabfa
f4f65e1
52af11a
f79a4e6
4fb6307
2a186cd
 
 
4fb6307
66fabfa
240efaf
dad551e
 
 
 
 
 
52af11a
f79a4e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
079a9a0
f79a4e6
52af11a
f79a4e6
 
 
 
ff077e4
66fabfa
f79a4e6
f5f461a
f79a4e6
240efaf
f5f461a
f79a4e6
f5f461a
f79a4e6
240efaf
f5f461a
f79a4e6
 
 
 
 
f5f461a
 
66fabfa
f79a4e6
 
 
 
 
 
66fabfa
 
240efaf
60a6dd5
f79a4e6
f5f461a
f79a4e6
f5f461a
 
ff077e4
f79a4e6
f5f461a
 
 
 
 
ff077e4
f79a4e6
f5f461a
 
 
 
 
ff077e4
f79a4e6
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

# Настройка моделей
models = {
    "ChatGPT-like (FRED-T5)": pipeline("text2text-generation", model="ai-forever/FRED-T5-1.7B", tokenizer="ai-forever/FRED-T5-1.7B", device=-1),
    "DeepSeek-like (Qwen7B)": pipeline("text-generation", model="lightblue/DeepSeek-R1-Distill-Qwen-7B-Multilingual", tokenizer="lightblue/DeepSeek-R1-Distill-Qwen-7B-Multilingual", device=-1),
    "GigaChat-like (GigaChat-20B)": pipeline("text-generation", model="ai-sage/GigaChat-20B-A3B-instruct", tokenizer="ai-sage/GigaChat-20B-A3B-instruct", 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=300):
    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 (FRED-T5)"]["cot_answer"], result["ChatGPT-like (FRED-T5)"]["cot_time"],
        result["ChatGPT-like (FRED-T5)"]["simple_answer"], result["ChatGPT-like (FRED-T5)"]["simple_time"],
        result["DeepSeek-like (Qwen7B)"]["cot_answer"], result["DeepSeek-like (Qwen7B)"]["cot_time"],
        result["DeepSeek-like (Qwen7B)"]["simple_answer"], result["DeepSeek-like (Qwen7B)"]["simple_time"],
        result["GigaChat-like (GigaChat-20B)"]["cot_answer"], result["GigaChat-like (GigaChat-20B)"]["cot_time"],
        result["GigaChat-like (GigaChat-20B)"]["simple_answer"], result["GigaChat-like (GigaChat-20B)"]["simple_time"]
    )

# Интерфейс
with gr.Blocks() as demo:
    gr.Markdown("## Сравнение моделей: ChatGPT, DeepSeek, GigaChat (банковская классификация)")

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

    # ChatGPT-like
    gr.Markdown("### ChatGPT-like (FRED-T5)")
    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 (Qwen7B)")
    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 (GigaChat-20B)")
    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()