File size: 5,037 Bytes
c07cbe4
 
941bf52
 
c07cbe4
941bf52
 
c07cbe4
 
 
9bb5c24
941bf52
c07cbe4
 
941bf52
c07cbe4
2eb5ff3
c07cbe4
 
941bf52
c07cbe4
 
941bf52
c07cbe4
941bf52
c07cbe4
 
941bf52
 
c07cbe4
 
 
 
348c6ea
 
c07cbe4
 
 
 
aaf4dac
 
c07cbe4
c13ec1f
941bf52
 
c07cbe4
 
a104a9f
3048408
 
 
 
 
a104a9f
3048408
a104a9f
 
aafe096
3170b3f
aafe096
 
 
 
a104a9f
 
 
 
a8fae85
c07cbe4
 
 
 
 
 
 
 
aaf4dac
c07cbe4
 
 
f4a8199
c07cbe4
 
 
 
 
941bf52
c07cbe4
 
50500f5
c07cbe4
 
941bf52
c07cbe4
 
 
941bf52
c07cbe4
 
 
 
 
33b6549
34cae98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c07cbe4
a20cadc
33b6549
c07cbe4
 
33b6549
c07cbe4
 
941bf52
c07cbe4
 
 
 
941bf52
c07cbe4
 
 
941bf52
c07cbe4
 
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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-4o-mini", # NOTE: feel free to change it to "gpt-4" or "our LLM"
            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


# Read the content of the Markdown file
def read_markdown_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        return file.read()


# Credit: Time
def current_year():
    now = datetime.now()
    return now.year


st.set_page_config(layout="wide")
# st.title("A.V.A. πŸ€–: How can I help?")


with st.sidebar:
    # Example:
    st.write("## Examples:")
    if st.button("Who's Yiqiao Yin?"):
        st.session_state["example_prompt"] = "Who's Yiqiao Yin?"
    if st.button("Any papers by Yiqiao?"):
        st.session_state["example_prompt"] = "Any papers by Yiqiao?"
    if st.button("Yiqiao's view on AI?"):
        st.session_state["example_prompt"] = "What is Yiqiao's view on AI?"
    if st.button("Yiqiao's view on market?"):
        st.session_state["example_prompt"] = "What is Yiqiao's view on stock market?"

    # Add a button to clear the session state
    st.write("---------------------------------")
    if st.button("Clear Session"):
        st.session_state.messages = []
        st.experimental_rerun()

    st.markdown("""
        ### 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/)
    """)

    # 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 = []

# Path to the Markdown file
md_file_path = 'docs/yiqiao_yin.md'

# Get the content of the Markdown file
yiqiaoyin_profile = read_markdown_file(md_file_path)

# Add the system message with the profile information to the chat history if it hasn't been added yet
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}"})

# Display chat messages from history on app rerun
for message in st.session_state.messages:
    if message["role"] != "system":  # Skip system messages
        with st.chat_message(message["role"]):
            st.markdown(message["content"])

# Check if an example prompt was selected
if "example_prompt" in st.session_state and st.session_state["example_prompt"]:
    example_prompt = st.session_state.pop("example_prompt")  # Remove after using
    st.session_state.messages.append({"role": "user", "content": example_prompt})

    # Display user message in chat message container
    st.chat_message("user").markdown(example_prompt)

    # API Call
    bot = ChatBot()
    bot.history = st.session_state.messages.copy()  # Update history from messages
    response = bot.generate_response(example_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})

# React to user input
if prompt := st.chat_input("πŸ˜‰ Virtual Yiqiao is here to help."):

    # Display user message in chat message container
    st.chat_message("user").markdown(prompt)

    # 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})