Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,61 +1,134 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# --- 1. SETUP ---
|
| 6 |
MY_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
if not MY_API_KEY:
|
| 10 |
-
return "⚠️ Error: API Key
|
| 11 |
-
|
| 12 |
if image is None:
|
| 13 |
-
return "⚠️ Please
|
| 14 |
|
| 15 |
genai.configure(api_key=MY_API_KEY)
|
| 16 |
|
| 17 |
-
# --- 2. THE DIAGNOSTIC CHECK ---
|
| 18 |
-
# This automatically finds the best model your key has access to.
|
| 19 |
-
target_model = ""
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
model = genai.GenerativeModel(target_model)
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
"""
|
| 43 |
|
|
|
|
|
|
|
| 44 |
response = model.generate_content([prompt, image])
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
except Exception as e:
|
| 48 |
-
return f"❌ Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 52 |
-
gr.Markdown("# 🍳 SnapChef AI (Final)")
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
app.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import tempfile
|
| 6 |
|
| 7 |
+
# --- 1. SETUP & SECURITY ---
|
| 8 |
MY_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 9 |
|
| 10 |
+
# --- 2. ADVANCED AI LOGIC ---
|
| 11 |
+
def snapchef_logic(image, diet_type, spice_level, language):
|
| 12 |
+
# A. Safety Checks
|
| 13 |
if not MY_API_KEY:
|
| 14 |
+
return "⚠️ System Error: API Key missing. Please check Settings.", None
|
|
|
|
| 15 |
if image is None:
|
| 16 |
+
return "⚠️ Please upload a photo of your ingredients first!", None
|
| 17 |
|
| 18 |
genai.configure(api_key=MY_API_KEY)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
+
# B. The "Smart" Prompt
|
| 22 |
+
# We inject the user's choices (Veg/Non-Veg, Spice, Language) directly into the brain
|
| 23 |
+
prompt = f"""
|
| 24 |
+
You are a Michelin Star Chef. Analyze this fridge photo.
|
| 25 |
+
|
| 26 |
+
USER PREFERENCES:
|
| 27 |
+
- Diet: {diet_type}
|
| 28 |
+
- Spice Level: {spice_level}
|
| 29 |
+
- Language: {language} (Output the recipe in this language)
|
| 30 |
+
|
| 31 |
+
TASK:
|
| 32 |
+
1. Identify the ingredients.
|
| 33 |
+
2. Create a unique recipe that matches the preferences above.
|
| 34 |
+
3. If 'Hindi' is selected, write the recipe in Hindi script (Devanagari).
|
|
|
|
| 35 |
|
| 36 |
+
FORMAT (Markdown):
|
| 37 |
+
# 🍽️ [Dish Name]
|
| 38 |
+
**🌍 Origin:** [Region] | **🔥 Spice:** {spice_level}
|
| 39 |
+
|
| 40 |
+
## 🛒 Ingredients
|
| 41 |
+
* [List]
|
| 42 |
+
|
| 43 |
+
## 👩🍳 Instructions
|
| 44 |
+
1. [Step 1]
|
| 45 |
+
2. [Step 2]
|
| 46 |
+
|
| 47 |
+
> 💡 **Chef's Secret Tip:** [A pro tip]
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
VOICE_SUMMARY:
|
| 51 |
+
(Write a short, exciting 2-sentence summary of this dish for the audio guide. Do not use markdown here.)
|
| 52 |
"""
|
| 53 |
|
| 54 |
+
# C. Call the AI
|
| 55 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 56 |
response = model.generate_content([prompt, image])
|
| 57 |
+
full_text = response.text
|
| 58 |
+
|
| 59 |
+
# D. Split Text & Audio Script
|
| 60 |
+
if "VOICE_SUMMARY:" in full_text:
|
| 61 |
+
parts = full_text.split("VOICE_SUMMARY:")
|
| 62 |
+
recipe_text = parts[0].strip()
|
| 63 |
+
voice_script = parts[1].strip()
|
| 64 |
+
else:
|
| 65 |
+
recipe_text = full_text
|
| 66 |
+
voice_script = "Your delicious recipe is ready. Enjoy cooking!"
|
| 67 |
+
|
| 68 |
+
# E. Generate Audio (with correct language accent)
|
| 69 |
+
# If Hindi, we use 'hi' code; if English, we use Indian English 'co.in'
|
| 70 |
+
lang_code = 'hi' if language == "Hindi" else 'en'
|
| 71 |
+
tld_code = 'co.in' # Indian accent for English
|
| 72 |
+
|
| 73 |
+
tts = gTTS(voice_script, lang=lang_code, tld=tld_code)
|
| 74 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
| 75 |
+
tts.save(temp_file.name)
|
| 76 |
+
|
| 77 |
+
return recipe_text, temp_file.name
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
+
return f"❌ Error: {str(e)}", None
|
| 81 |
+
|
| 82 |
+
# --- 3. THE "PRO" INTERFACE ---
|
| 83 |
+
# Custom CSS for the "Black & Gold" Premium Look
|
| 84 |
+
custom_css = """
|
| 85 |
+
body { background-color: #0F0F0F; }
|
| 86 |
+
.gradio-container { font-family: 'Helvetica', sans-serif; }
|
| 87 |
+
h1 { color: #FFD700; text-align: center; font-size: 2.5em; text-transform: uppercase; letter-spacing: 2px; }
|
| 88 |
+
.input-box { border: 1px solid #333; }
|
| 89 |
+
button.primary { background: linear-gradient(45deg, #FFD700, #FF8C00); color: black; font-weight: bold; border: none; }
|
| 90 |
+
"""
|
| 91 |
|
| 92 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="slate"), css=custom_css) as app:
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# --- HEADER ---
|
| 95 |
+
gr.Markdown("# 👨🍳 SnapChef Ultimate")
|
| 96 |
+
gr.Markdown("<center><i>The AI Personal Chef that Speaks Your Language</i></center>")
|
| 97 |
|
| 98 |
+
with gr.Row():
|
| 99 |
+
|
| 100 |
+
# --- LEFT COLUMN (CONTROLS) ---
|
| 101 |
+
with gr.Column(scale=1, variant="panel"):
|
| 102 |
+
gr.Markdown("### ⚙️ Your Preferences")
|
| 103 |
+
|
| 104 |
+
# The Advanced Inputs
|
| 105 |
+
img_input = gr.Image(type="pil", label="📸 Ingredients Photo", height=300)
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
diet_dropdown = gr.Dropdown(["Vegetarian", "Non-Vegetarian", "Vegan"], label="🥗 Diet", value="Vegetarian")
|
| 109 |
+
spice_dropdown = gr.Dropdown(["Low", "Medium", "High", "Spicy 🌶️"], label="🔥 Spice", value="Medium")
|
| 110 |
+
|
| 111 |
+
lang_dropdown = gr.Dropdown(["English", "Hindi"], label="🗣️ Language", value="English")
|
| 112 |
+
|
| 113 |
+
submit_btn = gr.Button("👨🍳 COOK NOW", variant="primary", size="lg")
|
| 114 |
+
|
| 115 |
+
# --- RIGHT COLUMN (RESULTS) ---
|
| 116 |
+
with gr.Column(scale=2):
|
| 117 |
+
# Audio Player at the top
|
| 118 |
+
audio_output = gr.Audio(label="🎧 Chef's Voice Note", type="filepath")
|
| 119 |
+
|
| 120 |
+
# Recipe Card
|
| 121 |
+
recipe_output = gr.Markdown(label="🍽️ Your Recipe")
|
| 122 |
+
|
| 123 |
+
# --- FOOTER ---
|
| 124 |
+
gr.Markdown("---")
|
| 125 |
+
gr.Markdown("<center>SnapChef Ultimate © 2026 • Powered by Google Gemini & gTTS</center>")
|
| 126 |
+
|
| 127 |
+
# --- LOGIC CONNECTION ---
|
| 128 |
+
submit_btn.click(
|
| 129 |
+
snapchef_logic,
|
| 130 |
+
inputs=[img_input, diet_dropdown, spice_dropdown, lang_dropdown],
|
| 131 |
+
outputs=[recipe_output, audio_output]
|
| 132 |
+
)
|
| 133 |
|
| 134 |
app.launch()
|