Ajayadav commited on
Commit
d3f907f
Β·
verified Β·
1 Parent(s): 1567dfc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -48
app.py CHANGED
@@ -4,34 +4,34 @@ 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]
@@ -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:** [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
@@ -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 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")
@@ -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 "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()
 
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()