Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,50 +2,58 @@ import gradio as gr
|
|
| 2 |
import google.generativeai as genai
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# ---
|
| 6 |
MY_API_KEY = os.environ.get("GEMINI_API_KEY")
|
| 7 |
|
| 8 |
def snapchef_logic(image):
|
| 9 |
if not MY_API_KEY:
|
| 10 |
-
return "⚠️ Error: API Key is missing.
|
| 11 |
|
| 12 |
if image is None:
|
| 13 |
return "⚠️ Please take a photo first!"
|
| 14 |
|
| 15 |
genai.configure(api_key=MY_API_KEY)
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
response = model.generate_content([prompt, image])
|
| 30 |
-
return response.text
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# Plan B: Try the reliable 'gemini-pro' (Works everywhere)
|
| 35 |
-
model = genai.GenerativeModel('gemini-pro-vision')
|
| 36 |
-
response = model.generate_content([prompt, image])
|
| 37 |
-
return f"✅ (Backup Mode)\n\n{response.text}"
|
| 38 |
-
except Exception as e_backup:
|
| 39 |
-
return f"❌ CRITICAL ERROR: {e_backup}\n\nCheck your API Key in Settings!"
|
| 40 |
|
| 41 |
-
# ---
|
| 42 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 43 |
-
gr.Markdown("# 🍳 SnapChef AI")
|
| 44 |
-
gr.Markdown("Created by Ajayadav • Powered by Gemini")
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
-
img_input = gr.Image(type="pil", label="Upload Photo")
|
| 48 |
-
recipe_output = gr.Markdown(label="
|
| 49 |
|
| 50 |
btn = gr.Button("Cook Something!", variant="primary")
|
| 51 |
btn.click(snapchef_logic, inputs=img_input, outputs=recipe_output)
|
|
|
|
| 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 |
def snapchef_logic(image):
|
| 9 |
if not MY_API_KEY:
|
| 10 |
+
return "⚠️ Error: API Key is missing. Check Settings > Secrets."
|
| 11 |
|
| 12 |
if image is None:
|
| 13 |
return "⚠️ Please take a photo first!"
|
| 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 |
+
for m in genai.list_models():
|
| 22 |
+
if 'generateContent' in m.supported_generation_methods:
|
| 23 |
+
if 'flash' in m.name:
|
| 24 |
+
target_model = m.name
|
| 25 |
+
break # Found the best one (Flash)
|
| 26 |
+
|
| 27 |
+
if not target_model:
|
| 28 |
+
target_model = "models/gemini-1.5-flash" # Default guess
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"❌ Connection Error: {e}"
|
| 32 |
+
|
| 33 |
+
# --- 3. THE GENERATION ---
|
| 34 |
try:
|
| 35 |
+
model = genai.GenerativeModel(target_model)
|
| 36 |
+
|
| 37 |
+
prompt = """
|
| 38 |
+
You are a Chef. Analyze this fridge photo.
|
| 39 |
+
1. List ingredients.
|
| 40 |
+
2. Suggest 1 Indian recipe.
|
| 41 |
+
3. Include 🇮🇳 Flag and YouTube Search Query.
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
response = model.generate_content([prompt, image])
|
| 45 |
+
return f"✅ Using Model: {target_model}\n\n{response.text}"
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"❌ Error with {target_model}: {e}\n\n(Try regenerating your API Key in Google AI Studio)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# --- 4. LAUNCH ---
|
| 51 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 52 |
+
gr.Markdown("# 🍳 SnapChef AI (Final)")
|
|
|
|
| 53 |
|
| 54 |
with gr.Row():
|
| 55 |
+
img_input = gr.Image(type="pil", label="Upload Food Photo")
|
| 56 |
+
recipe_output = gr.Markdown(label="Recipe")
|
| 57 |
|
| 58 |
btn = gr.Button("Cook Something!", variant="primary")
|
| 59 |
btn.click(snapchef_logic, inputs=img_input, outputs=recipe_output)
|