Spaces:
Sleeping
Sleeping
Download app.py from Ajayadav/SnapChef-AI: direct link, hf CLI and curl.
- Browser
- Download file 2.88 kB
-
https://huggingface.co/spaces/Ajayadav/SnapChef-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/Ajayadav/SnapChef-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Ajayadav/SnapChef-AI/resolve/main/app.py
2.88 kB
| import gradio as gr | |
| import google.generativeai as genai | |
| import os | |
| from gtts import gTTS | |
| import tempfile | |
| # --- 1. SETUP: Read from Vault --- | |
| MY_API_KEY = os.environ.get("GEMINI_API_KEY") | |
| # --- 2. ADVANCED AI LOGIC --- | |
| def snapchef_logic(image, diet_type, spice_level, language): | |
| # A. Safety Checks (CRITICAL) | |
| if not MY_API_KEY: | |
| return "β οΈ Error: API Key is missing. Add GEMINI_API_KEY in Settings.", None | |
| # THIS IS THE FIX: Check if photo exists before calling Google | |
| if image is None: | |
| return "β οΈ Please upload a photo first! The AI cannot cook 'nothing'.", None | |
| genai.configure(api_key=MY_API_KEY) | |
| try: | |
| # B. Prompt | |
| prompt = f""" | |
| You are a Michelin Star Chef. Analyze this fridge photo. | |
| PREFERENCES: Diet={diet_type}, Spice={spice_level}, Language={language} | |
| Task: Create a recipe. | |
| Structure: | |
| # π½οΈ [Name] | |
| **π Origin:** [Region] | |
| ## π Ingredients | |
| ## π©βπ³ Steps | |
| --- | |
| VOICE_SUMMARY: (Short summary) | |
| """ | |
| model = genai.GenerativeModel('gemini-1.5-flash') | |
| response = model.generate_content([prompt, image]) | |
| full_text = response.text | |
| # C. Split Text/Audio | |
| if "VOICE_SUMMARY:" in full_text: | |
| parts = full_text.split("VOICE_SUMMARY:") | |
| recipe = parts[0].strip() | |
| voice = parts[1].strip() | |
| else: | |
| recipe = full_text | |
| voice = "Recipe ready!" | |
| # D. Audio Gen | |
| lang = 'hi' if language == "Hindi" else 'en' | |
| tts = gTTS(voice, lang=lang, tld='co.in') | |
| temp = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") | |
| tts.save(temp.name) | |
| return recipe, temp.name | |
| except Exception as e: | |
| return f"β API Error: {str(e)}", None | |
| # --- 3. UI --- | |
| custom_css = """ | |
| body { background-color: #0F0F0F; } | |
| h1 { color: #FFD700; text-align: center; font-family: sans-serif; } | |
| """ | |
| with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange"), css=custom_css) as app: | |
| gr.Markdown("# π¨βπ³ SnapChef Ultimate") | |
| with gr.Row(): | |
| with gr.Column(): | |
| img = gr.Image(type="pil", label="Ingredients", height=300) | |
| with gr.Row(): | |
| diet = gr.Dropdown(["Vegetarian", "Non-Veg"], label="Diet", value="Vegetarian") | |
| spice = gr.Dropdown(["Medium", "Spicy"], label="Spice", value="Medium") | |
| lang = gr.Dropdown(["English", "Hindi"], label="Language", value="English") | |
| btn = gr.Button("π¨βπ³ COOK NOW", variant="primary") | |
| with gr.Column(): | |
| aud = gr.Audio(label="Voice") | |
| out = gr.Markdown(label="Recipe") | |
| btn.click(snapchef_logic, inputs=[img, diet, spice, lang], outputs=[out, aud]) | |
| app.launch() | |