Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,34 +4,34 @@ import os
|
|
| 4 |
from gtts import gTTS
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
-
# --- 1.
|
| 8 |
-
|
|
|
|
| 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 "β οΈ
|
| 15 |
if image is None:
|
| 16 |
-
return "β οΈ Please upload a photo
|
| 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}
|
| 30 |
|
| 31 |
TASK:
|
| 32 |
-
1. Identify
|
| 33 |
-
2. Create a unique recipe
|
| 34 |
-
3. If 'Hindi' is selected, write
|
| 35 |
|
| 36 |
FORMAT (Markdown):
|
| 37 |
# π½οΈ [Dish Name]
|
|
@@ -44,11 +44,11 @@ def snapchef_logic(image, diet_type, spice_level, language):
|
|
| 44 |
1. [Step 1]
|
| 45 |
2. [Step 2]
|
| 46 |
|
| 47 |
-
> π‘ **Chef's Secret Tip:** [
|
| 48 |
|
| 49 |
---
|
| 50 |
VOICE_SUMMARY:
|
| 51 |
-
(Write a short, exciting 2-sentence summary
|
| 52 |
"""
|
| 53 |
|
| 54 |
# C. Call the AI
|
|
@@ -56,19 +56,18 @@ def snapchef_logic(image, diet_type, spice_level, language):
|
|
| 56 |
response = model.generate_content([prompt, image])
|
| 57 |
full_text = response.text
|
| 58 |
|
| 59 |
-
# D. Split Text & Audio
|
| 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 = "
|
| 67 |
|
| 68 |
-
# E. Generate Audio (
|
| 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
|
| 72 |
|
| 73 |
tts = gTTS(voice_script, lang=lang_code, tld=tld_code)
|
| 74 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
@@ -79,56 +78,35 @@ def snapchef_logic(image, diet_type, spice_level, language):
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"β Error: {str(e)}", None
|
| 81 |
|
| 82 |
-
# --- 3. THE
|
| 83 |
-
# Custom CSS for the "Black & Gold" Premium Look
|
| 84 |
custom_css = """
|
| 85 |
body { background-color: #0F0F0F; }
|
| 86 |
-
|
| 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"
|
| 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 |
-
|
| 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"
|
| 109 |
-
spice_dropdown = gr.Dropdown(["
|
| 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 |
-
|
| 118 |
-
|
| 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()
|
|
|
|
| 4 |
from gtts import gTTS
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
+
# --- 1. CONFIGURATION ---
|
| 8 |
+
# π PASTE YOUR API KEY DIRECTLY BELOW (Inside the quotes)
|
| 9 |
+
MY_API_KEY = "PASTE_YOUR_AIZASY_KEY_HERE"
|
| 10 |
|
| 11 |
# --- 2. ADVANCED AI LOGIC ---
|
| 12 |
def snapchef_logic(image, diet_type, spice_level, language):
|
| 13 |
# A. Safety Checks
|
| 14 |
+
if not MY_API_KEY or "PASTE_YOUR" in MY_API_KEY:
|
| 15 |
+
return "β οΈ Error: You forgot to paste your API Key in line 8 of the code!", None
|
| 16 |
if image is None:
|
| 17 |
+
return "β οΈ Please upload a photo first!", None
|
| 18 |
|
| 19 |
genai.configure(api_key=MY_API_KEY)
|
| 20 |
|
| 21 |
try:
|
| 22 |
# B. The "Smart" Prompt
|
|
|
|
| 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}
|
| 30 |
|
| 31 |
TASK:
|
| 32 |
+
1. Identify ingredients.
|
| 33 |
+
2. Create a unique recipe matching preferences.
|
| 34 |
+
3. If 'Hindi' is selected, write recipe in Hindi script.
|
| 35 |
|
| 36 |
FORMAT (Markdown):
|
| 37 |
# π½οΈ [Dish Name]
|
|
|
|
| 44 |
1. [Step 1]
|
| 45 |
2. [Step 2]
|
| 46 |
|
| 47 |
+
> π‘ **Chef's Secret Tip:** [Tip]
|
| 48 |
|
| 49 |
---
|
| 50 |
VOICE_SUMMARY:
|
| 51 |
+
(Write a short, exciting 2-sentence summary for the audio guide.)
|
| 52 |
"""
|
| 53 |
|
| 54 |
# C. Call the AI
|
|
|
|
| 56 |
response = model.generate_content([prompt, image])
|
| 57 |
full_text = response.text
|
| 58 |
|
| 59 |
+
# D. Split Text & Audio
|
| 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 = "Enjoy your meal!"
|
| 67 |
|
| 68 |
+
# E. Generate Audio (Accent Control)
|
|
|
|
| 69 |
lang_code = 'hi' if language == "Hindi" else 'en'
|
| 70 |
+
tld_code = 'co.in' # Indian Accent
|
| 71 |
|
| 72 |
tts = gTTS(voice_script, lang=lang_code, tld=tld_code)
|
| 73 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
|
|
|
| 78 |
except Exception as e:
|
| 79 |
return f"β Error: {str(e)}", None
|
| 80 |
|
| 81 |
+
# --- 3. THE PREMIUM INTERFACE ---
|
|
|
|
| 82 |
custom_css = """
|
| 83 |
body { background-color: #0F0F0F; }
|
| 84 |
+
h1 { color: #FFD700; text-align: center; font-family: sans-serif; }
|
|
|
|
|
|
|
|
|
|
| 85 |
"""
|
| 86 |
|
| 87 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange"), css=custom_css) as app:
|
| 88 |
|
|
|
|
| 89 |
gr.Markdown("# π¨βπ³ SnapChef Ultimate")
|
|
|
|
| 90 |
|
| 91 |
with gr.Row():
|
| 92 |
+
with gr.Column(scale=1):
|
| 93 |
+
img_input = gr.Image(type="pil", label="πΈ Ingredients", height=300)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
+
diet_dropdown = gr.Dropdown(["Vegetarian", "Non-Vegetarian"], label="π₯ Diet", value="Vegetarian")
|
| 97 |
+
spice_dropdown = gr.Dropdown(["Medium", "Spicy πΆοΈ"], label="π₯ Spice", value="Medium")
|
| 98 |
|
| 99 |
lang_dropdown = gr.Dropdown(["English", "Hindi"], label="π£οΈ Language", value="English")
|
|
|
|
| 100 |
submit_btn = gr.Button("π¨βπ³ COOK NOW", variant="primary", size="lg")
|
| 101 |
|
|
|
|
| 102 |
with gr.Column(scale=2):
|
| 103 |
+
audio_output = gr.Audio(label="π§ Chef's Voice", type="filepath")
|
| 104 |
+
recipe_output = gr.Markdown(label="π½οΈ Recipe")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
|
|
|
| 106 |
submit_btn.click(
|
| 107 |
snapchef_logic,
|
| 108 |
inputs=[img_input, diet_dropdown, spice_dropdown, lang_dropdown],
|
| 109 |
outputs=[recipe_output, audio_output]
|
| 110 |
)
|
| 111 |
|
| 112 |
+
app.launch()
|