Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,108 +2,106 @@ import gradio as gr
|
|
2 |
import time
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
#
|
6 |
models = {
|
7 |
-
"ChatGPT-like": pipeline(
|
8 |
-
|
9 |
-
|
10 |
-
tokenizer="tiiuae/falcon-rw-1b",
|
11 |
-
device=-1
|
12 |
-
),
|
13 |
-
"DeepSeek-like": pipeline(
|
14 |
-
"text-generation",
|
15 |
-
model="deepseek-ai/DeepSeek-Coder-1.3B-instruct",
|
16 |
-
tokenizer="deepseek-ai/DeepSeek-Coder-1.3B-instruct",
|
17 |
-
device=-1
|
18 |
-
),
|
19 |
-
"GigaChat-like": pipeline(
|
20 |
-
"text-generation",
|
21 |
-
model="tinkoff-ai/ruDialoGPT-medium",
|
22 |
-
tokenizer="tinkoff-ai/ruDialoGPT-medium",
|
23 |
-
device=-1
|
24 |
-
)
|
25 |
}
|
26 |
|
27 |
# Промпты
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
-
#
|
42 |
-
def generate_classification(user_input):
|
43 |
-
prompt_simple = build_simple_prompt(user_input)
|
44 |
-
prompt_cot = build_cot_prompt(user_input)
|
45 |
|
46 |
-
|
|
|
|
|
|
|
47 |
|
48 |
for name, pipe in models.items():
|
49 |
-
# CoT
|
50 |
start_cot = time.time()
|
51 |
-
|
52 |
time_cot = round(time.time() - start_cot, 2)
|
53 |
|
54 |
-
#
|
55 |
start_simple = time.time()
|
56 |
-
|
57 |
time_simple = round(time.time() - start_simple, 2)
|
58 |
|
59 |
-
|
60 |
-
"
|
61 |
-
"cot_time": time_cot,
|
62 |
-
"
|
63 |
-
"simple_time": time_simple
|
64 |
}
|
65 |
|
66 |
return (
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
)
|
74 |
|
75 |
# Интерфейс
|
76 |
with gr.Blocks() as demo:
|
77 |
-
gr.Markdown("##
|
78 |
|
79 |
-
inp = gr.Textbox(label="Вопрос клиента", placeholder="Например: Я не могу
|
80 |
btn = gr.Button("Сгенерировать")
|
81 |
|
82 |
# ChatGPT-like
|
83 |
-
gr.Markdown("### ChatGPT-like (
|
84 |
cot1 = gr.Textbox(label="CoT ответ")
|
85 |
cot1_time = gr.Textbox(label="Время CoT")
|
86 |
simple1 = gr.Textbox(label="Обычный ответ")
|
87 |
simple1_time = gr.Textbox(label="Время обычного")
|
88 |
|
89 |
# DeepSeek-like
|
90 |
-
gr.Markdown("### DeepSeek-like")
|
91 |
cot2 = gr.Textbox(label="CoT ответ")
|
92 |
cot2_time = gr.Textbox(label="Время CoT")
|
93 |
simple2 = gr.Textbox(label="Обычный ответ")
|
94 |
simple2_time = gr.Textbox(label="Время обычного")
|
95 |
|
96 |
# GigaChat-like
|
97 |
-
gr.Markdown("### GigaChat-like (
|
98 |
cot3 = gr.Textbox(label="CoT ответ")
|
99 |
cot3_time = gr.Textbox(label="Время CoT")
|
100 |
simple3 = gr.Textbox(label="Обычный ответ")
|
101 |
simple3_time = gr.Textbox(label="Время обычного")
|
102 |
|
103 |
-
btn.click(
|
104 |
cot1, cot1_time, simple1, simple1_time,
|
105 |
cot2, cot2_time, simple2, simple2_time,
|
106 |
cot3, cot3_time, simple3, simple3_time
|
107 |
])
|
108 |
|
109 |
-
|
|
|
|
2 |
import time
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Настройка моделей
|
6 |
models = {
|
7 |
+
"ChatGPT-like (FRED-T5)": pipeline("text2text-generation", model="ai-forever/FRED-T5-1.7B", tokenizer="ai-forever/FRED-T5-1.7B", device_map="auto"),
|
8 |
+
"DeepSeek-like (Qwen7B)": pipeline("text-generation", model="lightblue/DeepSeek-R1-Distill-Qwen-7B-Multilingual", tokenizer="lightblue/DeepSeek-R1-Distill-Qwen-7B-Multilingual", device_map="auto"),
|
9 |
+
"GigaChat-like (GigaChat-20B)": pipeline("text-generation", model="ai-sage/GigaChat-20B-A3B-instruct", tokenizer="ai-sage/GigaChat-20B-A3B-instruct", device_map="auto")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
}
|
11 |
|
12 |
# Промпты
|
13 |
+
cot_instruction = (
|
14 |
+
"Ты — банковский специалист. Клиент задал вопрос."
|
15 |
+
" Проанализируй обращение пошагово:
|
16 |
+
1. В чём проблема?
|
17 |
+
2. Возможные причины?
|
18 |
+
3. Как решить?
|
19 |
+
Вывод: категория обращения."
|
20 |
+
)
|
21 |
|
22 |
+
simple_instruction = "Ты — банковский специалист. Определи категорию обращения клиента кратко."
|
23 |
+
|
24 |
+
# Генерация промптов
|
25 |
+
|
26 |
+
def build_prompt(instruction, user_input):
|
27 |
+
return f"{instruction}\n\nКлиент: {user_input}"
|
28 |
+
|
29 |
+
# Обработка ответа
|
30 |
+
|
31 |
+
def get_output(pipe, prompt, max_tokens=300):
|
32 |
+
try:
|
33 |
+
output = pipe(prompt, max_new_tokens=max_tokens, truncation=True, do_sample=True, temperature=0.7)[0]
|
34 |
+
return output.get("generated_text") or output.get("output_text") or "(нет ответа)"
|
35 |
+
except Exception as e:
|
36 |
+
return f"Ошибка: {e}"
|
37 |
|
38 |
+
# Основная функция
|
|
|
|
|
|
|
39 |
|
40 |
+
def generate_comparison(user_input):
|
41 |
+
result = {}
|
42 |
+
prompt_cot = build_prompt(cot_instruction, user_input)
|
43 |
+
prompt_simple = build_prompt(simple_instruction, user_input)
|
44 |
|
45 |
for name, pipe in models.items():
|
46 |
+
# CoT ответ
|
47 |
start_cot = time.time()
|
48 |
+
answer_cot = get_output(pipe, prompt_cot)
|
49 |
time_cot = round(time.time() - start_cot, 2)
|
50 |
|
51 |
+
# Обычный ответ
|
52 |
start_simple = time.time()
|
53 |
+
answer_simple = get_output(pipe, prompt_simple)
|
54 |
time_simple = round(time.time() - start_simple, 2)
|
55 |
|
56 |
+
result[name] = {
|
57 |
+
"cot_answer": answer_cot.strip(),
|
58 |
+
"cot_time": f"{time_cot} сек",
|
59 |
+
"simple_answer": answer_simple.strip(),
|
60 |
+
"simple_time": f"{time_simple} сек"
|
61 |
}
|
62 |
|
63 |
return (
|
64 |
+
result["ChatGPT-like (FRED-T5)"]["cot_answer"], result["ChatGPT-like (FRED-T5)"]["cot_time"],
|
65 |
+
result["ChatGPT-like (FRED-T5)"]["simple_answer"], result["ChatGPT-like (FRED-T5)"]["simple_time"],
|
66 |
+
result["DeepSeek-like (Qwen7B)"]["cot_answer"], result["DeepSeek-like (Qwen7B)"]["cot_time"],
|
67 |
+
result["DeepSeek-like (Qwen7B)"]["simple_answer"], result["DeepSeek-like (Qwen7B)"]["simple_time"],
|
68 |
+
result["GigaChat-like (GigaChat-20B)"]["cot_answer"], result["GigaChat-like (GigaChat-20B)"]["cot_time"],
|
69 |
+
result["GigaChat-like (GigaChat-20B)"]["simple_answer"], result["GigaChat-like (GigaChat-20B)"]["simple_time"]
|
70 |
)
|
71 |
|
72 |
# Интерфейс
|
73 |
with gr.Blocks() as demo:
|
74 |
+
gr.Markdown("## Сравнение моделей: ChatGPT, DeepSeek, GigaChat (банковская классификация)")
|
75 |
|
76 |
+
inp = gr.Textbox(label="Вопрос клиента", placeholder="Например: Я не могу оплатить картой", lines=2)
|
77 |
btn = gr.Button("Сгенерировать")
|
78 |
|
79 |
# ChatGPT-like
|
80 |
+
gr.Markdown("### ChatGPT-like (FRED-T5)")
|
81 |
cot1 = gr.Textbox(label="CoT ответ")
|
82 |
cot1_time = gr.Textbox(label="Время CoT")
|
83 |
simple1 = gr.Textbox(label="Обычный ответ")
|
84 |
simple1_time = gr.Textbox(label="Время обычного")
|
85 |
|
86 |
# DeepSeek-like
|
87 |
+
gr.Markdown("### DeepSeek-like (Qwen7B)")
|
88 |
cot2 = gr.Textbox(label="CoT ответ")
|
89 |
cot2_time = gr.Textbox(label="Время CoT")
|
90 |
simple2 = gr.Textbox(label="Обычный ответ")
|
91 |
simple2_time = gr.Textbox(label="Время обычного")
|
92 |
|
93 |
# GigaChat-like
|
94 |
+
gr.Markdown("### GigaChat-like (GigaChat-20B)")
|
95 |
cot3 = gr.Textbox(label="CoT ответ")
|
96 |
cot3_time = gr.Textbox(label="Время CoT")
|
97 |
simple3 = gr.Textbox(label="Обычный ответ")
|
98 |
simple3_time = gr.Textbox(label="Время обычного")
|
99 |
|
100 |
+
btn.click(generate_comparison, inputs=[inp], outputs=[
|
101 |
cot1, cot1_time, simple1, simple1_time,
|
102 |
cot2, cot2_time, simple2, simple2_time,
|
103 |
cot3, cot3_time, simple3, simple3_time
|
104 |
])
|
105 |
|
106 |
+
if __name__ == '__main__':
|
107 |
+
demo.launch()
|