Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
import os
|
7 |
+
|
8 |
+
openai.api_key = os.getenv("openapikey")
|
9 |
+
|
10 |
+
def generate_recipe(image_file):
|
11 |
+
try:
|
12 |
+
# Convert uploaded image to base64
|
13 |
+
img = Image.open(image_file)
|
14 |
+
buffered = io.BytesIO()
|
15 |
+
img.save(buffered, format="JPEG")
|
16 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
17 |
+
|
18 |
+
response = openai.chat.completions.create(
|
19 |
+
model="gpt-4o", #Correct Model.
|
20 |
+
messages=[
|
21 |
+
{
|
22 |
+
"role": "user",
|
23 |
+
"content": [
|
24 |
+
{"type": "text", "text": "What is this food? Generate a recipe for it."},
|
25 |
+
{
|
26 |
+
"type": "image_url",
|
27 |
+
"image_url": {
|
28 |
+
"url": f"data:image/jpeg;base64,{img_base64}"
|
29 |
+
},
|
30 |
+
},
|
31 |
+
],
|
32 |
+
}
|
33 |
+
],
|
34 |
+
max_tokens=500,
|
35 |
+
)
|
36 |
+
return response.choices[0].message.content.strip()
|
37 |
+
except Exception as e:
|
38 |
+
st.error(f"An error occurred: {e}")
|
39 |
+
return None
|
40 |
+
|
41 |
+
st.title("Recipe from Image")
|
42 |
+
|
43 |
+
url='https://jonascleveland.com/wp-content/uploads/2023/08/12-Best-AI-Recipe-Generators-The-Culinary-Revolution-in-the-Digital-Era-2.png'
|
44 |
+
st.image(url,width=800,)
|
45 |
+
st.markdown(f"""
|
46 |
+
<style>
|
47 |
+
/* Set the background image for the entire app */
|
48 |
+
.stApp {{
|
49 |
+
# background-image: url("https://i.pinimg.com/736x/29/51/8d/29518df9a720818938a3a58cf6c026df.jpg");
|
50 |
+
background-color: #A9A9A9;
|
51 |
+
background-size: 100px;
|
52 |
+
background-repeat:no;
|
53 |
+
background-attachment: auto;
|
54 |
+
background-position:center;
|
55 |
+
}}
|
56 |
+
</style>
|
57 |
+
""", unsafe_allow_html=True)
|
58 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
59 |
+
|
60 |
+
if uploaded_file:
|
61 |
+
with st.spinner("Generating recipe..."):
|
62 |
+
recipe = generate_recipe(uploaded_file)
|
63 |
+
if recipe:
|
64 |
+
st.write(recipe)
|