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""" """, 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)