Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from groq import Groq
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# -----------------------------
|
| 7 |
+
# Initialize Groq Client
|
| 8 |
+
# -----------------------------
|
| 9 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
| 10 |
+
|
| 11 |
+
# -----------------------------
|
| 12 |
+
# Function: Analyze Image
|
| 13 |
+
# -----------------------------
|
| 14 |
+
def analyze_ingredients(image):
|
| 15 |
+
with open(image, "rb") as f:
|
| 16 |
+
image_bytes = f.read()
|
| 17 |
+
|
| 18 |
+
base64_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 19 |
+
|
| 20 |
+
response = client.chat.completions.create(
|
| 21 |
+
model="llama-3.2-11b-vision-preview",
|
| 22 |
+
messages=[
|
| 23 |
+
{
|
| 24 |
+
"role": "user",
|
| 25 |
+
"content": [
|
| 26 |
+
{
|
| 27 |
+
"type": "text",
|
| 28 |
+
"text": "Identify food ingredients in this image. Return only comma separated list."
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"type": "image_url",
|
| 32 |
+
"image_url": {
|
| 33 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 34 |
+
}
|
| 35 |
+
}
|
| 36 |
+
]
|
| 37 |
+
}
|
| 38 |
+
],
|
| 39 |
+
temperature=0.3,
|
| 40 |
+
max_tokens=300,
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
return response.choices[0].message.content
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# -----------------------------
|
| 47 |
+
# Function: Generate Recipe
|
| 48 |
+
# -----------------------------
|
| 49 |
+
def generate_recipe(ingredients):
|
| 50 |
+
|
| 51 |
+
response = client.chat.completions.create(
|
| 52 |
+
model="llama-3.2-11b-text-preview",
|
| 53 |
+
messages=[
|
| 54 |
+
{
|
| 55 |
+
"role": "user",
|
| 56 |
+
"content": f"Suggest a simple recipe using these ingredients: {ingredients}"
|
| 57 |
+
}
|
| 58 |
+
],
|
| 59 |
+
temperature=0.7,
|
| 60 |
+
max_tokens=500,
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
return response.choices[0].message.content
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# -----------------------------
|
| 67 |
+
# Full Pipeline
|
| 68 |
+
# -----------------------------
|
| 69 |
+
def cooking_assistant(image):
|
| 70 |
+
|
| 71 |
+
ingredients = analyze_ingredients(image)
|
| 72 |
+
recipe = generate_recipe(ingredients)
|
| 73 |
+
|
| 74 |
+
return ingredients, recipe
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
# -----------------------------
|
| 78 |
+
# Gradio UI
|
| 79 |
+
# -----------------------------
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
|
| 82 |
+
gr.Markdown("# 🍳 AI Cooking Assistant (LLaMA 3.2 Vision + Groq)")
|
| 83 |
+
gr.Markdown("Upload an image of ingredients and get a recipe instantly.")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
image_input = gr.Image(type="filepath", label="Upload Ingredient Image")
|
| 87 |
+
|
| 88 |
+
ingredients_output = gr.Textbox(label="Detected Ingredients")
|
| 89 |
+
recipe_output = gr.Textbox(label="Suggested Recipe", lines=10)
|
| 90 |
+
|
| 91 |
+
btn = gr.Button("Generate Recipe")
|
| 92 |
+
|
| 93 |
+
btn.click(
|
| 94 |
+
fn=cooking_assistant,
|
| 95 |
+
inputs=image_input,
|
| 96 |
+
outputs=[ingredients_output, recipe_output]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
demo.launch()
|