Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
import streamlit as st
|
4 |
+
import google.generativeai as genai
|
5 |
+
from dotenv import load_dotenv
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Load environment variables
|
9 |
+
load_dotenv()
|
10 |
+
|
11 |
+
# Configure Google Generative AI with API key
|
12 |
+
api_key = os.getenv("GENERATIVEAI_API_KEY")
|
13 |
+
genai.configure(api_key=api_key)
|
14 |
+
|
15 |
+
# Global variable to maintain chat session
|
16 |
+
chat = None
|
17 |
+
|
18 |
+
# Generation configuration and safety settings
|
19 |
+
generation_config = {
|
20 |
+
"temperature": 0.9,
|
21 |
+
"top_p": 0.5,
|
22 |
+
"top_k": 5,
|
23 |
+
"max_output_tokens": 1000,
|
24 |
+
}
|
25 |
+
|
26 |
+
safety_settings = [
|
27 |
+
{
|
28 |
+
"category": "HARM_CATEGORY_HARASSMENT",
|
29 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
30 |
+
},
|
31 |
+
{
|
32 |
+
"category": "HARM_CATEGORY_HATE_SPEECH",
|
33 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
34 |
+
},
|
35 |
+
{
|
36 |
+
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
|
37 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
38 |
+
},
|
39 |
+
{
|
40 |
+
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
|
41 |
+
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
|
42 |
+
},
|
43 |
+
]
|
44 |
+
|
45 |
+
# Function to handle text summary requests
|
46 |
+
def text_summary(text, isNew=False):
|
47 |
+
global chat
|
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. Follow these guidelines strictly:
|
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 |
+
<strong>Risks</strong><br/>
|
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 |
+
# Streamlit UI
|
129 |
+
st.title("Financial Summary Chatbot")
|
130 |
+
st.write("Welcome to the Financial Summary Chatbot! Enter your text below to get a financial summary.")
|
131 |
+
|
132 |
+
# Input area for user text
|
133 |
+
user_input = st.text_area("Enter your text here:", "")
|
134 |
+
|
135 |
+
# Button to submit the text
|
136 |
+
if st.button("Get Summary"):
|
137 |
+
if user_input.strip():
|
138 |
+
summary = text_summary(user_input, isNew=True) # Start a new session each time
|
139 |
+
st.write("### Summary:")
|
140 |
+
st.markdown(summary, unsafe_allow_html=True)
|
141 |
+
else:
|
142 |
+
st.warning("Please enter some text to summarize.")
|
143 |
+
|
144 |
+
if __name__ == '__main__':
|
145 |
+
# Start the Streamlit app
|
146 |
+
st.run()
|