QuantumLearner commited on
Commit
6c1639c
·
verified ·
1 Parent(s): 9606de2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -46
app.py CHANGED
@@ -100,70 +100,99 @@ question_prompt = (
100
  "Unless retrievable from the documents, don't ask questions which cannot be compared to previous periods."
101
  )
102
 
 
 
 
 
 
103
  @cl.on_chat_start
104
  async def on_chat_start():
105
- ticker_response = await cl.AskUserMessage(
106
- content="Please enter the ticker symbol for the company you want to analyze:"
107
- ).send()
108
 
109
- ticker_symbol = ticker_response['content'].upper()
 
 
 
 
 
 
 
110
 
111
- msg = cl.Message(content=f"Retrieving financial data for {ticker_symbol}...")
112
- await msg.send()
113
 
114
- try:
115
- # Get the data for the company
116
- company = yf.Ticker(ticker_symbol)
 
 
117
 
118
- # Extract company information
119
- company_info = company.info
120
 
121
- # Extract analyst price targets
122
- analysts_target = company.analyst_price_targets
 
123
 
124
- # Retrieve the Quarterly Financial Statements
125
- quarterly_income_statement = company.quarterly_financials
126
- quarterly_balance_sheet = company.quarterly_balance_sheet
127
- quarterly_cash_flow = company.quarterly_cashflow
128
 
129
- # Generate the formatted financial summary
130
- financial_summary = format_financial_data(
131
- company_info, analysts_target, quarterly_income_statement,
132
- quarterly_balance_sheet, quarterly_cash_flow
133
- )
134
 
135
- # Create a Document object with the financial summary
136
- document = Document(text=financial_summary, metadata={"company": ticker_symbol})
 
 
137
 
138
- # Create index
139
- index = VectorStoreIndex.from_documents(
140
- [document], service_context=service_context
141
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
 
143
- # Store the index in the user session
144
- cl.user_session.set("index", index)
 
 
145
 
146
- # Generate summary
147
- query_engine = index.as_query_engine()
148
- summary_response = await cl.make_async(query_engine.query)(summary_prompt)
149
- await cl.Message(content=f"**Summary:**\n{summary_response}").send()
 
150
 
151
- # Generate questions
152
- questions_response = await cl.make_async(query_engine.query)(question_prompt)
153
- questions_format = str(questions_response).split('\n')
154
- relevant_questions = [question.strip() for question in questions_format if question.strip() and question.strip()[0].isdigit()]
155
 
156
- # Answer generated questions
157
- await cl.Message(content="Generated questions and answers:").send()
158
- for question in relevant_questions:
159
- response = await cl.make_async(query_engine.query)(question)
160
- await cl.Message(content=f"**{question}**\n{response}").send()
161
 
162
- msg.content = "Processing done. You can now ask more questions about the financial data!"
163
- await msg.update()
 
 
164
 
165
  except Exception as e:
166
- await cl.Message(content=f"An error occurred during processing: {str(e)}").send()
 
167
 
168
  @cl.on_message
169
  async def main(message: cl.Message):
 
100
  "Unless retrievable from the documents, don't ask questions which cannot be compared to previous periods."
101
  )
102
 
103
+ async def send_heartbeat(interval=300):
104
+ while True:
105
+ await asyncio.sleep(interval) # Send heartbeat every 5 minutes
106
+ await cl.Message(content="Still waiting for your input...").send()
107
+
108
  @cl.on_chat_start
109
  async def on_chat_start():
110
+ # Start the heartbeat task
111
+ heartbeat_task = asyncio.create_task(send_heartbeat())
 
112
 
113
+ try:
114
+ # Set a timeout for the user input
115
+ ticker_response = await asyncio.wait_for(
116
+ cl.AskUserMessage(
117
+ content="Please enter the ticker symbol for the company you want to analyze:"
118
+ ).send(),
119
+ timeout=300 # Timeout after 300 seconds (5 minutes)
120
+ )
121
 
122
+ # Cancel the heartbeat once the user provides input
123
+ heartbeat_task.cancel()
124
 
125
+ # Handle the ticker response
126
+ ticker_symbol = ticker_response['content'].strip().upper()
127
+ if not ticker_symbol:
128
+ await cl.Message(content="No valid ticker symbol provided. Please restart and enter a valid ticker symbol.").send()
129
+ return
130
 
131
+ msg = cl.Message(content=f"Retrieving financial data for {ticker_symbol}...")
132
+ await msg.send()
133
 
134
+ try:
135
+ # Get the data for the company
136
+ company = yf.Ticker(ticker_symbol)
137
 
138
+ # Extract company information
139
+ company_info = company.info
 
 
140
 
141
+ # Extract analyst price targets
142
+ analysts_target = company.analyst_price_targets
 
 
 
143
 
144
+ # Retrieve the Quarterly Financial Statements
145
+ quarterly_income_statement = company.quarterly_financials
146
+ quarterly_balance_sheet = company.quarterly_balance_sheet
147
+ quarterly_cash_flow = company.quarterly_cashflow
148
 
149
+ # Generate the formatted financial summary
150
+ financial_summary = format_financial_data(
151
+ company_info, analysts_target, quarterly_income_statement,
152
+ quarterly_balance_sheet, quarterly_cash_flow
153
+ )
154
+
155
+ # Create a Document object with the financial summary
156
+ document = Document(text=financial_summary, metadata={"company": ticker_symbol})
157
+
158
+ # Create index
159
+ index = VectorStoreIndex.from_documents(
160
+ [document], service_context=service_context
161
+ )
162
+
163
+ # Store the index in the user session
164
+ cl.user_session.set("index", index)
165
+
166
+ # Generate summary
167
+ query_engine = index.as_query_engine()
168
+ summary_response = await cl.make_async(query_engine.query)(summary_prompt)
169
+ await cl.Message(content=f"**Summary:**\n{summary_response}").send()
170
 
171
+ # Generate questions
172
+ questions_response = await cl.make_async(query_engine.query)(question_prompt)
173
+ questions_format = str(questions_response).split('\n')
174
+ relevant_questions = [question.strip() for question in questions_format if question.strip() and question.strip()[0].isdigit()]
175
 
176
+ # Answer generated questions
177
+ await cl.Message(content="Generated questions and answers:").send()
178
+ for question in relevant_questions:
179
+ response = await cl.make_async(query_engine.query)(question)
180
+ await cl.Message(content=f"**{question}**\n{response}").send()
181
 
182
+ msg.content = "Processing done. You can now ask more questions about the financial data!"
183
+ await msg.update()
 
 
184
 
185
+ except Exception as e:
186
+ await cl.Message(content=f"An error occurred during processing: {str(e)}").send()
 
 
 
187
 
188
+ except asyncio.TimeoutError:
189
+ # Handle timeout if no input is provided within 5 minutes
190
+ heartbeat_task.cancel()
191
+ await cl.Message(content="Session timed out due to inactivity. Please restart and enter a ticker symbol.").send()
192
 
193
  except Exception as e:
194
+ heartbeat_task.cancel()
195
+ await cl.Message(content=f"An error occurred: {str(e)}").send()
196
 
197
  @cl.on_message
198
  async def main(message: cl.Message):