Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,52 +3,52 @@ import time
|
|
3 |
from transformers import pipeline
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
-
# Загружаем датасет
|
7 |
dataset = load_dataset("Romjiik/Russian_bank_reviews", split="train")
|
8 |
|
9 |
-
# Примеры для few-shot
|
10 |
few_shot_examples = []
|
11 |
for row in dataset.select(range(2)):
|
12 |
review = row["review"]
|
13 |
-
|
14 |
-
few_shot_examples.append(
|
15 |
|
16 |
-
#
|
17 |
cot_instruction = (
|
18 |
-
"Ты —
|
19 |
-
"
|
20 |
-
" Вывод: укажи категорию обращения (например: доступ, безопасность, платежи, перевод и т.д.)"
|
21 |
)
|
22 |
|
23 |
simple_instruction = (
|
24 |
-
"Ты —
|
25 |
-
" (например: доступ, платежи, безопасность и т.д.)."
|
26 |
)
|
27 |
|
28 |
-
#
|
29 |
-
models = {
|
30 |
-
"ChatGPT-like (ruGPT3-medium)": pipeline("text-generation", model="cointegrated/rugpt3-medium", tokenizer="cointegrated/rugpt3-medium", device=-1),
|
31 |
-
"DeepSeek-like (ruGPT3-small)": pipeline("text-generation", model="ai-forever/rugpt3small_based_on_gpt2", tokenizer="ai-forever/rugpt3small_based_on_gpt2", device=-1),
|
32 |
-
"GigaChat-like (rubert-tiny2)": pipeline("text-generation", model="cointegrated/rubert-tiny2", tokenizer="cointegrated/rubert-tiny2", device=-1),
|
33 |
-
}
|
34 |
|
35 |
-
# Промпт CoT
|
36 |
def build_cot_prompt(user_input):
|
37 |
examples = "\n\n".join(few_shot_examples)
|
38 |
return (
|
39 |
f"{cot_instruction}\n\n{examples}\n\nКлиент: {user_input}\n"
|
40 |
-
"
|
41 |
)
|
42 |
|
43 |
-
|
44 |
def build_simple_prompt(user_input):
|
45 |
examples = "\n\n".join(few_shot_examples)
|
46 |
return (
|
47 |
f"{simple_instruction}\n\n{examples}\n\nКлиент: {user_input}\n"
|
48 |
-
"Категория:"
|
49 |
)
|
50 |
|
51 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
def generate_dual_answers(user_input):
|
53 |
results = {}
|
54 |
prompt_cot = build_cot_prompt(user_input)
|
@@ -57,62 +57,63 @@ def generate_dual_answers(user_input):
|
|
57 |
for name, pipe in models.items():
|
58 |
# CoT
|
59 |
start_cot = time.time()
|
60 |
-
|
|
|
|
|
|
|
|
|
61 |
end_cot = round(time.time() - start_cot, 2)
|
62 |
-
answer_cot = out_cot.strip().split('\n')[-1]
|
63 |
|
64 |
# Simple
|
65 |
start_simple = time.time()
|
66 |
-
|
|
|
|
|
|
|
|
|
67 |
end_simple = round(time.time() - start_simple, 2)
|
68 |
-
answer_simple = out_simple.strip().split('\n')[-1]
|
69 |
|
70 |
results[name] = {
|
71 |
-
"cot_answer": answer_cot,
|
72 |
"cot_time": end_cot,
|
73 |
-
"simple_answer": answer_simple,
|
74 |
"simple_time": end_simple
|
75 |
}
|
76 |
|
77 |
return (
|
78 |
-
results["ChatGPT-like (
|
79 |
-
results["ChatGPT-like (
|
|
|
80 |
results["DeepSeek-like (ruGPT3-small)"]["cot_answer"], f"{results['DeepSeek-like (ruGPT3-small)']['cot_time']} сек",
|
81 |
results["DeepSeek-like (ruGPT3-small)"]["simple_answer"], f"{results['DeepSeek-like (ruGPT3-small)']['simple_time']} сек",
|
82 |
-
|
83 |
-
results["GigaChat-like (
|
|
|
84 |
)
|
85 |
|
86 |
# Интерфейс Gradio
|
87 |
with gr.Blocks() as demo:
|
88 |
-
gr.Markdown("##
|
89 |
-
|
90 |
-
|
91 |
-
btn = gr.Button("Получить ответы")
|
92 |
|
93 |
-
gr.Markdown("### ChatGPT-like (
|
94 |
-
cot1 = gr.Textbox(label="CoT ответ")
|
95 |
-
|
96 |
-
simple1 = gr.Textbox(label="Обычный ответ")
|
97 |
-
simple1_time = gr.Textbox(label="Время обычного")
|
98 |
|
99 |
gr.Markdown("### DeepSeek-like (ruGPT3-small)")
|
100 |
-
cot2 = gr.Textbox(label="CoT ответ")
|
101 |
-
|
102 |
-
simple2 = gr.Textbox(label="Обычный ответ")
|
103 |
-
simple2_time = gr.Textbox(label="Время обычного")
|
104 |
|
105 |
-
gr.Markdown("### GigaChat-like (
|
106 |
-
cot3 = gr.Textbox(label="CoT ответ")
|
107 |
-
|
108 |
-
simple3 = gr.Textbox(label="Обычный ответ")
|
109 |
-
simple3_time = gr.Textbox(label="Время обычного")
|
110 |
|
111 |
btn.click(generate_dual_answers, inputs=[inp], outputs=[
|
112 |
cot1, cot1_time, simple1, simple1_time,
|
113 |
cot2, cot2_time, simple2, simple2_time,
|
114 |
-
cot3, cot3_time, simple3, simple3_time
|
115 |
])
|
116 |
|
117 |
-
if __name__ ==
|
118 |
demo.launch()
|
|
|
3 |
from transformers import pipeline
|
4 |
from datasets import load_dataset
|
5 |
|
6 |
+
# Загружаем банковский датасет
|
7 |
dataset = load_dataset("Romjiik/Russian_bank_reviews", split="train")
|
8 |
|
9 |
+
# Примеры для few-shot
|
10 |
few_shot_examples = []
|
11 |
for row in dataset.select(range(2)):
|
12 |
review = row["review"]
|
13 |
+
example = f"Клиент: {review}\nКлассификация: прочее"
|
14 |
+
few_shot_examples.append(example)
|
15 |
|
16 |
+
# Инструкции
|
17 |
cot_instruction = (
|
18 |
+
"Ты — ассистент банка. Проанализируй обращение клиента и классифицируй его по теме."
|
19 |
+
" Сначала рассуждай шаг за шагом, затем выведи финальную категорию."
|
|
|
20 |
)
|
21 |
|
22 |
simple_instruction = (
|
23 |
+
"Ты — банковский помощник. Классифицируй обращение клиента одним словом — категорией."
|
|
|
24 |
)
|
25 |
|
26 |
+
# Промпты
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
28 |
def build_cot_prompt(user_input):
|
29 |
examples = "\n\n".join(few_shot_examples)
|
30 |
return (
|
31 |
f"{cot_instruction}\n\n{examples}\n\nКлиент: {user_input}\n"
|
32 |
+
f"Рассуждение:"
|
33 |
)
|
34 |
|
35 |
+
|
36 |
def build_simple_prompt(user_input):
|
37 |
examples = "\n\n".join(few_shot_examples)
|
38 |
return (
|
39 |
f"{simple_instruction}\n\n{examples}\n\nКлиент: {user_input}\n"
|
40 |
+
f"Категория:"
|
41 |
)
|
42 |
|
43 |
+
# Рабочие модели с поддержкой русского языка и легкие
|
44 |
+
models = {
|
45 |
+
"ChatGPT-like (FRED-T5-small)": pipeline("text2text-generation", model="cointegrated/translation-t5-russian-finetuned", tokenizer="cointegrated/translation-t5-russian-finetuned", device=-1),
|
46 |
+
"DeepSeek-like (ruGPT3-small)": pipeline("text-generation", model="ai-forever/rugpt3small_based_on_gpt2", tokenizer="ai-forever/rugpt3small_based_on_gpt2", device=-1),
|
47 |
+
"GigaChat-like (RuBERT-tiny2)": pipeline("text-classification", model="cointegrated/rubert-tiny2", tokenizer="cointegrated/rubert-tiny2", device=-1),
|
48 |
+
}
|
49 |
+
|
50 |
+
# Генерация ответов
|
51 |
+
|
52 |
def generate_dual_answers(user_input):
|
53 |
results = {}
|
54 |
prompt_cot = build_cot_prompt(user_input)
|
|
|
57 |
for name, pipe in models.items():
|
58 |
# CoT
|
59 |
start_cot = time.time()
|
60 |
+
try:
|
61 |
+
out_cot = pipe(prompt_cot, max_new_tokens=150, do_sample=True, top_p=0.9, temperature=0.7)[0]
|
62 |
+
answer_cot = out_cot.get("generated_text", out_cot.get("label", "-"))
|
63 |
+
except:
|
64 |
+
answer_cot = "Ошибка в CoT"
|
65 |
end_cot = round(time.time() - start_cot, 2)
|
|
|
66 |
|
67 |
# Simple
|
68 |
start_simple = time.time()
|
69 |
+
try:
|
70 |
+
out_simple = pipe(prompt_simple, max_new_tokens=150, do_sample=True, top_p=0.9, temperature=0.7)[0]
|
71 |
+
answer_simple = out_simple.get("generated_text", out_simple.get("label", "-"))
|
72 |
+
except:
|
73 |
+
answer_simple = "Ошибка в обычном"
|
74 |
end_simple = round(time.time() - start_simple, 2)
|
|
|
75 |
|
76 |
results[name] = {
|
77 |
+
"cot_answer": answer_cot.strip(),
|
78 |
"cot_time": end_cot,
|
79 |
+
"simple_answer": answer_simple.strip(),
|
80 |
"simple_time": end_simple
|
81 |
}
|
82 |
|
83 |
return (
|
84 |
+
results["ChatGPT-like (FRED-T5-small)"]["cot_answer"], f"{results['ChatGPT-like (FRED-T5-small)']['cot_time']} сек",
|
85 |
+
results["ChatGPT-like (FRED-T5-small)"]["simple_answer"], f"{results['ChatGPT-like (FRED-T5-small)']['simple_time']} сек",
|
86 |
+
|
87 |
results["DeepSeek-like (ruGPT3-small)"]["cot_answer"], f"{results['DeepSeek-like (ruGPT3-small)']['cot_time']} сек",
|
88 |
results["DeepSeek-like (ruGPT3-small)"]["simple_answer"], f"{results['DeepSeek-like (ruGPT3-small)']['simple_time']} сек",
|
89 |
+
|
90 |
+
results["GigaChat-like (RuBERT-tiny2)"]["cot_answer"], f"{results['GigaChat-like (RuBERT-tiny2)']['cot_time']} сек",
|
91 |
+
results["GigaChat-like (RuBERT-tiny2)"]["simple_answer"], f"{results['GigaChat-like (RuBERT-tiny2)']['simple_time']} сек",
|
92 |
)
|
93 |
|
94 |
# Интерфейс Gradio
|
95 |
with gr.Blocks() as demo:
|
96 |
+
gr.Markdown("## 🤖 Классификация клиентских обращений — CoT vs обычный промпт")
|
97 |
+
inp = gr.Textbox(label="Обращение клиента", placeholder="Например: Я не могу войти в личный кабинет", lines=2)
|
98 |
+
btn = gr.Button("Классифицировать")
|
|
|
99 |
|
100 |
+
gr.Markdown("### ChatGPT-like (FRED-T5-small)")
|
101 |
+
cot1, cot1_time = gr.Textbox(label="CoT ответ"), gr.Textbox(label="Время CoT")
|
102 |
+
simple1, simple1_time = gr.Textbox(label="Обычный ответ"), gr.Textbox(label="Время обычного")
|
|
|
|
|
103 |
|
104 |
gr.Markdown("### DeepSeek-like (ruGPT3-small)")
|
105 |
+
cot2, cot2_time = gr.Textbox(label="CoT ответ"), gr.Textbox(label="Время CoT")
|
106 |
+
simple2, simple2_time = gr.Textbox(label="Обычный ответ"), gr.Textbox(label="Время обычного")
|
|
|
|
|
107 |
|
108 |
+
gr.Markdown("### GigaChat-like (RuBERT-tiny2)")
|
109 |
+
cot3, cot3_time = gr.Textbox(label="CoT ответ"), gr.Textbox(label="Время CoT")
|
110 |
+
simple3, simple3_time = gr.Textbox(label="Обычный ответ"), gr.Textbox(label="Время обычного")
|
|
|
|
|
111 |
|
112 |
btn.click(generate_dual_answers, inputs=[inp], outputs=[
|
113 |
cot1, cot1_time, simple1, simple1_time,
|
114 |
cot2, cot2_time, simple2, simple2_time,
|
115 |
+
cot3, cot3_time, simple3, simple3_time,
|
116 |
])
|
117 |
|
118 |
+
if __name__ == '__main__':
|
119 |
demo.launch()
|