File size: 4,923 Bytes
57645e8
66fabfa
f4f65e1
52af11a
ef90d3b
4fb6307
ef90d3b
 
 
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
ef90d3b
 
 
 
 
 
66fabfa
 
240efaf
60a6dd5
ef90d3b
f5f461a
ef90d3b
f5f461a
 
ff077e4
ef90d3b
f5f461a
 
 
 
 
ff077e4
ef90d3b
f5f461a
 
 
 
 
ff077e4
ef90d3b
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 (Falcon)": pipeline("text-generation", model="tiiuae/falcon-7b-instruct", tokenizer="tiiuae/falcon-7b-instruct", device=-1),
    "DeepSeek-like": pipeline("text-generation", model="deepseek-ai/deepseek-coder-6.7b-instruct", tokenizer="deepseek-ai/deepseek-coder-6.7b-instruct", device=-1),
    "GigaChat-like (Mistral)": pipeline("text-generation", model="mistralai/Mistral-7B-Instruct", tokenizer="mistralai/Mistral-7B-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=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 (Falcon)"]["cot_answer"], result["ChatGPT-like (Falcon)"]["cot_time"],
        result["ChatGPT-like (Falcon)"]["simple_answer"], result["ChatGPT-like (Falcon)"]["simple_time"],
        result["DeepSeek-like"]["cot_answer"], result["DeepSeek-like"]["cot_time"],
        result["DeepSeek-like"]["simple_answer"], result["DeepSeek-like"]["simple_time"],
        result["GigaChat-like (Mistral)"]["cot_answer"], result["GigaChat-like (Mistral)"]["cot_time"],
        result["GigaChat-like (Mistral)"]["simple_answer"], result["GigaChat-like (Mistral)"]["simple_time"]
    )

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

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

    # ChatGPT-like
    gr.Markdown("### ChatGPT-like (Falcon)")
    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")
    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 (Mistral)")
    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()