Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("OpenAI GPT-4o π€")
|
30 |
+
|
31 |
+
|
32 |
+
with st.sidebar:
|
33 |
+
with st.expander("Instruction Manual"):
|
34 |
+
st.markdown("""
|
35 |
+
## OpenAI GPT-4o π€ 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/meta-llama) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/)
|
43 |
+
Enjoy chatting with Meta's Llama3 model!
|
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.session_state["bot"] = None
|
57 |
+
st.experimental_rerun()
|
58 |
+
|
59 |
+
|
60 |
+
# Initialize chat history
|
61 |
+
if "messages" not in st.session_state:
|
62 |
+
st.session_state.messages = []
|
63 |
+
|
64 |
+
|
65 |
+
# Display chat messages from history on app rerun
|
66 |
+
for message in st.session_state.messages:
|
67 |
+
with st.chat_message(message["role"]):
|
68 |
+
st.markdown(message["content"])
|
69 |
+
|
70 |
+
|
71 |
+
# React to user input
|
72 |
+
bot = ChatBot()
|
73 |
+
if bot:
|
74 |
+
st.session_state["bot"] = bot
|
75 |
+
|
76 |
+
|
77 |
+
if prompt := st.chat_input("π Ask any question or feel free to use the examples provided in the left sidebar."):
|
78 |
+
|
79 |
+
# Display user message in chat message container
|
80 |
+
st.chat_message("user").markdown(prompt)
|
81 |
+
|
82 |
+
# Add user message to chat history
|
83 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
84 |
+
|
85 |
+
# API Call
|
86 |
+
response = bot.generate_response("What is the weather like today?")
|
87 |
+
|
88 |
+
# Display assistant response in chat message container
|
89 |
+
with st.chat_message("assistant"):
|
90 |
+
st.markdown(response)
|
91 |
+
# Add assistant response to chat history
|
92 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|