| import gradio as gr |
| import base64 |
| from groq import Groq |
| from PIL import Image |
| import io |
| import os |
|
|
| |
| client = Groq(api_key=os.getenv("GROQ_API_KEY")) |
|
|
| |
| def encode_image(image): |
| buffered = io.BytesIO() |
| image.save(buffered, format="JPEG") |
| return base64.b64encode(buffered.getvalue()).decode() |
|
|
|
|
| |
| def analyze_ingredients(image): |
| if image is None: |
| return "Please upload an image." |
|
|
| base64_image = encode_image(image) |
|
|
| try: |
| completion = client.chat.completions.create( |
| model="meta-llama/llama-4-scout-17b-16e-instruct", |
| messages=[ |
| { |
| "role": "user", |
| "content": [ |
| { |
| "type": "text", |
| "text": """ |
| Analyze this image carefully. |
| |
| If the image contains food: |
| β List all ingredients clearly (comma separated). |
| |
| If the image does NOT contain food: |
| β Respond ONLY with: NO_FOOD_DETECTED |
| """ |
| }, |
| { |
| "type": "image_url", |
| "image_url": { |
| "url": f"data:image/jpeg;base64,{base64_image}" |
| } |
| } |
| ], |
| } |
| ], |
| temperature=0.3, |
| ) |
|
|
| result = completion.choices[0].message.content.strip() |
|
|
| return result |
|
|
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
|
|
| |
| def generate_recipe(ingredients_text): |
| if not ingredients_text: |
| return "No ingredients detected." |
|
|
| |
| if "NO_FOOD_DETECTED" in ingredients_text: |
| return "β No food detected in the image. Please upload a valid food image." |
|
|
| |
| bad_keywords = ["diagram", "cnn", "architecture", "network", "chart"] |
| if any(word in ingredients_text.lower() for word in bad_keywords): |
| return "β Invalid input. This does not look like food ingredients." |
|
|
| try: |
| completion = client.chat.completions.create( |
| model="llama-3.1-8b-instant", |
| messages=[ |
| { |
| "role": "user", |
| "content": f""" |
| You are a strict cooking assistant. |
| |
| Only generate a recipe if the input contains real food ingredients. |
| |
| Ingredients: |
| {ingredients_text} |
| |
| If this is NOT food β reply exactly: |
| INVALID_FOOD_INPUT |
| |
| Otherwise provide: |
| - Dish name |
| - Ingredients |
| - Step-by-step instructions |
| """ |
| } |
| ], |
| temperature=0.7, |
| ) |
|
|
| result = completion.choices[0].message.content.strip() |
|
|
| |
| if "INVALID_FOOD_INPUT" in result: |
| return "β Could not generate recipe. Invalid food input." |
|
|
| return result |
|
|
| except Exception as e: |
| return f"Error: {str(e)}" |
|
|
|
|
| |
| with gr.Blocks(title="AI Cooking Assistant") as app: |
| gr.Markdown("# π³ AI Cooking Assistant") |
| gr.Markdown("Upload food image β Detect ingredients β Generate recipe") |
|
|
| image_input = gr.Image(type="pil", label="Upload Ingredients Image") |
|
|
| detect_btn = gr.Button("π Detect Ingredients") |
| ingredients_output = gr.Textbox(label="Detected Ingredients") |
|
|
| recipe_btn = gr.Button("π² Generate Recipe") |
| recipe_output = gr.Textbox(label="Recipe", lines=12) |
|
|
| detect_btn.click(analyze_ingredients, inputs=image_input, outputs=ingredients_output) |
| recipe_btn.click(generate_recipe, inputs=ingredients_output, outputs=recipe_output) |
|
|
| app.launch() |