Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from groq import Groq
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Page Config
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="AI Cooking Assistant",
|
| 11 |
+
page_icon="🍳",
|
| 12 |
+
layout="centered"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Title
|
| 16 |
+
st.title("🍳 AI Cooking Assistant")
|
| 17 |
+
st.write("Upload ingredients image and get recipe suggestions")
|
| 18 |
+
|
| 19 |
+
# Load API Key
|
| 20 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 21 |
+
|
| 22 |
+
if not GROQ_API_KEY:
|
| 23 |
+
st.error("❌ Please add GROQ_API_KEY in Hugging Face Secrets")
|
| 24 |
+
st.stop()
|
| 25 |
+
|
| 26 |
+
# Initialize Groq Client
|
| 27 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# Convert Image to Base64
|
| 31 |
+
def image_to_base64(image):
|
| 32 |
+
buffer = io.BytesIO()
|
| 33 |
+
image.save(buffer, format="PNG")
|
| 34 |
+
return base64.b64encode(buffer.getvalue()).decode()
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# File Uploader
|
| 38 |
+
uploaded_file = st.file_uploader(
|
| 39 |
+
"Upload Image of Ingredients",
|
| 40 |
+
type=["jpg", "jpeg", "png"]
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
if uploaded_file:
|
| 45 |
+
|
| 46 |
+
# Show Image
|
| 47 |
+
image = Image.open(uploaded_file)
|
| 48 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 49 |
+
|
| 50 |
+
if st.button("🍽️ Generate Recipe"):
|
| 51 |
+
|
| 52 |
+
with st.spinner("Analyzing ingredients..."):
|
| 53 |
+
|
| 54 |
+
# Convert image
|
| 55 |
+
img_base64 = image_to_base64(image)
|
| 56 |
+
|
| 57 |
+
# Prompt
|
| 58 |
+
prompt = """
|
| 59 |
+
You are a professional chef AI.
|
| 60 |
+
Look at the image and identify ingredients.
|
| 61 |
+
Then suggest:
|
| 62 |
+
1. Dish name
|
| 63 |
+
2. Ingredients list
|
| 64 |
+
3. Step-by-step recipe
|
| 65 |
+
4. Cooking tips
|
| 66 |
+
"""
|
| 67 |
+
|
| 68 |
+
# Call LLaMA Vision Model
|
| 69 |
+
response = client.chat.completions.create(
|
| 70 |
+
model="llama-3.2-11b-vision-preview",
|
| 71 |
+
messages=[
|
| 72 |
+
{
|
| 73 |
+
"role": "user",
|
| 74 |
+
"content": [
|
| 75 |
+
{"type": "text", "text": prompt},
|
| 76 |
+
{
|
| 77 |
+
"type": "image_url",
|
| 78 |
+
"image_url": {
|
| 79 |
+
"url": f"data:image/png;base64,{img_base64}"
|
| 80 |
+
}
|
| 81 |
+
}
|
| 82 |
+
]
|
| 83 |
+
}
|
| 84 |
+
],
|
| 85 |
+
max_tokens=800
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
result = response.choices[0].message.content
|
| 89 |
+
|
| 90 |
+
# Display Output
|
| 91 |
+
st.subheader("🍲 Suggested Recipe")
|
| 92 |
+
st.write(result)
|