File size: 5,909 Bytes
57645e8
66fabfa
f4f65e1
c43dd36
52af11a
8519047
22c5acd
34db0e6
607079d
 
 
 
 
 
 
34db0e6
 
ead8089
b97cde7
 
ead8089
 
 
b97cde7
ead8089
 
607079d
 
 
6b3ca0d
607079d
 
 
 
 
ead8089
c43dd36
607079d
 
 
ead8089
 
c43dd36
607079d
b97cde7
607079d
ead8089
22c5acd
34db0e6
ead8089
 
 
 
ff077e4
66fabfa
607079d
 
22c5acd
607079d
22c5acd
 
607079d
 
 
 
22c5acd
 
607079d
34db0e6
607079d
34db0e6
607079d
f5f461a
607079d
34db0e6
607079d
34db0e6
607079d
34db0e6
22c5acd
607079d
 
 
 
22c5acd
34db0e6
22c5acd
607079d
 
 
 
 
 
66fabfa
 
607079d
60a6dd5
324dafa
607079d
b97cde7
607079d
 
 
 
 
d143975
324dafa
607079d
 
 
 
d143975
324dafa
607079d
 
 
 
d143975
324dafa
22c5acd
 
 
 
 
 
34db0e6
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import gradio as gr
import time
from transformers import pipeline
from datasets import load_dataset

# Загружаем датасет
dataset = load_dataset("Romjiik/Russian_bank_reviews", split="train")

# Примеры для few-shot
few_shot_examples = []
for row in dataset.select(range(3)):
    review = row["review"]
    category = row["category"] if "category" in row else "(Категория)"
    ex = f"Клиент: {review}\nКлассификация: {category}"
    few_shot_examples.append(ex)

# Инструкции
cot_instruction = (
    "Ты — помощник банка. Клиент задал вопрос. Проанализируй обращение шаг за шагом, "
    "выдели ключевые признаки и выдай итоговую категорию обращения."
)

simple_instruction = (
    "Ты — помощник банка. Определи категорию обращения клиента. Ответ должен быть кратким, без лишнего текста."
)

# Используемые модели
models = {
    "ChatGPT-like (ruGPT3small)": pipeline("text-generation", model="ai-forever/rugpt3small_based_on_gpt2", tokenizer="ai-forever/rugpt3small_based_on_gpt2", device=-1),
    "GigaChat-like (ruDialoGPT-medium)": pipeline("text-generation", model="t-bank-ai/ruDialoGPT-medium", tokenizer="t-bank-ai/ruDialoGPT-medium", device=-1),
    "DeepSeek-like (RuBERT-tiny2)": pipeline("text-classification", model="cointegrated/rubert-tiny2", tokenizer="cointegrated/rubert-tiny2", device=-1)
}

# Формирование промптов

def build_cot_prompt(user_input):
    examples = "\n\n".join(few_shot_examples)
    return (
        f"{cot_instruction}\n\n{examples}\n\nКлиент: {user_input}\nРассуждение и классификация:"
    )

def build_simple_prompt(user_input):
    examples = "\n\n".join(few_shot_examples)
    return (
        f"{simple_instruction}\n\n{examples}\n\nКлиент: {user_input}\nКлассификация:"
    )

# Генерация ответов

def generate_dual_answers(user_input):
    results = {}
    prompt_cot = build_cot_prompt(user_input)
    prompt_simple = build_simple_prompt(user_input)

    for name, pipe in models.items():
        if name.startswith("DeepSeek"):
            # классификация
            start = time.time()
            output = pipe(user_input)[0]
            end = round(time.time() - start, 2)
            results[name] = {
                "cot_answer": output['label'],
                "cot_time": end,
                "simple_answer": output['label'],
                "simple_time": end
            }
        else:
            # генерация CoT
            start_cot = time.time()
            out_cot = pipe(prompt_cot, max_new_tokens=100, do_sample=True, top_p=0.9, temperature=0.7)[0]["generated_text"]
            end_cot = round(time.time() - start_cot, 2)
            answer_cot = out_cot.split("Классификация:")[-1].strip()

            # генерация Simple
            start_simple = time.time()
            out_simple = pipe(prompt_simple, max_new_tokens=60, do_sample=True, top_p=0.9, temperature=0.7)[0]["generated_text"]
            end_simple = round(time.time() - start_simple, 2)
            answer_simple = out_simple.split("Классификация:")[-1].strip()

            results[name] = {
                "cot_answer": answer_cot,
                "cot_time": end_cot,
                "simple_answer": answer_simple,
                "simple_time": end_simple
            }

    return (
        results["ChatGPT-like (ruGPT3small)"]["cot_answer"], f"{results['ChatGPT-like (ruGPT3small)']['cot_time']} сек",
        results["ChatGPT-like (ruGPT3small)"]["simple_answer"], f"{results['ChatGPT-like (ruGPT3small)']['simple_time']} сек",
        results["GigaChat-like (ruDialoGPT-medium)"]["cot_answer"], f"{results['GigaChat-like (ruDialoGPT-medium)']['cot_time']} сек",
        results["GigaChat-like (ruDialoGPT-medium)"]["simple_answer"], f"{results['GigaChat-like (ruDialoGPT-medium)']['simple_time']} сек",
        results["DeepSeek-like (RuBERT-tiny2)"]["cot_answer"], f"{results['DeepSeek-like (RuBERT-tiny2)']['cot_time']} сек",
        results["DeepSeek-like (RuBERT-tiny2)"]["simple_answer"], f"{results['DeepSeek-like (RuBERT-tiny2)']['simple_time']} сек"
    )

# Gradio интерфейс
with gr.Blocks() as demo:
    gr.Markdown("## 🛡️ Детектирование мошеннических обращений")

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

    gr.Markdown("### ChatGPT-like (ruGPT3small)")
    cot1 = gr.Textbox(label="CoT ответ")
    cot1_time = gr.Textbox(label="Время CoT")
    simple1 = gr.Textbox(label="Zero-shot ответ")
    simple1_time = gr.Textbox(label="Время Zero-shot")

    gr.Markdown("### GigaChat-like (ruDialoGPT-medium)")
    cot2 = gr.Textbox(label="CoT ответ")
    cot2_time = gr.Textbox(label="Время CoT")
    simple2 = gr.Textbox(label="Zero-shot ответ")
    simple2_time = gr.Textbox(label="Время Zero-shot")

    gr.Markdown("### DeepSeek-like (RuBERT-tiny2)")
    cot3 = gr.Textbox(label="CoT ответ")
    cot3_time = gr.Textbox(label="Время CoT")
    simple3 = gr.Textbox(label="Zero-shot ответ")
    simple3_time = gr.Textbox(label="Время Zero-shot")

    btn.click(generate_dual_answers, inputs=[inp], outputs=[
        cot1, cot1_time, simple1, simple1_time,
        cot2, cot2_time, simple2, simple2_time,
        cot3, cot3_time, simple3, simple3_time
    ])

demo.launch()