Spaces:
Sleeping
Sleeping
Update app2.py
Browse files
app2.py
CHANGED
@@ -1,5 +1,3 @@
|
|
1 |
-
# app.py
|
2 |
-
|
3 |
import streamlit as st
|
4 |
import google.generativeai as genai
|
5 |
from dotenv import load_dotenv
|
@@ -12,8 +10,13 @@ load_dotenv()
|
|
12 |
api_key = os.getenv("GENERATIVEAI_API_KEY")
|
13 |
genai.configure(api_key=api_key)
|
14 |
|
15 |
-
#
|
16 |
-
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
# Generation configuration and safety settings
|
19 |
generation_config = {
|
@@ -42,101 +45,109 @@ safety_settings = [
|
|
42 |
},
|
43 |
]
|
44 |
|
45 |
-
# Function to handle text summary requests
|
46 |
def text_summary(text, isNew=False):
|
47 |
-
|
48 |
-
|
49 |
-
if isNew or chat is None: # Start a new chat session
|
50 |
model = genai.GenerativeModel(
|
51 |
model_name="gemini-pro",
|
52 |
generation_config=generation_config,
|
53 |
safety_settings=safety_settings
|
54 |
)
|
55 |
-
chat = model.start_chat()
|
56 |
-
chat.send_message("""
|
57 |
-
Act as a financial advisor and generate financial summaries in a structured and tabular format
|
58 |
-
|
59 |
-
- Start each section with a clear title in <strong> tags.
|
60 |
-
- For key metrics, use a table with two columns: one for the metric name and one for its value.
|
61 |
-
- Use bullet points only for listing risks and growth prospects.
|
62 |
-
- Ensure each section is clearly separated with line breaks.
|
63 |
-
- Do not use bold or italic formatting (, *), except for the specified HTML tags.
|
64 |
-
|
65 |
-
Example format:
|
66 |
-
|
67 |
-
<strong>Company Overview</strong><br/>
|
68 |
-
<p>Company Name: {Company Name}</p>
|
69 |
-
<p>Description: {Company Description}</p>
|
70 |
-
<br/><br/>
|
71 |
-
|
72 |
-
<strong>Stock Performance</strong><br/>
|
73 |
-
<p>Apple Inc. (AAPL) is a highly valued stock...</p>
|
74 |
-
<br/><br/>
|
75 |
-
|
76 |
-
<strong>Key Metrics</strong><br/>
|
77 |
-
<table>
|
78 |
-
<tr>
|
79 |
-
<th>Metric</th>
|
80 |
-
<th>Value</th>
|
81 |
-
</tr>
|
82 |
-
<tr>
|
83 |
-
<td>Market Capitalization</td>
|
84 |
-
<td>$2.7 trillion</td>
|
85 |
-
</tr>
|
86 |
-
<tr>
|
87 |
-
<td>Stock Price</td>
|
88 |
-
<td>$170 per share</td>
|
89 |
-
</tr>
|
90 |
-
<tr>
|
91 |
-
<td>EPS (TTM)</td>
|
92 |
-
<td>$6.15</td>
|
93 |
-
</tr>
|
94 |
-
<tr>
|
95 |
-
<td>P/E Ratio</td>
|
96 |
-
<td>24.34</td>
|
97 |
-
</tr>
|
98 |
-
</table>
|
99 |
-
<br/><br/>
|
100 |
-
|
101 |
-
<strong>Growth Prospects</strong><br/>
|
102 |
-
<ul>
|
103 |
-
<li>iPhone sales growth in emerging markets.</li>
|
104 |
-
<li>Expansion of services revenue.</li>
|
105 |
-
<li>Increased demand for wearable devices.</li>
|
106 |
-
<li>Development of AR/VR technologies.</li>
|
107 |
-
</ul>
|
108 |
-
<br/><br/>
|
109 |
|
110 |
-
|
111 |
-
<ul>
|
112 |
-
<li>Competition from other technology companies.</li>
|
113 |
-
<li>Dependence on iPhone sales.</li>
|
114 |
-
<li>Economic downturns.</li>
|
115 |
-
<li>Supply chain disruptions and geopolitical risks.</li>
|
116 |
-
</ul>
|
117 |
-
<br/><br/>
|
118 |
-
|
119 |
-
<strong>Overall</strong><br/>
|
120 |
-
<p>Apple Inc. is a financially strong company with a history of innovation...</p>
|
121 |
-
<br/><br/>
|
122 |
-
""")
|
123 |
-
|
124 |
-
# Send message and return response
|
125 |
-
response = chat.send_message(text)
|
126 |
return response.text
|
127 |
|
128 |
-
#
|
129 |
-
st.
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
-
#
|
133 |
-
|
134 |
|
135 |
-
#
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import google.generativeai as genai
|
3 |
from dotenv import load_dotenv
|
|
|
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 |
+
# Initialize session state for chat
|
18 |
+
if 'chat' not in st.session_state:
|
19 |
+
st.session_state.chat = None
|
20 |
|
21 |
# Generation configuration and safety settings
|
22 |
generation_config = {
|
|
|
45 |
},
|
46 |
]
|
47 |
|
|
|
48 |
def text_summary(text, isNew=False):
|
49 |
+
if isNew or st.session_state.chat is None:
|
|
|
|
|
50 |
model = genai.GenerativeModel(
|
51 |
model_name="gemini-pro",
|
52 |
generation_config=generation_config,
|
53 |
safety_settings=safety_settings
|
54 |
)
|
55 |
+
st.session_state.chat = model.start_chat()
|
56 |
+
st.session_state.chat.send_message("""
|
57 |
+
Act as a financial advisor and generate financial summaries in a structured and tabular format...
|
58 |
+
""") # Your existing prompt here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
+
response = st.session_state.chat.send_message(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
return response.text
|
62 |
|
63 |
+
# Custom CSS for chat interface
|
64 |
+
st.markdown("""
|
65 |
+
<style>
|
66 |
+
.chat-container {
|
67 |
+
height: 600px;
|
68 |
+
overflow-y: auto;
|
69 |
+
padding: 20px;
|
70 |
+
margin-bottom: 100px;
|
71 |
+
}
|
72 |
+
.user-message {
|
73 |
+
background-color: #e6f3ff;
|
74 |
+
padding: 10px;
|
75 |
+
border-radius: 10px;
|
76 |
+
margin: 10px 0;
|
77 |
+
max-width: 80%;
|
78 |
+
margin-left: auto;
|
79 |
+
}
|
80 |
+
.bot-message {
|
81 |
+
background-color: #f0f0f0;
|
82 |
+
padding: 10px;
|
83 |
+
border-radius: 10px;
|
84 |
+
margin: 10px 0;
|
85 |
+
max-width: 80%;
|
86 |
+
}
|
87 |
+
.input-container {
|
88 |
+
position: fixed;
|
89 |
+
bottom: 0;
|
90 |
+
left: 0;
|
91 |
+
right: 0;
|
92 |
+
padding: 20px;
|
93 |
+
background-color: white;
|
94 |
+
border-top: 1px solid #ddd;
|
95 |
+
}
|
96 |
+
.stTextInput input {
|
97 |
+
border-radius: 20px;
|
98 |
+
}
|
99 |
+
</style>
|
100 |
+
""", unsafe_allow_html=True)
|
101 |
|
102 |
+
# Main title
|
103 |
+
st.title("Financial Summary Chatbot")
|
104 |
|
105 |
+
# Chat message container
|
106 |
+
chat_container = st.container()
|
107 |
+
|
108 |
+
# Function to process the message
|
109 |
+
def process_message(message: str):
|
110 |
+
if message.strip():
|
111 |
+
# Add user message to history
|
112 |
+
st.session_state.messages.append({"role": "user", "content": message})
|
113 |
+
|
114 |
+
# Get bot response
|
115 |
+
response = text_summary(message)
|
116 |
+
|
117 |
+
# Add bot response to history
|
118 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
119 |
+
|
120 |
+
# Create a container for the input area at the bottom
|
121 |
+
with st.container():
|
122 |
+
st.markdown('<div class="input-container">', unsafe_allow_html=True)
|
123 |
+
|
124 |
+
# Create two columns for input and button
|
125 |
+
col1, col2 = st.columns([5,1])
|
126 |
+
|
127 |
+
with col1:
|
128 |
+
user_input = st.text_input("Message", key="user_input", label_visibility="collapsed")
|
129 |
+
|
130 |
+
with col2:
|
131 |
+
if st.button("Send"):
|
132 |
+
process_message(user_input)
|
133 |
+
# Instead of trying to clear the input directly, we'll use a rerun
|
134 |
+
st.rerun()
|
135 |
+
|
136 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
137 |
+
|
138 |
+
# Handle Enter key press
|
139 |
+
if user_input and len(user_input.strip()) > 0:
|
140 |
+
if '\n' in user_input or st.session_state.get('enter_pressed', False):
|
141 |
+
process_message(user_input)
|
142 |
+
st.session_state.enter_pressed = False
|
143 |
+
st.rerun()
|
144 |
+
|
145 |
+
# Display chat messages in the container
|
146 |
+
with chat_container:
|
147 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
148 |
+
for message in st.session_state.messages:
|
149 |
+
if message["role"] == "user":
|
150 |
+
st.markdown(f'<div class="user-message">{message["content"]}</div>', unsafe_allow_html=True)
|
151 |
+
else:
|
152 |
+
st.markdown(f'<div class="bot-message">{message["content"]}</div>', unsafe_allow_html=True)
|
153 |
+
st.markdown('</div>', unsafe_allow_html=True)
|