Spaces:
Sleeping
Sleeping
Akshayram1
commited on
Commit
•
341de20
1
Parent(s):
73edfbf
Update app.py
Browse files
app.py
CHANGED
@@ -1,140 +1,143 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import google.generativeai as genai
|
3 |
-
from dotenv import load_dotenv
|
4 |
-
import os
|
5 |
-
|
6 |
-
# Load environment variables
|
7 |
-
load_dotenv()
|
8 |
-
|
9 |
-
# Configure Google Generative AI with API key
|
10 |
-
api_key = os.getenv("GENERATIVEAI_API_KEY")
|
11 |
-
genai.configure(api_key=api_key)
|
12 |
-
|
13 |
-
#
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
{
|
30 |
-
"category": "
|
31 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
32 |
-
},
|
33 |
-
{
|
34 |
-
"category": "
|
35 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
36 |
-
},
|
37 |
-
{
|
38 |
-
"category": "
|
39 |
-
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
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 |
-
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import google.generativeai as genai
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load environment variables
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Configure Google Generative AI with API key
|
10 |
+
api_key = os.getenv("GENERATIVEAI_API_KEY")
|
11 |
+
genai.configure(api_key=api_key)
|
12 |
+
|
13 |
+
# Initialize session state for message history if it doesn't exist
|
14 |
+
if 'messages' not in st.session_state:
|
15 |
+
st.session_state.messages = []
|
16 |
+
|
17 |
+
# Global variable to maintain chat session
|
18 |
+
chat = None
|
19 |
+
|
20 |
+
# Generation configuration and safety settings
|
21 |
+
generation_config = {
|
22 |
+
"temperature": 0.9,
|
23 |
+
"top_p": 0.5,
|
24 |
+
"top_k": 5,
|
25 |
+
"max_output_tokens": 1000,
|
26 |
+
}
|
27 |
+
|
28 |
+
safety_settings = [
|
29 |
+
{
|
30 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
31 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
32 |
+
},
|
33 |
+
{
|
34 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
35 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
36 |
+
},
|
37 |
+
{
|
38 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
39 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
40 |
+
},
|
41 |
+
{
|
42 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
43 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
44 |
+
},
|
45 |
+
]
|
46 |
+
|
47 |
+
def text_summary(text, isNew=False):
|
48 |
+
global chat
|
49 |
+
|
50 |
+
if isNew or chat is None:
|
51 |
+
model = genai.GenerativeModel(
|
52 |
+
model_name="gemini-pro",
|
53 |
+
generation_config=generation_config,
|
54 |
+
safety_settings=safety_settings
|
55 |
+
)
|
56 |
+
chat = model.start_chat()
|
57 |
+
chat.send_message("""
|
58 |
+
Act as a financial advisor and generate financial summaries in a structured and tabular format...
|
59 |
+
""") # Your existing prompt here
|
60 |
+
|
61 |
+
response = chat.send_message(text)
|
62 |
+
return response.text
|
63 |
+
|
64 |
+
# Custom CSS for chat interface
|
65 |
+
st.markdown("""
|
66 |
+
<style>
|
67 |
+
.chat-container {
|
68 |
+
height: 600px;
|
69 |
+
overflow-y: auto;
|
70 |
+
padding: 20px;
|
71 |
+
margin-bottom: 100px;
|
72 |
+
}
|
73 |
+
.user-message {
|
74 |
+
background-color: #e6f3ff;
|
75 |
+
padding: 10px;
|
76 |
+
border-radius: 10px;
|
77 |
+
margin: 10px 0;
|
78 |
+
max-width: 80%;
|
79 |
+
margin-left: auto;
|
80 |
+
}
|
81 |
+
.bot-message {
|
82 |
+
background-color: #f0f0f0;
|
83 |
+
padding: 10px;
|
84 |
+
border-radius: 10px;
|
85 |
+
margin: 10px 0;
|
86 |
+
max-width: 80%;
|
87 |
+
}
|
88 |
+
.input-container {
|
89 |
+
position: fixed;
|
90 |
+
bottom: 0;
|
91 |
+
left: 0;
|
92 |
+
right: 0;
|
93 |
+
padding: 20px;
|
94 |
+
background-color: white;
|
95 |
+
border-top: 1px solid #ddd;
|
96 |
+
}
|
97 |
+
</style>
|
98 |
+
""", unsafe_allow_html=True)
|
99 |
+
|
100 |
+
# Main title
|
101 |
+
st.title("Financial Summary Chatbot")
|
102 |
+
|
103 |
+
# Chat message container
|
104 |
+
chat_container = st.container()
|
105 |
+
|
106 |
+
# Create a container for the input area at the bottom
|
107 |
+
with st.container():
|
108 |
+
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
109 |
+
|
110 |
+
# Create two columns for input and button
|
111 |
+
col1, col2 = st.columns([5,1])
|
112 |
+
|
113 |
+
with col1:
|
114 |
+
user_input = st.text_input("Message", key="user_input", label_visibility="collapsed")
|
115 |
+
|
116 |
+
with col2:
|
117 |
+
send_button = st.button("Send")
|
118 |
+
|
119 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
120 |
+
|
121 |
+
# Handle sending messages
|
122 |
+
if send_button and user_input.strip():
|
123 |
+
# Add user message to history
|
124 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
125 |
+
|
126 |
+
# Get bot response
|
127 |
+
response = text_summary(user_input)
|
128 |
+
|
129 |
+
# Add bot response to history
|
130 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
131 |
+
|
132 |
+
# Clear input
|
133 |
+
st.session_state.user_input = ""
|
134 |
+
|
135 |
+
# Display chat messages in the container
|
136 |
+
with chat_container:
|
137 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
138 |
+
for message in st.session_state.messages:
|
139 |
+
if message["role"] == "user":
|
140 |
+
st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
|
141 |
+
else:
|
142 |
+
st.markdown(f'<div class="bot-message">{message["content"]}</div>', unsafe_allow_html=True)
|
143 |
+
st.markdown('</div>', unsafe_allow_html=True)
|