BhavaishKumar112 commited on
Commit
c8d823f
·
verified ·
1 Parent(s): d2ae30d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -9,25 +9,29 @@ model = AutoModelForCausalLM.from_pretrained("Ashikan/dut-recipe-generator")
9
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=get_device_id())
10
 
11
  def perform_model_inference(ingredients_list=None, recipe_name=None):
12
- if ingredients_list:
13
- for ingredient_index in range(len(ingredients_list)):
14
- ingredients_list[ingredient_index] = ingredients_list[ingredient_index].strip()
15
-
16
- # Create a simple prompt without JSON formatting
17
- input_text = "Generate a recipe with these ingredients: " + ", ".join(ingredients_list)
18
- elif recipe_name:
19
- # Simple prompt for recipe name
20
- input_text = "Generate ingredients and method for the recipe: " + recipe_name
21
- else:
22
- return "Invalid input"
23
-
24
- # Limit the length of the input text to avoid long processing times
25
- input_text = input_text[:512] # Truncate if it's too long
26
-
27
- # Use higher temperature for quicker responses
28
- output = pipe(input_text, max_length=512, temperature=0.7, do_sample=True, truncation=True)[0]["generated_text"]
29
-
30
- return format_output(output)
 
 
 
 
31
 
32
  def chat_function(history, user_input, mode):
33
  # If mode is "ingredients", process as ingredient list
 
9
  pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, device=get_device_id())
10
 
11
  def perform_model_inference(ingredients_list=None, recipe_name=None):
12
+ try:
13
+ if ingredients_list:
14
+ for ingredient_index in range(len(ingredients_list)):
15
+ ingredients_list[ingredient_index] = ingredients_list[ingredient_index].strip()
16
+
17
+ # Create a simple prompt for generating a recipe from ingredients
18
+ input_text = "Generate a recipe with these ingredients: " + ", ".join(ingredients_list)
19
+ elif recipe_name:
20
+ # Simple prompt for generating ingredients and method from recipe name
21
+ input_text = "Generate ingredients and cooking method for the recipe: " + recipe_name
22
+ else:
23
+ return "Invalid input"
24
+
25
+ # Limit the length of the input text to avoid long processing times
26
+ input_text = input_text[:512] # Truncate if it's too long
27
+
28
+ # Generate output with a higher temperature for quicker responses
29
+ output = pipe(input_text, max_length=512, temperature=0.7, do_sample=True, truncation=True)[0]["generated_text"]
30
+
31
+ return format_output(output)
32
+
33
+ except Exception as e:
34
+ return f"Error occurred: {str(e)}"
35
 
36
  def chat_function(history, user_input, mode):
37
  # If mode is "ingredients", process as ingredient list