File size: 3,252 Bytes
261bad5
9f98769
729a463
9f98769
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261bad5
9f98769
 
 
 
 
 
 
 
 
729a463
9f98769
729a463
 
9f98769
 
 
 
 
 
 
 
 
 
 
 
729a463
9f98769
 
 
 
 
 
 
 
 
 
 
 
 
 
261bad5
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import streamlit as st
import openai

# Function to interact with GPT-3 model
def ask_gpt3_personalized(prompt, height_cm, weight_kg, age):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a chatbot that will answer queries related to fitness and nutrition. The chatbot should understand questions about workout routines, dietary advice, and general fitness tips. Chatbot will offer personalized workout and diet plans based on user inputs such as body type, fitness goals, and dietary restrictions. this is the chat history use this as a reference to answer the queries"+st.session_state.messages},
            {"role": "user", "content": "my height is " + str(height_cm) + " cm, my weight is " + str(weight_kg) + " kg, and I am " + str(age) + " years old."+prompt},
        ]
    )
    return response['choices'][0]['message']['content']

def ask_gpt3(prompt):
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a chatbot that will answer queries related to fitness and nutrition. The chatbot should understand questions about workout routines, dietary advice, and general fitness tips. Chatbot will offer personalized workout and diet plans based on user inputs such as body type, fitness goals, and dietary restrictions. this is the chat history use this as a reference to answer the queries"+st.session_state.messages},
            {"role": "user", "content": prompt},
        ]
    )
    return response['choices'][0]['message']['content']

# Main function to run the Streamlit app
def main():
    st.title("Fitness Chatbot")
    
    # add a sibebar that can take in user input for the chatbot prompt and the API key
    st.sidebar.title("Personal Information")
    openai.api_key = st.sidebar.text_input("Your OpenAI API Key here", "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")
    height_cm = st.sidebar.number_input("Height (cm)", 0, 300)
    weight_kg = st.sidebar.number_input("Weight (kg)", 0, 300)
    age = st.sidebar.number_input("Age", 0, 100)
    
    
    # Initialize conversation history
    if "messages" not in st.session_state:
        st.session_state.messages = []

    # User input
    user_input = st.chat_input("Ask something")

    # If user input is not empty
    if user_input:
        # Add user input to conversation history
        st.session_state.messages.append({"role": "user", "content": user_input})

        # Get chatbot response
        if height_cm and weight_kg and age:
            chatbot_response = ask_gpt3_personalized(user_input, height_cm, weight_kg, age)
        else:
            chatbot_response = ask_gpt3(user_input)

        # Add chatbot response to conversation history
        st.session_state.messages.append({"role": "assistant", "content": chatbot_response})

        # Display conversation history
        for message in st.session_state.messages:
            with st.chat_message(message["role"]):
                st.markdown(message["content"])

        # Clear the chat input
        st.session_state["chat_input"] = ""

# Run the main function
if __name__ == "__main__":
    main()