Skip to content

Instantly share code, notes, and snippets.

@hlb
Created March 28, 2023 16:01
Show Gist options
  • Save hlb/883fa2a12002bdb5a744657741337b7a to your computer and use it in GitHub Desktop.
Save hlb/883fa2a12002bdb5a744657741337b7a to your computer and use it in GitHub Desktop.
Example to generate a python code by ChatGPT
import streamlit as st
import openai
openai.api_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
def generate_recipe(food_name, ingredients_must_have):
prompt = f"Create a recipe for {food_name} that must include the following ingredients: {ingredients_must_have}."
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that provides recipes."},
{"role": "user", "content": prompt}
],
max_tokens=1024,
temperature=0.8,
n=1
)
recipe = response['choices'][0]['message']['content'].strip()
return recipe
st.title("Recipe Generator using GPT-3.5-turbo")
food_name = st.text_input("Enter the food name:")
ingredients_must_have = st.text_input("Enter the ingredients that must be included (separated by commas):")
if st.button("Generate Recipe"):
if not food_name or not ingredients_must_have:
st.warning("Please provide both food name and required ingredients.")
else:
ingredients_list = [ingredient.strip() for ingredient in ingredients_must_have.split(",")]
recipe = generate_recipe(food_name, ingredients_list)
st.markdown(f"**Generated Recipe for {food_name}:**")
st.write(recipe)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment