-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ai_invester.py
152 lines (122 loc) · 4.03 KB
/
Ai_invester.py
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
145
146
147
148
149
150
151
152
import time
import os
import joblib
import streamlit as st
from dotenv import load_dotenv
import google.generativeai as genai
load_dotenv()
GOOGLE_API_KEY = os.environ.get('API_TOKEN')
genai.configure(api_key=GOOGLE_API_KEY)
new_chat_id = f'{time.time()}'
MODEL_ROLE = 'ai'
AI_AVATAR_ICON = '$'
try:
os.mkdir('data/')
except:
pass
try:
past_chats: dict = joblib.load('data/past_chats_list')
except:
past_chats = {}
def load_chat_data():
try:
st.session_state.messages = joblib.load(
f'data/{st.session_state.chat_id}-st_messages'
)
st.session_state.gemini_history = joblib.load(
f'data/{st.session_state.chat_id}-gemini_messages'
)
print('old cache')
except:
st.session_state.messages = []
st.session_state.gemini_history = []
print('new_cache made')
def save_chat_data():
joblib.dump(
st.session_state.messages,
f'data/{st.session_state.chat_id}-st_messages',
)
joblib.dump(
st.session_state.gemini_history,
f'data/{st.session_state.chat_id}-gemini_messages',
)
def main():
try:
os.mkdir('data/')
except:
pass
try:
past_chats: dict = joblib.load('data/past_chats_list')
except:
past_chats = {}
with st.sidebar:
st.write('# Past Chats')
if st.session_state.get('chat_id') is None:
st.session_state.chat_id = st.selectbox(
label='Pick a past chat',
options=[new_chat_id] + list(past_chats.keys()),
format_func=lambda x: past_chats.get(x, 'New Chat'),
placeholder='_',
)
else:
st.session_state.chat_id = st.selectbox(
label='Pick a past chat',
options=[new_chat_id, st.session_state.chat_id] + list(past_chats.keys()),
index=1,
format_func=lambda x: past_chats.get(x, 'New Chat' if x != st.session_state.chat_id else st.session_state.chat_title),
placeholder='_',
)
st.session_state.chat_title = f'ChatSession-{st.session_state.chat_id}'
st.write('# CHATBOT')
load_chat_data()
st.session_state.model = genai.GenerativeModel('gemini-pro')
st.session_state.chat = st.session_state.model.start_chat(
history=st.session_state.gemini_history,
)
for message in st.session_state.messages:
with st.chat_message(
name=message['role'],
avatar=message.get('avatar'),
):
st.markdown(message['content'])
if prompt := st.chat_input('Your message here...'):
if st.session_state.chat_id not in past_chats.keys():
past_chats[st.session_state.chat_id] = st.session_state.chat_title
joblib.dump(past_chats, 'data/past_chats_list')
with st.chat_message('user'):
st.markdown(prompt)
st.session_state.messages.append(
dict(
role='user',
content=prompt,
)
)
response = st.session_state.chat.send_message(
prompt,
stream=True,
)
with st.chat_message(
name=MODEL_ROLE,
avatar=AI_AVATAR_ICON,
):
message_placeholder = st.empty()
full_response = ''
assistant_response = response
for chunk in response:
for ch in chunk.text.split(' '):
full_response += ch + ' '
time.sleep(0.05)
message_placeholder.write(full_response + '▌')
message_placeholder.write(full_response)
st.session_state.messages.append(
dict(
role=MODEL_ROLE,
content=st.session_state.chat.history[-1].parts[0].text,
avatar=AI_AVATAR_ICON,
)
)
st.session_state.gemini_history = st.session_state.chat.history
save_chat_data()
if __name__ == "__main__":
assess_user_needs()
main()