File size: 1,116 Bytes
bd29083
 
 
 
 
15f4c90
bd29083
 
15f4c90
bd29083
 
 
15f4c90
bd29083
15f4c90
 
 
 
 
 
 
 
 
 
 
 
bd29083
 
 
 
 
15f4c90
 
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
import streamlit as st
import openai

def main():
    st.title("AI Chatbot")

    # Input field for OpenAI API Key
    api_key = st.text_input("Enter your OpenAI API Key", type="password")

    if api_key:
        openai.api_key = api_key
        user_input = st.text_area("Ask me anything:")

        if st.button("Generate Response"):
            if user_input.strip():
                try:
                    response = openai.ChatCompletion.create(
                        model="gpt-3.5-turbo",
                        messages=[{"role": "user", "content": user_input}]
                    )
                    
                    st.toast("Generating response...")  # Show a popup message
                    st.subheader("Response:")
                    st.write(response["choices"][0]["message"]["content"])
                except Exception as e:
                    st.error(f"Error: {str(e)}")  # Show error popup
            else:
                st.warning("Please enter a question.")
    else:
        st.warning("Please enter your OpenAI API Key to proceed.")

if __name__ == "__main__":
    main()