Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,16 +7,11 @@ import os
|
|
7 |
load_dotenv()
|
8 |
|
9 |
# Configure Google Generative AI with API key
|
10 |
-
api_key = os.getenv("
|
11 |
genai.configure(api_key=api_key)
|
12 |
|
13 |
-
#
|
14 |
-
|
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,88 +40,101 @@ safety_settings = [
|
|
45 |
},
|
46 |
]
|
47 |
|
|
|
48 |
def text_summary(text, isNew=False):
|
49 |
-
|
|
|
|
|
50 |
model = genai.GenerativeModel(
|
51 |
model_name="gemini-pro",
|
52 |
generation_config=generation_config,
|
53 |
safety_settings=safety_settings
|
54 |
)
|
55 |
-
|
56 |
-
|
57 |
-
Act as a financial advisor and generate financial summaries in a structured and tabular format
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
return response.text
|
62 |
|
63 |
-
#
|
64 |
-
st.
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
.
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
}
|
79 |
-
/* Scrollable container */
|
80 |
-
.scrollable-container {
|
81 |
-
max-height: 400px; /* Set a max height for scrolling */
|
82 |
-
overflow-y: auto; /* Enable vertical scrolling */
|
83 |
-
}
|
84 |
-
/* Message styling */
|
85 |
-
.message-container {
|
86 |
-
background-color: #444444; /* Dark gray for messages */
|
87 |
-
border-radius: 8px;
|
88 |
-
padding: 10px; /* Padding for messages */
|
89 |
-
margin: 5px 0; /* Margin for messages */
|
90 |
-
}
|
91 |
-
</style>
|
92 |
-
""", unsafe_allow_html=True)
|
93 |
-
|
94 |
-
# Sidebar for input and button
|
95 |
-
with st.sidebar:
|
96 |
-
|
97 |
-
user_input = st.text_area("", height=150, label_visibility="collapsed")
|
98 |
-
if st.button("Generate Summary", key="send_button"):
|
99 |
-
if user_input.strip():
|
100 |
-
# Get bot response
|
101 |
-
response = text_summary(user_input)
|
102 |
-
# Add to message history
|
103 |
-
st.session_state.messages.append({
|
104 |
-
"input": user_input,
|
105 |
-
"response": response
|
106 |
-
})
|
107 |
-
st.rerun()
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
for message in reversed(st.session_state.messages): # Show newest messages first
|
112 |
-
st.markdown("**Input Text:**")
|
113 |
-
st.text(message["input"][:200] + "..." if len(message["input"]) > 200 else message["input"])
|
114 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
# Text area for storing all generated summaries
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
# Join all generated summaries into a single string
|
123 |
-
all_summaries = "\n\n".join(message["response"] for message in st.session_state.messages)
|
124 |
-
|
125 |
-
# Display the summaries in a text area
|
126 |
-
st.text_area("Generated Summaries", value=all_summaries, height=300, disabled=True)
|
127 |
-
|
128 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
129 |
-
|
130 |
-
# Initialize the chat if it's not already initialized
|
131 |
-
if st.session_state.chat is None:
|
132 |
-
text_summary("", isNew=True)
|
|
|
7 |
load_dotenv()
|
8 |
|
9 |
# Configure Google Generative AI with API key
|
10 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
11 |
genai.configure(api_key=api_key)
|
12 |
|
13 |
+
# Global variable to maintain chat session
|
14 |
+
chat = None
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Generation configuration and safety settings
|
17 |
generation_config = {
|
|
|
40 |
},
|
41 |
]
|
42 |
|
43 |
+
# Function to handle text summary requests
|
44 |
def text_summary(text, isNew=False):
|
45 |
+
global chat
|
46 |
+
|
47 |
+
if isNew or chat is None: # Start a new chat session
|
48 |
model = genai.GenerativeModel(
|
49 |
model_name="gemini-pro",
|
50 |
generation_config=generation_config,
|
51 |
safety_settings=safety_settings
|
52 |
)
|
53 |
+
chat = model.start_chat()
|
54 |
+
chat.send_message("""
|
55 |
+
Act as a financial advisor and generate financial summaries in a structured and tabular format. Follow these guidelines strictly:
|
56 |
+
|
57 |
+
- Start each section with a clear title in <strong> tags.
|
58 |
+
- For key metrics, use a table with two columns: one for the metric name and one for its value.
|
59 |
+
- Use bullet points only for listing risks and growth prospects.
|
60 |
+
- Ensure each section is clearly separated with line breaks.
|
61 |
+
- Do not use bold or italic formatting (, *), except for the specified HTML tags.
|
62 |
+
|
63 |
+
Example format:
|
64 |
+
|
65 |
+
<strong>Company Overview</strong><br/>
|
66 |
+
<p>Company Name: {Company Name}</p>
|
67 |
+
<p>Description: {Company Description}</p>
|
68 |
+
<br/><br/>
|
69 |
+
|
70 |
+
<strong>Stock Performance</strong><br/>
|
71 |
+
<p>Apple Inc. (AAPL) is a highly valued stock...</p>
|
72 |
+
<br/><br/>
|
73 |
+
|
74 |
+
<strong>Key Metrics</strong><br/>
|
75 |
+
<table>
|
76 |
+
<tr>
|
77 |
+
<th>Metric</th>
|
78 |
+
<th>Value</th>
|
79 |
+
</tr>
|
80 |
+
<tr>
|
81 |
+
<td>Market Capitalization</td>
|
82 |
+
<td>$2.7 trillion</td>
|
83 |
+
</tr>
|
84 |
+
<tr>
|
85 |
+
<td>Stock Price</td>
|
86 |
+
<td>$170 per share</td>
|
87 |
+
</tr>
|
88 |
+
<tr>
|
89 |
+
<td>EPS (TTM)</td>
|
90 |
+
<td>$6.15</td>
|
91 |
+
</tr>
|
92 |
+
<tr>
|
93 |
+
<td>P/E Ratio</td>
|
94 |
+
<td>24.34</td>
|
95 |
+
</tr>
|
96 |
+
</table>
|
97 |
+
<br/><br/>
|
98 |
+
|
99 |
+
<strong>Growth Prospects</strong><br/>
|
100 |
+
<ul>
|
101 |
+
<li>iPhone sales growth in emerging markets.</li>
|
102 |
+
<li>Expansion of services revenue.</li>
|
103 |
+
<li>Increased demand for wearable devices.</li>
|
104 |
+
<li>Development of AR/VR technologies.</li>
|
105 |
+
</ul>
|
106 |
+
<br/><br/>
|
107 |
+
|
108 |
+
<strong>Risks</strong><br/>
|
109 |
+
<ul>
|
110 |
+
<li>Competition from other technology companies.</li>
|
111 |
+
<li>Dependence on iPhone sales.</li>
|
112 |
+
<li>Economic downturns.</li>
|
113 |
+
<li>Supply chain disruptions and geopolitical risks.</li>
|
114 |
+
</ul>
|
115 |
+
<br/><br/>
|
116 |
+
|
117 |
+
<strong>Overall</strong><br/>
|
118 |
+
<p>Apple Inc. is a financially strong company with a history of innovation...</p>
|
119 |
+
<br/><br/>
|
120 |
+
""")
|
121 |
+
|
122 |
+
# Send message and return response
|
123 |
+
response = chat.send_message(text)
|
124 |
return response.text
|
125 |
|
126 |
+
# Streamlit app layout
|
127 |
+
st.title("Financial Summary Generator")
|
128 |
+
st.write("Enter the text you want summarized and select if you want to start a new session.")
|
129 |
+
|
130 |
+
# Input text and options
|
131 |
+
input_text = st.text_area("Enter text to summarize:")
|
132 |
+
start_new = st.checkbox("Start new session", value=False)
|
133 |
+
|
134 |
+
if st.button("Generate Summary"):
|
135 |
+
if input_text:
|
136 |
+
# Generate the summary using the text_summary function
|
137 |
+
result = text_summary(input_text, start_new)
|
138 |
+
st.markdown(result, unsafe_allow_html=True)
|
139 |
+
else:
|
140 |
+
st.write("Please enter some text to summarize.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|