Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
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 HuggingFace inference endpoint
|
11 |
+
llm = HuggingFaceEndpoint(
|
12 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
13 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
14 |
+
temperature=0.7,
|
15 |
+
)
|
16 |
+
|
17 |
+
# Input validation function
|
18 |
+
def validate_ingredients(ingredients):
|
19 |
+
prompt = (
|
20 |
+
f"Review the provided list of items: {ingredients}. "
|
21 |
+
f"Determine if all items are valid food ingredients. "
|
22 |
+
f"Respond only with 'Valid' if all are valid food items or 'Invalid' if any are not."
|
23 |
+
)
|
24 |
+
response = llm(prompt)
|
25 |
+
return response.strip()
|
26 |
+
|
27 |
+
# Recipe generation function
|
28 |
+
def generate_recipe(ingredients):
|
29 |
+
prompt = (
|
30 |
+
f"You are an expert chef. Using the ingredients: {ingredients}, "
|
31 |
+
f"suggest exactly one recipe. Provide a title, preparation time, and step-by-step instructions. "
|
32 |
+
f"Do not include the ingredient list explicitly in the response."
|
33 |
+
)
|
34 |
+
response = llm(prompt)
|
35 |
+
return response.strip()
|
36 |
+
|
37 |
+
# Combined function for Gradio
|
38 |
+
def suggest_recipes(ingredients):
|
39 |
+
validation_result = validate_ingredients(ingredients)
|
40 |
+
if validation_result == "Valid":
|
41 |
+
return generate_recipe(ingredients)
|
42 |
+
else:
|
43 |
+
return "I'm sorry, but I can't process this request due to invalid ingredients."
|
44 |
+
|
45 |
+
# Gradio interface
|
46 |
+
with gr.Blocks() as app:
|
47 |
+
gr.Markdown("# Recipe Suggestion App")
|
48 |
+
gr.Markdown("Provide the ingredients you have, and this app will validate them and suggest a recipe!")
|
49 |
+
|
50 |
+
with gr.Row():
|
51 |
+
ingredients_input = gr.Textbox(label="Enter Ingredients (comma-separated):", placeholder="e.g., eggs, milk, flour")
|
52 |
+
|
53 |
+
recipe_output = gr.Textbox(label="Suggested Recipes or Validation Result:", lines=15, interactive=False)
|
54 |
+
|
55 |
+
generate_button = gr.Button("Get Recipes")
|
56 |
+
|
57 |
+
generate_button.click(suggest_recipes, inputs=ingredients_input, outputs=recipe_output)
|
58 |
+
|
59 |
+
# Launch the app
|
60 |
+
app.launch()
|