app.py
CHANGED
@@ -28,29 +28,32 @@ model = PeftModel.from_pretrained(
|
|
28 |
ignore_mismatched_sizes=True,
|
29 |
)
|
30 |
|
31 |
-
def
|
32 |
"""
|
33 |
-
|
34 |
"""
|
35 |
-
prompt = f"
|
36 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
37 |
with torch.no_grad():
|
38 |
outputs = model.generate(
|
39 |
**inputs,
|
40 |
-
max_new_tokens=
|
41 |
-
temperature=0.
|
42 |
-
top_p=0
|
|
|
43 |
)
|
44 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
45 |
return answer
|
46 |
|
47 |
with gr.Blocks() as demo:
|
48 |
-
gr.Markdown("## Qwen + LoRA Adapter: Food
|
49 |
-
input_box = gr.Textbox(lines=
|
50 |
-
output_box = gr.Textbox(lines=
|
51 |
|
52 |
-
|
53 |
-
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
demo.launch()
|
|
|
28 |
ignore_mismatched_sizes=True,
|
29 |
)
|
30 |
|
31 |
+
def extract_food_term(text):
|
32 |
"""
|
33 |
+
Extract or simplify a food term to a single word or best descriptor.
|
34 |
"""
|
35 |
+
prompt = f"Extract the best single word or term that describes this food item:\nInput: {text}\nOutput:"
|
36 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
37 |
with torch.no_grad():
|
38 |
outputs = model.generate(
|
39 |
**inputs,
|
40 |
+
max_new_tokens=16, # Limit output to a single word
|
41 |
+
temperature=0.0, # Deterministic output
|
42 |
+
top_p=1.0, # Focus on the most likely word
|
43 |
+
do_sample=False, # Disable sampling
|
44 |
)
|
45 |
answer = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
46 |
+
# Extract only the generated output
|
47 |
+
answer = answer.split("Output:")[1].strip()
|
48 |
return answer
|
49 |
|
50 |
with gr.Blocks() as demo:
|
51 |
+
gr.Markdown("## Qwen + LoRA Adapter: Food Term Extraction Demo")
|
52 |
+
input_box = gr.Textbox(lines=1, label="Enter a food item (e.g., 'blaubeertorte')")
|
53 |
+
output_box = gr.Textbox(lines=1, label="Best single-word descriptor")
|
54 |
|
55 |
+
extract_btn = gr.Button("Extract Term")
|
56 |
+
extract_btn.click(fn=extract_food_term, inputs=input_box, outputs=output_box)
|
57 |
|
58 |
if __name__ == "__main__":
|
59 |
demo.launch()
|