Spaces:
Runtime error
Runtime error
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, | |
) | |
# Recipe generation function | |
def suggest_recipes(ingredients): | |
# Create a prompt for the recipe generation | |
prompt = ( | |
f"You are an expert in cooking. If any of the following ingredients are invalid or unrecognizable, " | |
f"write a soft two-line apology for not being able to provide recipes using: {ingredients} and please do not suggest any recipe. " | |
f"Otherwise, please suggest 2 recipes using the valid {ingredients}. Provide a title for each recipe, " | |
f"include preparation time, and list step-by-step directions. Do not include the ingredients list in the response." | |
) | |
# Use the HuggingFaceEndpoint model to generate a response | |
response = llm(prompt) | |
return response | |
# Gradio interface | |
with gr.Blocks() as app: | |
gr.Markdown("# Recipe Suggestion App") | |
gr.Markdown("Provide the ingredients you have, and this app will suggest recipes along with preparation times!") | |
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:", 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() | |