Spaces:
Sleeping
Sleeping
app.py
CHANGED
@@ -29,52 +29,67 @@ model = PeftModel.from_pretrained(
|
|
29 |
)
|
30 |
model.eval()
|
31 |
|
32 |
-
def
|
33 |
"""
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
Temperatur=0.0 und do_sample=False sorgen für deterministischen Output.
|
38 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
prompt = (
|
40 |
-
|
41 |
-
|
42 |
-
f"
|
|
|
|
|
43 |
)
|
44 |
|
45 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
46 |
with torch.no_grad():
|
47 |
output = model.generate(
|
48 |
**inputs,
|
49 |
-
max_new_tokens=
|
50 |
-
temperature=0.0, #
|
51 |
top_p=1.0,
|
52 |
do_sample=False
|
53 |
)
|
54 |
|
55 |
-
#
|
56 |
decoded = tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
57 |
|
58 |
-
#
|
59 |
lines = decoded.split("\n")
|
60 |
label = lines[-1].strip()
|
61 |
|
62 |
return label
|
63 |
|
64 |
-
# Gradio-Interface
|
65 |
with gr.Blocks() as demo:
|
66 |
produkt_box = gr.Textbox(
|
67 |
lines=2,
|
68 |
label="Produktbeschreibung",
|
69 |
-
placeholder="z.B. '
|
70 |
)
|
71 |
output_box = gr.Textbox(
|
72 |
lines=1,
|
73 |
-
label="
|
74 |
placeholder="Hier erscheint das Ergebnis"
|
75 |
)
|
76 |
|
77 |
-
classify_button = gr.Button("Kategorie bestimmen")
|
78 |
-
classify_button.click(
|
|
|
|
|
|
|
|
|
79 |
|
80 |
demo.launch()
|
|
|
29 |
)
|
30 |
model.eval()
|
31 |
|
32 |
+
def klassifiziere_lebensmittel_fewshot(produkt_text):
|
33 |
"""
|
34 |
+
Verwendet einen Few-Shot-Prompt mit Beispielen auf Deutsch,
|
35 |
+
um das Modell zu einer einzigen, kurzen Lebensmittel-Kategorie
|
36 |
+
ohne zusätzliche Erklärungen zu führen.
|
|
|
37 |
"""
|
38 |
+
|
39 |
+
# Beispiele (Few-Shot).
|
40 |
+
# Du kannst die Beispiele anpassen, wenn du andere demonstrieren willst.
|
41 |
+
beispiele = (
|
42 |
+
"1) Produkt: \"Cailler Branches Milch, 44 x 46 g\"\n Kategorie: Schokolade\n\n"
|
43 |
+
"2) Produkt: \"Aeschbach Trinkschokolade Milch, 1 kg\"\n Kategorie: Trinkschokolade\n\n"
|
44 |
+
"3) Produkt: \"Biedermann Bio Vollmilch 3,8%, pasteurisiert\"\n Kategorie: Milch\n\n"
|
45 |
+
)
|
46 |
+
|
47 |
+
# Prompt mit Few-Shot und neuer Eingabe
|
48 |
prompt = (
|
49 |
+
"Du bist ein Modell zur Klassifikation von Lebensmitteln in deutsche Kategorien.\n"
|
50 |
+
"Hier sind einige Beispiele:\n\n"
|
51 |
+
f"{beispiele}"
|
52 |
+
f"Neues Produkt: \"{produkt_text}\"\n"
|
53 |
+
"Kategorie (NUR das Wort und keine Erklärung):"
|
54 |
)
|
55 |
|
56 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
57 |
with torch.no_grad():
|
58 |
output = model.generate(
|
59 |
**inputs,
|
60 |
+
max_new_tokens=10, # Begrenze die Antwort auf wenige Tokens
|
61 |
+
temperature=0.0, # So wenig "kreatives" Rauschen wie möglich
|
62 |
top_p=1.0,
|
63 |
do_sample=False
|
64 |
)
|
65 |
|
66 |
+
# Modell-Antwort dekodieren
|
67 |
decoded = tokenizer.decode(output[0], skip_special_tokens=True).strip()
|
68 |
|
69 |
+
# Oft wiederholt das Modell das Prompt - wir nehmen daher nur die letzte Zeile
|
70 |
lines = decoded.split("\n")
|
71 |
label = lines[-1].strip()
|
72 |
|
73 |
return label
|
74 |
|
75 |
+
# Gradio-Interface
|
76 |
with gr.Blocks() as demo:
|
77 |
produkt_box = gr.Textbox(
|
78 |
lines=2,
|
79 |
label="Produktbeschreibung",
|
80 |
+
placeholder="z.B. 'Biedermann Bio Jogurt Schafmilch Himbeer, 5 x 120 g'"
|
81 |
)
|
82 |
output_box = gr.Textbox(
|
83 |
lines=1,
|
84 |
+
label="Predizierte Kategorie",
|
85 |
placeholder="Hier erscheint das Ergebnis"
|
86 |
)
|
87 |
|
88 |
+
classify_button = gr.Button("Kategorie bestimmen (Few-Shot)")
|
89 |
+
classify_button.click(
|
90 |
+
fn=klassifiziere_lebensmittel_fewshot,
|
91 |
+
inputs=produkt_box,
|
92 |
+
outputs=output_box
|
93 |
+
)
|
94 |
|
95 |
demo.launch()
|