Spaces:
Sleeping
Sleeping
import os | |
import openai | |
import streamlit as st | |
from openai import OpenAI | |
# Initialize OpenAI | |
openai.api_key = os.environ['OPENAI_API_KEY'] | |
client = OpenAI( | |
api_key=os.environ['OPENAI_API_KEY'] | |
) | |
# Function to generate the recipe and detailed instructions | |
def create_dish_prompt(list_of_ingredients, category): | |
prompt = f"Make a {category} dish with the following ingredients: " + ', '.join( | |
list_of_ingredients) + ".\nWrite down the title, detailed recipe, and a description of the image you want to generate.\n" | |
return prompt | |
# Function to generate the image | |
def generate_dalle_image(recipe_title): | |
response = client.images.generate( | |
model="dall-e-3", | |
prompt=recipe_title, | |
size="1024x1024", | |
quality="standard", | |
n=1, | |
) | |
url = response.data[0].url if response.data else None | |
return url | |
# Initialize Streamlit | |
st.image("logo 3.jpg") | |
st.title("AutoChef.AI") | |
# Create a text input box for users to input ingredients | |
list_of_ingredients = st.text_input("Enter your ingredients (comma-separated):", "") | |
# Create a text input for users to input the category of food | |
category = st.text_input("Enter the category of food such as fried, baked, grilled, etc:") | |
# Generate Recipe button at the bottom | |
if st.button("Generate Recipe"): | |
# Check if the user has entered ingredients | |
if list_of_ingredients: | |
# Convert the input string into a list of ingredients | |
list_of_ingredients = [item.strip() for item in list_of_ingredients.split(',')] | |
# Call your existing functions to generate the recipe and image | |
recipe = create_dish_prompt(list_of_ingredients, category) | |
completion = client.chat.completions.create( | |
messages=[ | |
{ | |
"role": "user", | |
"content": recipe, | |
} | |
], | |
model="gpt-3.5-turbo", | |
) | |
content = completion.choices[0].message.content | |
split_content = content.split('\n', 1) | |
recipe_title = split_content[0].strip() | |
detailed_recipe = split_content[1].strip() | |
url = generate_dalle_image(recipe_title) | |
# Display the recipe title and detailed recipe | |
st.title(recipe_title) | |
st.subheader("A comprehensive culinary guide outlining the step-by-step procedure for preparing a dish.") | |
st.write(detailed_recipe) | |
# Display the image if a URL exists | |
if url: | |
st.image(url) | |
else: | |
st.write("No image URL generated.") | |