cheberle commited on
Commit
17b5cd4
·
1 Parent(s): d154bbf
Files changed (1) hide show
  1. app.py +14 -11
app.py CHANGED
@@ -28,29 +28,32 @@ model = PeftModel.from_pretrained(
28
  ignore_mismatched_sizes=True,
29
  )
30
 
31
- def classify_food(text):
32
  """
33
- Classify or extract food-related terms from the input text.
34
  """
35
- prompt = f"Below is some text. Please identify and classify food-related terms.\nText: {text}\nFood classification or extraction:"
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=64,
41
- temperature=0.7, # Adjust temperature for creativity
42
- top_p=0.9, # Adjust top_p for diversity
 
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 Classification/Extraction Demo")
49
- input_box = gr.Textbox(lines=3, label="Enter text containing food items")
50
- output_box = gr.Textbox(lines=3, label="Model's classification or extraction output")
51
 
52
- classify_btn = gr.Button("Analyze Food Terms")
53
- classify_btn.click(fn=classify_food, inputs=input_box, outputs=output_box)
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()