eagle0504 commited on
Commit
941bf52
β€’
1 Parent(s): b8eb15d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -0
app.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import os
3
+ from openai import OpenAI
4
+
5
+
6
+ class ChatBot:
7
+ def __init__(self):
8
+ self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
9
+ self.history = [{"role": "system", "content": "You are a helpful assistant."}]
10
+
11
+ def generate_response(self, prompt: str) -> str:
12
+ self.history.append({"role": "user", "content": prompt})
13
+
14
+ completion = self.client.chat.completions.create(
15
+ model="gpt-4o",
16
+ messages=self.history
17
+ )
18
+
19
+ response = completion.choices[0].message.content
20
+ self.history.append({"role": "assistant", "content": response})
21
+
22
+ return response
23
+
24
+ def get_history(self) -> list:
25
+ return self.history
26
+
27
+
28
+ st.set_page_config(layout="wide")
29
+ st.title("Yin's Profile πŸ€–")
30
+
31
+
32
+ with st.sidebar:
33
+ with st.expander("Instruction Manual"):
34
+ st.markdown("""
35
+ ## Yin's Profile πŸ€– Chatbot
36
+ This Streamlit app allows you to chat with GPT-4o model.
37
+ ### How to Use:
38
+ 1. **Input**: Type your prompt into the chat input box labeled "What is up?".
39
+ 2. **Response**: The app will display a response from GPT-4o.
40
+ 3. **Chat History**: Previous conversations will be shown on the app.
41
+ ### Credits:
42
+ - **Developer**: [Yiqiao Yin](https://www.y-yin.io/) | [App URL](https://huggingface.co/spaces/eagle0504/gpt-4o-demo) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
43
+ Enjoy chatting with Yin's assistant!
44
+ """)
45
+
46
+ # Example:
47
+ st.success("Example: Explain what is supervised learning.")
48
+ st.success("Example: What is large language model?")
49
+ st.success("Example: How to conduct an AI experiment?")
50
+ st.success("Example: Write a tensorflow flow code with a 3-layer neural network model.")
51
+
52
+
53
+ # Add a button to clear the session state
54
+ if st.button("Clear Session"):
55
+ st.session_state.messages = []
56
+ st.experimental_rerun()
57
+
58
+
59
+ # Initialize chat history
60
+ if "messages" not in st.session_state:
61
+ st.session_state.messages = []
62
+
63
+ # Ensure messages are a list of dictionaries
64
+ if not isinstance(st.session_state.messages, list):
65
+ st.session_state.messages = []
66
+ if not all(isinstance(msg, dict) for msg in st.session_state.messages):
67
+ st.session_state.messages = []
68
+
69
+ # Display chat messages from history on app rerun
70
+ for message in st.session_state.messages:
71
+ with st.chat_message(message["role"]):
72
+ st.markdown(message["content"])
73
+
74
+ # React to user input
75
+ if prompt := st.chat_input("πŸ˜‰ Ask any question or feel free to use the examples provided in the left sidebar."):
76
+
77
+ # Display user message in chat message container
78
+ st.chat_message("user").markdown(prompt)
79
+
80
+ # Add user message to chat history
81
+ st.session_state.messages.append({"role": "user", "content": prompt})
82
+
83
+ # API Call
84
+ bot = ChatBot()
85
+ bot.history = st.session_state.messages.copy() # Update history from messages
86
+ response = bot.generate_response(prompt)
87
+
88
+ # Display assistant response in chat message container
89
+ with st.chat_message("assistant"):
90
+ st.markdown(response)
91
+
92
+ # Add assistant response to chat history
93
+ st.session_state.messages.append({"role": "assistant", "content": response})