File size: 2,060 Bytes
fa4c443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import openai
from PIL import Image
import io
import base64
import os

openai.api_key = os.getenv("openapikey")

def generate_recipe(image_file):
    try:
        # Convert uploaded image to base64
        img = Image.open(image_file)
        buffered = io.BytesIO()
        img.save(buffered, format="JPEG")
        img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')

        response = openai.chat.completions.create(
            model="gpt-4o", #Correct Model.
            messages=[
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": "What is this food? Generate a recipe for it."},
                        {
                            "type": "image_url",
                            "image_url": {
                                "url": f"data:image/jpeg;base64,{img_base64}"
                            },
                        },
                    ],
                }
            ],
            max_tokens=500,
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        st.error(f"An error occurred: {e}")
        return None

st.title("Recipe from Image")

url='https://jonascleveland.com/wp-content/uploads/2023/08/12-Best-AI-Recipe-Generators-The-Culinary-Revolution-in-the-Digital-Era-2.png'
st.image(url,width=800,)
st.markdown(f"""
<style>
    /* Set the background image for the entire app */
    .stApp {{
        # background-image: url("https://i.pinimg.com/736x/29/51/8d/29518df9a720818938a3a58cf6c026df.jpg");
        background-color: #A9A9A9;
        background-size: 100px;
        background-repeat:no;
        background-attachment: auto;
        background-position:center;
    }}
</style>
""", unsafe_allow_html=True)
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

if uploaded_file:
    with st.spinner("Generating recipe..."):
        recipe = generate_recipe(uploaded_file)
        if recipe:
            st.write(recipe)