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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
| 5 |
+
from datetime import datetime
|
| 6 |
+
|
| 7 |
+
# Load environment variables
|
| 8 |
+
load_dotenv()
|
| 9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 10 |
+
|
| 11 |
+
# Initialize the HuggingFace inference endpoint
|
| 12 |
+
llm = HuggingFaceEndpoint(
|
| 13 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 14 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
| 15 |
+
temperature=0.7,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Input validation function
|
| 19 |
+
def validate_ingredients(ingredients):
|
| 20 |
+
prompt = (
|
| 21 |
+
f"Review the provided list of items: {ingredients}. "
|
| 22 |
+
f"Determine if all items are valid food ingredients. "
|
| 23 |
+
f"Respond only with 'Valid' if all are valid food items or 'Invalid' if any are not."
|
| 24 |
+
)
|
| 25 |
+
response = llm(prompt)
|
| 26 |
+
return response.strip()
|
| 27 |
+
|
| 28 |
+
# Recipe generation function
|
| 29 |
+
def generate_recipe(ingredients):
|
| 30 |
+
prompt = (
|
| 31 |
+
f"You are an expert chef. Using the ingredients: {ingredients}, "
|
| 32 |
+
f"suggest two recipes. Provide a title, preparation time, and step-by-step instructions. "
|
| 33 |
+
f"It is not mandatory to include all ingredients, pick the ingredients required for each recipe. "
|
| 34 |
+
f"Do not include the ingredient list explicitly in the response."
|
| 35 |
+
)
|
| 36 |
+
response = llm(prompt)
|
| 37 |
+
return response.strip()
|
| 38 |
+
|
| 39 |
+
# Combined function for Gradio
|
| 40 |
+
def suggest_recipes(ingredients):
|
| 41 |
+
validation_result = validate_ingredients(ingredients)
|
| 42 |
+
if validation_result == "Valid":
|
| 43 |
+
return generate_recipe(ingredients)
|
| 44 |
+
else:
|
| 45 |
+
return "I'm sorry, but I can't process this request due to invalid ingredients. Please provide valid ingredients for cooking!"
|
| 46 |
+
|
| 47 |
+
# Feedback function
|
| 48 |
+
def save_feedback(feedback):
|
| 49 |
+
if feedback.strip():
|
| 50 |
+
with open("feedback.txt", "a") as file:
|
| 51 |
+
file.write(f"{datetime.now()}: {feedback}\n")
|
| 52 |
+
return "Thank you for your feedback!"
|
| 53 |
+
return "Please enter feedback before submitting."
|
| 54 |
+
|
| 55 |
+
# Gradio interface with professional color theme
|
| 56 |
+
with gr.Blocks(theme=gr.themes.Monochrome(primary_hue="blue")) as app:
|
| 57 |
+
# Header section with professional background and white text
|
| 58 |
+
with gr.Row():
|
| 59 |
+
gr.HTML(
|
| 60 |
+
"""
|
| 61 |
+
<div style="text-align: center; margin: auto;">
|
| 62 |
+
<h1 style="color: #1155ff; font-size: 3.5em; font-weight: bold; font-family: 'Arial', sans-serif; margin-bottom: 0.5em;">🍳 Recipe Generator !</h1>
|
| 63 |
+
<p style="font-size: 1.5em; color: #555555; font-weight: lighter; font-family: 'Verdana', sans-serif;">
|
| 64 |
+
Enter the ingredients you have, and we'll validate them and suggest delightful recipes!
|
| 65 |
+
</p>
|
| 66 |
+
</div>
|
| 67 |
+
"""
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Banner Image
|
| 71 |
+
with gr.Row():
|
| 72 |
+
gr.Image("./recipe-generator-banner.png", show_label=False, elem_id="banner-image", width="100%")
|
| 73 |
+
|
| 74 |
+
# Ingredient input and output with borders and padding for more contrast
|
| 75 |
+
with gr.Row():
|
| 76 |
+
with gr.Column():
|
| 77 |
+
ingredients_input = gr.Textbox(
|
| 78 |
+
label="Enter Ingredients (comma-separated):",
|
| 79 |
+
placeholder="e.g., eggs, milk, flour",
|
| 80 |
+
elem_id="input-box",
|
| 81 |
+
show_label=True,
|
| 82 |
+
interactive=True,
|
| 83 |
+
lines=2,
|
| 84 |
+
max_lines=5
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
with gr.Column():
|
| 88 |
+
recipe_output = gr.Textbox(
|
| 89 |
+
label="Suggested Recipes or Validation Result:",
|
| 90 |
+
lines=15,
|
| 91 |
+
interactive=False,
|
| 92 |
+
elem_id="output-box"
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
# Buttons with professional color design and hover effect
|
| 96 |
+
with gr.Row():
|
| 97 |
+
generate_button = gr.Button("Get Recipes", elem_id="generate-btn", size="lg")
|
| 98 |
+
reset_button = gr.Button("Reset", elem_id="reset-btn", size="lg")
|
| 99 |
+
|
| 100 |
+
generate_button.click(suggest_recipes, inputs=ingredients_input, outputs=recipe_output)
|
| 101 |
+
reset_button.click(lambda: "", inputs=None, outputs=ingredients_input)
|
| 102 |
+
|
| 103 |
+
# Feedback Section with professional styled input fields
|
| 104 |
+
with gr.Row():
|
| 105 |
+
feedback_input = gr.Textbox(
|
| 106 |
+
label="Feedback:",
|
| 107 |
+
placeholder="Let us know how we can improve!",
|
| 108 |
+
elem_id="feedback-input",
|
| 109 |
+
lines=1
|
| 110 |
+
)
|
| 111 |
+
feedback_button = gr.Button("Submit", elem_id="feedback-btn", size="lg")
|
| 112 |
+
feedback_output = gr.Textbox(
|
| 113 |
+
label="Feedback Response:",
|
| 114 |
+
lines=1,
|
| 115 |
+
interactive=False,
|
| 116 |
+
elem_id="feedback-output"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
feedback_button.click(save_feedback, inputs=feedback_input, outputs=feedback_output)
|
| 120 |
+
|
| 121 |
+
# Footer with contact link
|
| 122 |
+
with gr.Row():
|
| 123 |
+
gr.Markdown(
|
| 124 |
+
"""
|
| 125 |
+
<div style="text-align: center; margin-top: 2em;">
|
| 126 |
+
<p style="font-size: 1em; color:#333333; font-family: 'Arial', sans-serif;">
|
| 127 |
+
Developed by CloudYuga! Your feedback is valuable. Have questions?
|
| 128 |
+
<a href="mailto:connect@cloudyuga.guru" style="color: #0000FF; font-weight: bold; text-decoration: underline;">Contact us!</a>
|
| 129 |
+
</p>
|
| 130 |
+
</div>
|
| 131 |
+
"""
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# Apply custom professional theme styling
|
| 135 |
+
app.css = """
|
| 136 |
+
body {
|
| 137 |
+
background-color: #f4f4f9; /* Soft background */
|
| 138 |
+
color: #333333;
|
| 139 |
+
font-family: 'Arial', sans-serif;
|
| 140 |
+
}
|
| 141 |
+
#input-box, #output-box, #feedback-input {
|
| 142 |
+
background-color: #ffffff;
|
| 143 |
+
border: 2px solid #cccccc;
|
| 144 |
+
border-radius: 8px;
|
| 145 |
+
padding: 12px;
|
| 146 |
+
color: #333333;
|
| 147 |
+
font-size: 1.1em;
|
| 148 |
+
}
|
| 149 |
+
#generate-btn, #reset-btn, #feedback-btn {
|
| 150 |
+
border-radius: 8px;
|
| 151 |
+
padding: 15px 30px;
|
| 152 |
+
font-size: 1.2em;
|
| 153 |
+
transition: background-color 0.3s ease;
|
| 154 |
+
}
|
| 155 |
+
#generate-btn {
|
| 156 |
+
background-color: #4caf50; /* Green for recipe button */
|
| 157 |
+
color: #fff;
|
| 158 |
+
}
|
| 159 |
+
#generate-btn:hover {
|
| 160 |
+
background-color: #45a049;
|
| 161 |
+
}
|
| 162 |
+
#reset-btn {
|
| 163 |
+
background-color: #ff9800; /* Orange for reset button */
|
| 164 |
+
color: #fff;
|
| 165 |
+
}
|
| 166 |
+
#reset-btn:hover {
|
| 167 |
+
background-color: #fb8c00;
|
| 168 |
+
}
|
| 169 |
+
#feedback-btn {
|
| 170 |
+
background-color: #2196f3; /* Blue for feedback button */
|
| 171 |
+
color: #fff;
|
| 172 |
+
}
|
| 173 |
+
#feedback-btn:hover {
|
| 174 |
+
background-color: #1e88e5;
|
| 175 |
+
}
|
| 176 |
+
#banner-image {
|
| 177 |
+
width: 100%;
|
| 178 |
+
height: auto;
|
| 179 |
+
border-radius: 10px;
|
| 180 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 181 |
+
}
|
| 182 |
+
"""
|
| 183 |
+
|
| 184 |
+
# Launch the app
|
| 185 |
+
app.launch()
|