File size: 1,914 Bytes
61723ad
5bfe249
e8135f4
 
 
06be144
 
5bfe249
 
1c8a261
055b956
 
f4fae98
61723ad
 
 
 
 
 
 
 
 
a06e462
 
61723ad
6949504
e895790
6949504
 
 
 
 
 
 
 
4e8a722
6949504
4e8a722
055b956
1c1f8d7
055b956
2795588
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import json
import openai
import pandas as pd
import streamlit as st

st.set_page_config(layout="wide")

st.header('Calories')
openai.api_key = st.secrets["open_ai_key"]

if 'gpt_response' not in st.session_state:
    st.session_state.gpt_response = None

context = '''create a valid JSON array of objects for tracking the calorie and macronutrient content of specified food items in the following format:
[{
"food_item_name": "the name of the food item that was inputted including quantity expressed as a string",
"number_of_calories": "number of calories for the specified quantity of the food item expressed as an integer",
"grams_of_protein": "number of grams of protein for the specified quantity of the food item expressed as an integer",
"grams_of_fat": "number of grams of fat for the specified quantity of the food item expressed as an integer",
"grams_of_carbs": "number of grams of carbohydrates for the specified quantity of the food item expressed as an integer"
}]'''

prompt = st.text_input('List food items:')
st.write('Example: *1 cup broccoli, 500g boneless skinless chicken breast, 1 cup coffee*')

def get_response(context, prompt):
    st.session_state.gpt_response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": context},
            {"role": "user", "content": prompt}
        ],
        temperature=0.2,
        max_tokens=1000
    )
    
st.button(label='Submit', on_click=get_response, kwargs=dict(context=context, prompt=prompt))

if st.session_state.gpt_response is not None:
    st.dataframe(pd.DataFrame(json.loads(st.session_state.gpt_response['choices'][0]['message']['content'])), hide_index=True)
    cost = st.session_state.gpt_response['usage']["prompt_tokens"]*(0.0015/1000) + st.session_state.gpt_response['usage']["completion_tokens"]*(0.002/1000)
    st.write(f'Cost for query was approx ${cost}')