File size: 2,027 Bytes
54d0b91
 
 
 
 
 
 
 
 
5ea9762
54d0b91
83ba8ab
54d0b91
 
 
 
 
 
5ea9762
54d0b91
c508f50
1d6323b
 
 
 
 
 
2450cde
 
11af300
54d0b91
5ea9762
97f528e
84c05ae
54d0b91
 
 
5ea9762
 
54d0b91
 
5ea9762
54d0b91
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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 chef. Carefully review the provided ingredients: {ingredients}. "
        f"If all ingredients are valid and kitchen-friendly, suggest exactly one recipe using them."
        f"Provide a title for each recipe, preparation time, and detailed step-by-step instructions." 
        f"Do not include the ingredients list explicitly in the response. "
        f"If the ingredients are invalid that are not used to make a food. for example, non-food items, random words, questions, or incomplete sentences,"
        f"Respond only with: 'I'm sorry, but I can't process this request due to invalid ingredients."
        f"It is strictly recommended, do not provide any recipes, alternative suggestions, or further explanations in such cases."
    )


    
    # 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()