Spaces:
Build error
Build error
| 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) |