Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,12 @@ import gradio as gr
|
|
2 |
import time
|
3 |
from transformers import pipeline
|
4 |
|
5 |
-
# Инициализация моделей
|
6 |
models = {
|
7 |
"ChatGPT-like": pipeline(
|
8 |
"text-generation",
|
9 |
-
model="
|
10 |
-
tokenizer="
|
11 |
device=-1
|
12 |
),
|
13 |
"DeepSeek-like": pipeline(
|
@@ -18,84 +18,82 @@ models = {
|
|
18 |
),
|
19 |
"GigaChat-like": pipeline(
|
20 |
"text-generation",
|
21 |
-
model="tinkoff-ai/
|
22 |
-
tokenizer="tinkoff-ai/
|
23 |
device=-1
|
24 |
)
|
25 |
}
|
26 |
|
27 |
-
#
|
28 |
def build_simple_prompt(user_input):
|
29 |
return f"Клиент: {user_input}\nКатегория обращения:"
|
30 |
|
31 |
-
# Промпт с рассуждением
|
32 |
def build_cot_prompt(user_input):
|
33 |
return (
|
34 |
f"Клиент: {user_input}\n"
|
35 |
-
"Проанализируй обращение
|
36 |
-
"1. В чём
|
37 |
-
"2.
|
38 |
-
"3.
|
39 |
-
"
|
40 |
)
|
41 |
|
42 |
-
# Генерация
|
43 |
def generate_classification(user_input):
|
44 |
prompt_simple = build_simple_prompt(user_input)
|
45 |
prompt_cot = build_cot_prompt(user_input)
|
46 |
|
47 |
results = {}
|
48 |
-
|
49 |
for name, pipe in models.items():
|
50 |
# CoT
|
51 |
start_cot = time.time()
|
52 |
-
|
53 |
-
|
54 |
|
55 |
# Simple
|
56 |
start_simple = time.time()
|
57 |
-
|
58 |
-
|
59 |
|
60 |
results[name] = {
|
61 |
-
"
|
62 |
-
"cot_time":
|
63 |
-
"
|
64 |
-
"simple_time":
|
65 |
}
|
66 |
|
67 |
return (
|
68 |
-
results["ChatGPT-like"]["
|
69 |
-
results["ChatGPT-like"]["
|
70 |
-
results["DeepSeek-like"]["
|
71 |
-
results["DeepSeek-like"]["
|
72 |
-
results["GigaChat-like"]["
|
73 |
-
results["GigaChat-like"]["
|
74 |
)
|
75 |
|
76 |
-
#
|
77 |
with gr.Blocks() as demo:
|
78 |
-
gr.Markdown("##
|
79 |
|
80 |
inp = gr.Textbox(label="Вопрос клиента", placeholder="Например: Я не могу перевести деньги", lines=2)
|
81 |
btn = gr.Button("Сгенерировать")
|
82 |
|
83 |
-
# ChatGPT
|
84 |
-
gr.Markdown("### ChatGPT-like (
|
85 |
cot1 = gr.Textbox(label="CoT ответ")
|
86 |
cot1_time = gr.Textbox(label="Время CoT")
|
87 |
simple1 = gr.Textbox(label="Обычный ответ")
|
88 |
simple1_time = gr.Textbox(label="Время обычного")
|
89 |
|
90 |
-
# DeepSeek
|
91 |
gr.Markdown("### DeepSeek-like")
|
92 |
cot2 = gr.Textbox(label="CoT ответ")
|
93 |
cot2_time = gr.Textbox(label="Время CoT")
|
94 |
simple2 = gr.Textbox(label="Обычный ответ")
|
95 |
simple2_time = gr.Textbox(label="Время обычного")
|
96 |
|
97 |
-
# GigaChat
|
98 |
-
gr.Markdown("### GigaChat-like (
|
99 |
cot3 = gr.Textbox(label="CoT ответ")
|
100 |
cot3_time = gr.Textbox(label="Время CoT")
|
101 |
simple3 = gr.Textbox(label="Обычный ответ")
|
|
|
2 |
import time
|
3 |
from transformers import pipeline
|
4 |
|
5 |
+
# Инициализация моделей
|
6 |
models = {
|
7 |
"ChatGPT-like": pipeline(
|
8 |
"text-generation",
|
9 |
+
model="OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
10 |
+
tokenizer="OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5",
|
11 |
device=-1
|
12 |
),
|
13 |
"DeepSeek-like": pipeline(
|
|
|
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 |
def build_simple_prompt(user_input):
|
29 |
return f"Клиент: {user_input}\nКатегория обращения:"
|
30 |
|
|
|
31 |
def build_cot_prompt(user_input):
|
32 |
return (
|
33 |
f"Клиент: {user_input}\n"
|
34 |
+
"Проанализируй обращение пошагово:\n"
|
35 |
+
"1. В чём суть проблемы?\n"
|
36 |
+
"2. Возможные причины?\n"
|
37 |
+
"3. Решение?\n"
|
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 |
results = {}
|
|
|
47 |
for name, pipe in models.items():
|
48 |
# CoT
|
49 |
start_cot = time.time()
|
50 |
+
cot_out = pipe(prompt_cot, max_new_tokens=150, do_sample=True, top_p=0.9, temperature=0.7)[0]["generated_text"]
|
51 |
+
time_cot = round(time.time() - start_cot, 2)
|
52 |
|
53 |
# Simple
|
54 |
start_simple = time.time()
|
55 |
+
simple_out = pipe(prompt_simple, max_new_tokens=80, do_sample=True, top_p=0.9, temperature=0.7)[0]["generated_text"]
|
56 |
+
time_simple = round(time.time() - start_simple, 2)
|
57 |
|
58 |
results[name] = {
|
59 |
+
"cot": cot_out.strip(),
|
60 |
+
"cot_time": time_cot,
|
61 |
+
"simple": simple_out.strip(),
|
62 |
+
"simple_time": time_simple
|
63 |
}
|
64 |
|
65 |
return (
|
66 |
+
results["ChatGPT-like"]["cot"], f"{results['ChatGPT-like']['cot_time']} сек",
|
67 |
+
results["ChatGPT-like"]["simple"], f"{results['ChatGPT-like']['simple_time']} сек",
|
68 |
+
results["DeepSeek-like"]["cot"], f"{results['DeepSeek-like']['cot_time']} сек",
|
69 |
+
results["DeepSeek-like"]["simple"], f"{results['DeepSeek-like']['simple_time']} сек",
|
70 |
+
results["GigaChat-like"]["cot"], f"{results['GigaChat-like']['cot_time']} сек",
|
71 |
+
results["GigaChat-like"]["simple"], f"{results['GigaChat-like']['simple_time']} сек"
|
72 |
)
|
73 |
|
74 |
+
# Интерфейс
|
75 |
with gr.Blocks() as demo:
|
76 |
+
gr.Markdown("## 🤖 Сравнение моделей: ChatGPT, DeepSeek, GigaChat — классификация банковских обращений")
|
77 |
|
78 |
inp = gr.Textbox(label="Вопрос клиента", placeholder="Например: Я не могу перевести деньги", lines=2)
|
79 |
btn = gr.Button("Сгенерировать")
|
80 |
|
81 |
+
# ChatGPT
|
82 |
+
gr.Markdown("### ChatGPT-like (OpenAssistant)")
|
83 |
cot1 = gr.Textbox(label="CoT ответ")
|
84 |
cot1_time = gr.Textbox(label="Время CoT")
|
85 |
simple1 = gr.Textbox(label="Обычный ответ")
|
86 |
simple1_time = gr.Textbox(label="Время обычного")
|
87 |
|
88 |
+
# DeepSeek
|
89 |
gr.Markdown("### DeepSeek-like")
|
90 |
cot2 = gr.Textbox(label="CoT ответ")
|
91 |
cot2_time = gr.Textbox(label="Время CoT")
|
92 |
simple2 = gr.Textbox(label="Обычный ответ")
|
93 |
simple2_time = gr.Textbox(label="Время обычного")
|
94 |
|
95 |
+
# GigaChat
|
96 |
+
gr.Markdown("### GigaChat-like (ruDialoGPT)")
|
97 |
cot3 = gr.Textbox(label="CoT ответ")
|
98 |
cot3_time = gr.Textbox(label="Время CoT")
|
99 |
simple3 = gr.Textbox(label="Обычный ответ")
|