|
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", |
|
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 |
|
|
|
|
|
|
|
def read_markdown_file(file_path): |
|
with open(file_path, 'r', encoding='utf-8') as file: |
|
return file.read() |
|
|
|
|
|
|
|
def current_year(): |
|
now = datetime.now() |
|
return now.year |
|
|
|
|
|
st.set_page_config(layout="wide") |
|
st.title("Yin's Profile π€") |
|
|
|
|
|
with st.sidebar: |
|
with st.expander("Instruction Manual"): |
|
st.markdown(""" |
|
## Yin's Profile π€ Chatbot |
|
This Streamlit app allows you to chat with GPT-4o model. However, it's been deprecated due to high cost and can be turned on upon request. |
|
### 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-4o. |
|
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/y-yin-homepage) | [LinkedIn](https://www.linkedin.com/in/yiqiaoyin/) | [YouTube](https://youtube.com/YiqiaoYin/) |
|
Enjoy chatting with Yin's assistant! |
|
""") |
|
|
|
|
|
with st.expander("Examples"): |
|
st.success("Example: Who is Yiqiao Yin?") |
|
st.success("Example: What did Yiqiao do at graduate school?") |
|
st.success("Example: Where to find published papers by Yiqiao?") |
|
st.success("Example: What is Yiqiao's view on AI?") |
|
st.success("Example: What are some online links by Yiqiao I can read about?") |
|
st.success("Example: What is Yiqiao's view on stock market?") |
|
|
|
|
|
with st.expander("AI Consulting"): |
|
stripe_payment_link_consulting = os.environ["STRIPE_PAYMENT_LINK_CONSULTING"] |
|
st.markdown( |
|
f""" |
|
Want website with copilot like mine? βοΈ Schedule an appointment with me [here]({stripe_payment_link_consulting}) |
|
""" |
|
) |
|
|
|
|
|
with st.expander("Donation"): |
|
stripe_payment_link = os.environ["STRIPE_PAYMENT_LINK"] |
|
st.markdown( |
|
f""" |
|
Want to support me? π Click here using this [link]({stripe_payment_link}). |
|
""" |
|
) |
|
|
|
|
|
if st.button("Clear Session"): |
|
st.session_state.messages = [] |
|
st.experimental_rerun() |
|
|
|
|
|
current_year = current_year() |
|
st.markdown( |
|
f""" |
|
<h6 style='text-align: left;'>Copyright Β© 2010-{current_year} Present Yiqiao Yin</h6> |
|
""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
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 = [] |
|
|
|
|
|
md_file_path = 'docs/yiqiao_yin.md' |
|
|
|
|
|
yiqiaoyin_profile = read_markdown_file(md_file_path) |
|
|
|
|
|
if not any(msg["role"] == "system" for msg in st.session_state.messages): |
|
st.session_state.messages.append({"role": "system", "content": f"You know the following about Mr. Yiqiao Yin: {yiqiaoyin_profile}"}) |
|
|
|
|
|
for message in st.session_state.messages: |
|
if message["role"] != "system": |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
if prompt := st.chat_input("π Ask any question or feel free to use the examples provided in the left sidebar."): |
|
|
|
|
|
st.chat_message("user").markdown(prompt) |
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": prompt}) |
|
|
|
|
|
bot = ChatBot() |
|
bot.history = st.session_state.messages.copy() |
|
response = bot.generate_response(prompt) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(response) |
|
|
|
|
|
st.session_state.messages.append({"role": "assistant", "content": response}) |
|
|