Created
December 2, 2023 20:27
-
-
Save kcarnold/7f0d2451098de64d23197900d9d11710 to your computer and use it in GitHub Desktop.
Recipe generator - Streamlit + OpenAI chatcompletions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import os | |
from openai import OpenAI | |
# load the API key from a .env file | |
from dotenv import load_dotenv | |
load_dotenv() | |
st.title("Recipe Finder") | |
recipe_name = st.text_input("Enter recipe name:") | |
missing_ingredient = st.text_input("Enter missing ingredient:") | |
if not recipe_name: | |
st.stop() | |
st.write(f"Searching for {recipe_name} without {missing_ingredient}...") | |
client = OpenAI() | |
client.api_key = os.getenv("OPENAI_API_KEY") | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": f"Recipe for {recipe_name}" | |
}, | |
], | |
temperature=0.7, | |
#max_tokens=2411, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
recipe_content = response.choices[0].message.content | |
st.subheader("Recipe") | |
st.markdown(recipe_content) | |
response_refined = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=[ | |
{ | |
"role": "user", | |
"content": f"Recipe for {recipe_name}" | |
}, | |
{ | |
"role": "assistant", | |
"content": recipe_content | |
}, | |
{ | |
"role": "user", | |
"content": f"But I don't have {missing_ingredient}" | |
}, | |
], | |
temperature=0.7, | |
#max_tokens=2411, | |
top_p=1, | |
frequency_penalty=0, | |
presence_penalty=0 | |
) | |
recipe_content_refined = response_refined.choices[0].message.content | |
st.subheader("Recipe without missing ingredient") | |
st.markdown(recipe_content_refined) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment