Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,53 +2,42 @@ import os
|
|
2 |
from dotenv import load_dotenv
|
3 |
import gradio as gr
|
4 |
from langchain_huggingface import HuggingFaceEndpoint
|
|
|
5 |
|
6 |
# Load environment variables
|
7 |
load_dotenv()
|
8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
9 |
|
10 |
-
# Initialize the
|
11 |
llm = HuggingFaceEndpoint(
|
12 |
-
repo_id="
|
13 |
huggingfacehub_api_token=HF_TOKEN.strip(),
|
14 |
temperature=0.7,
|
15 |
-
max_new_tokens=
|
16 |
)
|
17 |
|
18 |
# Recipe generation function
|
19 |
def suggest_recipes(ingredients):
|
|
|
20 |
prompt = (
|
21 |
-
f"You are an expert
|
22 |
f"ingredients: {ingredients}. Provide a title for each recipe, include "
|
23 |
f"preparation time, and list step-by-step directions."
|
24 |
)
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
generated_text = response.content
|
30 |
-
recipes = generated_text.split("Recipe")
|
31 |
-
structured_recipes = []
|
32 |
-
|
33 |
-
for i, recipe in enumerate(recipes):
|
34 |
-
if recipe.strip(): # Ensure non-empty recipe
|
35 |
-
structured_recipes.append(f"Recipe {i+1}:\n{recipe.strip()}")
|
36 |
-
|
37 |
-
return "\n\n".join(structured_recipes)
|
38 |
-
|
39 |
-
except Exception as e:
|
40 |
-
return f"Error: {e}"
|
41 |
|
42 |
# Gradio interface
|
43 |
with gr.Blocks() as app:
|
44 |
-
gr.Markdown("#
|
45 |
-
gr.Markdown("
|
46 |
|
47 |
with gr.Row():
|
48 |
-
ingredients_input = gr.Textbox(
|
49 |
-
label="Enter Ingredients (comma-separated):",
|
50 |
-
placeholder="e.g., eggs, milk, flour"
|
51 |
-
)
|
52 |
|
53 |
recipe_output = gr.Textbox(label="Suggested Recipes:", lines=15, interactive=False)
|
54 |
|
|
|
2 |
from dotenv import load_dotenv
|
3 |
import gradio as gr
|
4 |
from langchain_huggingface import HuggingFaceEndpoint
|
5 |
+
from langchain_core.messages import HumanMessage
|
6 |
|
7 |
# Load environment variables
|
8 |
load_dotenv()
|
9 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
10 |
|
11 |
+
# Initialize the HuggingFace inference endpoint
|
12 |
llm = HuggingFaceEndpoint(
|
13 |
+
repo_id="flax-community/t5-recipe-generation",
|
14 |
huggingfacehub_api_token=HF_TOKEN.strip(),
|
15 |
temperature=0.7,
|
16 |
+
max_new_tokens=500
|
17 |
)
|
18 |
|
19 |
# Recipe generation function
|
20 |
def suggest_recipes(ingredients):
|
21 |
+
# Create a prompt for the recipe generation
|
22 |
prompt = (
|
23 |
+
f"You are an expert in cooking. Please suggest 3 recipes using the following "
|
24 |
f"ingredients: {ingredients}. Provide a title for each recipe, include "
|
25 |
f"preparation time, and list step-by-step directions."
|
26 |
)
|
27 |
+
# Wrap the prompt in a HumanMessage object
|
28 |
+
input_message = HumanMessage(content=prompt)
|
29 |
|
30 |
+
# Use the HuggingFaceEndpoint model to generate a response
|
31 |
+
response = llm(input_message)
|
32 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
# Gradio interface
|
35 |
with gr.Blocks() as app:
|
36 |
+
gr.Markdown("# Recipe Suggestion App")
|
37 |
+
gr.Markdown("Provide the ingredients you have, and this app will suggest recipes along with preparation times!")
|
38 |
|
39 |
with gr.Row():
|
40 |
+
ingredients_input = gr.Textbox(label="Enter Ingredients (comma-separated):", placeholder="e.g., eggs, milk, flour")
|
|
|
|
|
|
|
41 |
|
42 |
recipe_output = gr.Textbox(label="Suggested Recipes:", lines=15, interactive=False)
|
43 |
|