Wills17 commited on
Commit
4e4a6be
·
verified ·
1 Parent(s): b480d52

Update FastAPI_app.py

Browse files
Files changed (1) hide show
  1. FastAPI_app.py +18 -6
FastAPI_app.py CHANGED
@@ -94,21 +94,33 @@ def generate_recipe_qwen(ingredient_names):
94
  ]
95
 
96
  # Use Qwen chat template
97
- input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
 
 
 
 
 
98
  inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
99
 
100
- outputs = model.generate(
101
  inputs.input_ids,
102
- max_new_tokens=450,
103
  temperature=0.7,
 
104
  top_p=0.9,
105
- do_sample=True
 
106
  )
107
 
108
- recipe_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
109
  # Strip the prompt part
110
- return recipe_text.split("<|assistant|>")[-1].strip()
 
111
 
 
 
 
 
 
112
 
113
 
114
  # Infer uploaded image function
 
94
  ]
95
 
96
  # Use Qwen chat template
97
+ input_text = tokenizer.apply_chat_template(
98
+ messages,
99
+ tokenize=False,
100
+ add_generation_prompt=True,
101
+ return_dict = True)
102
+
103
  inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
104
 
105
+ output = model.generate(
106
  inputs.input_ids,
107
+ max_new_tokens=500,
108
  temperature=0.7,
109
+ do_sample=True,
110
  top_p=0.9,
111
+ eos_token_id=tokenizer.eos_token_id,
112
+ pad_token_id=tokenizer.eos_token_id
113
  )
114
 
 
115
  # Strip the prompt part
116
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
117
+ recipe_text = response.split("assistant")[-1].strip()
118
 
119
+ # Final cleanup
120
+ if "<|" in recipe_text:
121
+ recipe_text = recipe_text.split("<|")[0].strip()
122
+
123
+ return recipe_text
124
 
125
 
126
  # Infer uploaded image function