Spaces:
Running
Running
File size: 4,446 Bytes
6dca666 8e423a7 8f4a175 8e423a7 6dca666 8e423a7 8569739 8e423a7 8569739 8e423a7 8569739 8e423a7 e7bf46e 8569739 8e423a7 2c45d48 8e423a7 8417fa4 fd0e171 8417fa4 6dca666 8e423a7 28217e3 8e423a7 a80cfa9 8e423a7 a80cfa9 47954f5 8e423a7 3eccf2a a80cfa9 56a0864 a80cfa9 3eccf2a 274c763 e7bf46e 3eccf2a |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
from datetime import datetime
import streamlit as st
import os
from openai import OpenAI
class ChatBot:
def __init__(self):
self.client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
self.history = [{"role": "system", "content": "You are a helpful assistant."}]
def generate_response(self, prompt: str) -> str:
self.history.append({"role": "user", "content": prompt})
completion = self.client.chat.completions.create(
model="gpt-3.5-turbo", # NOTE: feel free to change it to gpt-4, or gpt-4o
messages=self.history
)
response = completion.choices[0].message.content
self.history.append({"role": "assistant", "content": response})
return response
def get_history(self) -> list:
return self.history
# Credit: Time
def current_year():
now = datetime.now()
return now.year
st.set_page_config(layout="wide")
st.title("Just chat! π€")
with st.sidebar:
with st.expander("Instruction Manual"):
st.markdown("""
## OpenAI GPT-4 π€ Chatbot
This Streamlit app allows you to chat with GPT-4 model. The model GPT-4o is deprecated due to high cost and will only be turned on for special occasions.
### How to Use:
1. **Input**: Type your prompt into the chat input box labeled "What is up?".
2. **Response**: The app will display a response from GPT-4.
3. **Chat History**: Previous conversations will be shown on the app.
### Credits:
- **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/)
Enjoy chatting with OpenAI's GPT-4 model!
""")
# Example:
with st.expander("Examples"):
st.success("Example: Explain what is supervised learning.")
st.success("Example: What is large language model?")
st.success("Example: How to conduct an AI experiment?")
st.success("Example: Write a tensorflow flow code with a 3-layer neural network model.")
# Add a button to clear the session state
if st.button("Clear Session"):
st.session_state.messages = []
st.experimental_rerun()
# Donation
# stripe_payment_link = os.environ["STRIPE_PAYMENT_LINK"]
# st.markdown(
# f"""
# Want to support me? π Click here using this [link]({stripe_payment_link}).
# """
# )
# Credit:
current_year = current_year() # This will print the current year
st.markdown(
f"""
<h6 style='text-align: left;'>Copyright Β© 2010-{current_year} Present Yiqiao Yin</h6>
""",
unsafe_allow_html=True,
)
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Ensure messages are a list of dictionaries
if not isinstance(st.session_state.messages, list):
st.session_state.messages = []
if not all(isinstance(msg, dict) for msg in st.session_state.messages):
st.session_state.messages = []
# Display chat messages from history on app rerun, excluding system messages
for message in st.session_state.messages:
if message["role"] != "system": # Skip displaying system messages
with st.chat_message(message["role"]):
st.markdown(message["content"])
# React to user input
if prompt := st.chat_input("π Ask any question or feel free to use the examples provided in the left sidebar."):
# Display user message in chat message container
st.chat_message("user").markdown(prompt)
# Add a system message to the chat history, but don't display it
st.session_state.messages.append({"role": "system", "content": f"You are a helpful assistant. Year now is {current_year}"})
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# API Call
bot = ChatBot()
bot.history = st.session_state.messages.copy() # Update history from messages
response = bot.generate_response(prompt)
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.markdown(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
|