Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|