Spaces:
Runtime error
Runtime error
Update app-mistral.py
Browse files- app-mistral.py +60 -0
app-mistral.py
CHANGED
|
@@ -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 Hugging Face endpoint
|
| 11 |
+
llm = HuggingFaceEndpoint(
|
| 12 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3", # Replace with the desired Hugging Face model
|
| 13 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
| 14 |
+
temperature=0.7,
|
| 15 |
+
max_new_tokens=300
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Recipe generation function
|
| 19 |
+
def suggest_recipes(ingredients):
|
| 20 |
+
prompt = (
|
| 21 |
+
f"You are an expert chef. Please suggest 3 unique recipes using the following "
|
| 22 |
+
f"ingredients: {ingredients}. Provide a title for each recipe, include "
|
| 23 |
+
f"preparation time, and list step-by-step directions."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = llm(prompt)
|
| 28 |
+
# Format response into multiple recipes
|
| 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("# AI Recipe Generator")
|
| 45 |
+
gr.Markdown("Enter the ingredients you have, and this app will generate 3 unique recipes along with preparation times!")
|
| 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 |
+
|
| 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()
|