Ajayadav commited on
Commit
95a7558
·
verified ·
1 Parent(s): ed8b19a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -28
app.py CHANGED
@@ -2,50 +2,58 @@ import gradio as gr
2
  import google.generativeai as genai
3
  import os
4
 
5
- # --- SECURITY: Load the key from the Vault ---
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. Please add GEMINI_API_KEY in 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
- # SYSTEM INSTRUCTION: The Prompt
18
- prompt = """
19
- You are a Chef. Analyze this fridge photo.
20
- 1. List ingredients.
21
- 2. Suggest 1 Indian recipe.
22
- 3. Include 🇮🇳 Flag and YouTube Search Query.
23
- """
24
-
25
- # --- SMART ENGINE: Try Newest -> Fallback to Oldest ---
 
 
 
 
 
 
 
 
26
  try:
27
- # Plan A: Try the fast Flash model
28
- model = genai.GenerativeModel('gemini-1.5-flash')
 
 
 
 
 
 
 
29
  response = model.generate_content([prompt, image])
30
- return response.text
31
- except Exception as e_flash:
32
- print(f"Flash failed: {e_flash}. Switching to backup...")
33
- try:
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
- # --- APP UI ---
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="Chef's Recipe")
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)