File size: 4,463 Bytes
b903e14
8329fd1
 
 
a37f4ce
 
8329fd1
 
 
a37f4ce
 
 
 
 
 
 
68d7edf
a37f4ce
8329fd1
a37f4ce
2c05295
8329fd1
 
a37f4ce
2c05295
a37f4ce
8329fd1
a37f4ce
 
 
 
 
 
 
 
 
 
 
 
68d7edf
 
 
 
a37f4ce
68d7edf
a37f4ce
 
 
 
 
 
 
 
68d7edf
 
 
a37f4ce
 
68d7edf
a37f4ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68d7edf
 
 
 
 
 
 
 
 
 
 
a37f4ce
 
68d7edf
a37f4ce
 
 
 
 
 
 
 
 
 
68d7edf
a37f4ce
8329fd1
a37f4ce
b903e14
68d7edf
a37f4ce
b903e14
68d7edf
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

APP_NAME = "EuroChef"

tokenizer = AutoTokenizer.from_pretrained("BenTouss/mdeberta-eurochef")
model = AutoModelForSequenceClassification.from_pretrained("BenTouss/mdeberta-eurochef")

device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)
model.eval()

def predict(text: str, threshold: float = 0.6, top_k: int = 8, only_above: bool = True):
    text = (text or "").strip()
    if not text:
        return "_Paste a message on the left to start._", []

    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256)
    inputs = {k: v.to(device) for k, v in inputs.items()}

    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.sigmoid(outputs.logits)[0].detach().cpu()

    items = []
    for idx, prob in enumerate(probs):
        score = float(prob)
        label = model.config.id2label[idx]
        if (not only_above) or (score >= threshold):
            items.append((label, score))

    items.sort(key=lambda x: x[1], reverse=True)
    items = items[: max(1, int(top_k))]

    rows = [[lbl, float(f"{sc:.3f}")] for lbl, sc in items]

    if rows:
        best_lbl, best_sc = rows[0][0], rows[0][1]
        summary = (
            f"**Top label:** `{best_lbl}` • **score:** `{best_sc}`  \n"
            f"**Results:** {len(rows)} • **threshold:** `{threshold:.2f}`"
        )
    else:
        summary = f"_No label (threshold `{threshold:.2f}`). Try lowering it._"

    return summary, rows


CSS = """
#title { margin-bottom: 0.25rem; }
#subtitle { margin-top: 0; opacity: 0.8; }
.footer { opacity: 0.7; font-size: 0.85rem; text-align: center; margin-top: 0.75rem; }

/* Force a nicer dataframe area without using height= */
#pred_table { min-height: 320px; }
"""

with gr.Blocks() as demo:
    gr.Markdown(f"# 🍳 {APP_NAME}", elem_id="title")
    gr.Markdown("Customer support message → labels + scores.", elem_id="subtitle")

    with gr.Row():
        with gr.Column(scale=6):
            text = gr.Textbox(
                label="Customer support message",
                placeholder="Ex: Bonjour, je n’arrive pas à lancer les vidéos…",
                lines=10,
            )

            with gr.Row():
                threshold = gr.Slider(0.0, 1.0, value=0.6, step=0.01, label="Threshold")
                top_k = gr.Slider(1, 20, value=8, step=1, label="Top-K")
                only_above = gr.Checkbox(value=True, label="Only ≥ threshold")

            with gr.Row():
                run = gr.Button("Analyze", variant="primary")
                clear = gr.ClearButton(value="Clear")

            gr.Examples(
                examples=[
                    # FR
                    "Bonjour,\nJe n’arrive pas à lancer les vidéos depuis hier soir : écran noir et chargement infini. "
                    "Je suis Premium (paiement OK) mais certaines recettes restent verrouillées. Pouvez-vous vérifier mon compte ?\nMerci !",
                    # EN
                    "Hi,\nSince yesterday evening I can't play any videos: the screen stays black and keeps buffering. "
                    "I'm a Premium subscriber (payment went through), but some recipes are still locked. "
                    "Could you please check my account?\nThanks!",
                    # DE
                    "Hallo,\nseit gestern Abend kann ich keine Videos mehr abspielen: Der Bildschirm bleibt schwarz und es lädt endlos. "
                    "Ich habe ein Premium-Abo (Zahlung ist erfolgt), aber einige Rezepte sind weiterhin gesperrt. "
                    "Können Sie bitte mein Konto überprüfen?\nVielen Dank!"
                ],
                inputs=[text],
                label="Examples (FR / EN / DE)",
            )

        with gr.Column(scale=6):
            summary = gr.Markdown(label="Summary")
            table = gr.Dataframe(
                headers=["label", "score"],
                datatype=["str", "number"],
                label="Predictions",
                wrap=True,
                interactive=False,
                elem_id="pred_table",
            )

    gr.Markdown(f"<div class='footer'>Made with ❤️ by Ben • {APP_NAME}</div>")

    run.click(fn=predict, inputs=[text, threshold, top_k, only_above], outputs=[summary, table])
    clear.add([text, summary, table])

demo.launch(theme=gr.themes.Soft(), css=CSS)