File size: 3,700 Bytes
2e1eb5d 93d1f27 2e1eb5d a137398 93d1f27 2e1eb5d a137398 93d1f27 4735832 a137398 93d1f27 d041338 93d1f27 04bc3d9 a137398 93d1f27 04bc3d9 93d1f27 a137398 93d1f27 a137398 93d1f27 a137398 93d1f27 a137398 93d1f27 04bc3d9 93d1f27 a137398 93d1f27 04bc3d9 93d1f27 a137398 04bc3d9 a137398 04bc3d9 a137398 04bc3d9 93d1f27 a137398 93d1f27 a137398 93d1f27 a137398 04bc3d9 93d1f27 a137398 2e1eb5d 04bc3d9 a137398 2e1eb5d a137398 2e1eb5d 04bc3d9 2e1eb5d 93d1f27 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import gradio as gr
import base64
from groq import Groq
from PIL import Image
import io
import os
# Initialize Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
# Convert image β base64
def encode_image(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode()
# π STEP 1: Detect ingredients
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)}"
# π² STEP 2: Generate recipe
def generate_recipe(ingredients_text):
if not ingredients_text:
return "No ingredients detected."
# π¨ HARD BLOCK for non-food
if "NO_FOOD_DETECTED" in ingredients_text:
return "β No food detected in the image. Please upload a valid food image."
# Extra safety filter
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()
# π¨ Final safety check
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)}"
# π¨ UI
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() |