Spaces:
Running
Running
import os | |
from dotenv import load_dotenv | |
import gradio as gr | |
from langchain_huggingface import HuggingFaceEndpoint | |
# Load environment variables | |
load_dotenv() | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
# Initialize the HuggingFace inference endpoint | |
llm = HuggingFaceEndpoint( | |
repo_id="mistralai/Mistral-7B-Instruct-v0.3", | |
huggingfacehub_api_token=HF_TOKEN.strip(), | |
temperature=0.7, | |
) | |
# Input validation function | |
def validate_ingredients(ingredients): | |
prompt = ( | |
f"Review the provided list of items: {ingredients}. " | |
f"Determine if all items are valid food ingredients. " | |
f"Respond only with 'Valid' if all are valid food items or 'Invalid' if any are not." | |
) | |
response = llm(prompt) | |
return response.strip() | |
# Recipe generation function | |
def generate_recipe(ingredients): | |
prompt = ( | |
f"You are an expert chef. Using the ingredients: {ingredients}, " | |
f"suggest two recipes. Provide a title, preparation time, and step-by-step instructions. " | |
f"It is not mandatory to include all ingredients, pick the ingredients required for each recipe. " | |
f"Do not include the ingredient list explicitly in the response." | |
) | |
response = llm(prompt) | |
return response.strip() | |
# Combined function for Gradio | |
def suggest_recipes(ingredients): | |
validation_result = validate_ingredients(ingredients) | |
if validation_result == "Valid": | |
return generate_recipe(ingredients) | |
else: | |
return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid ingredients for cooking!" | |
# Gradio interface | |
with gr.Blocks() as app: | |
gr.Markdown("# Recipe Suggestion App") | |
gr.Markdown("Provide the ingredients you have, and this app will validate them and suggest a recipe!") | |
with gr.Row(): | |
ingredients_input = gr.Textbox(label="Enter Ingredients (comma-separated):", placeholder="e.g., eggs, milk, flour") | |
recipe_output = gr.Textbox(label="Suggested Recipes or Validation Result:", lines=15, interactive=False) | |
generate_button = gr.Button("Get Recipes") | |
generate_button.click(suggest_recipes, inputs=ingredients_input, outputs=recipe_output) | |
# Launch the app | |
app.launch() | |