Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import nltk
|
| 3 |
-
import asyncio
|
| 4 |
|
| 5 |
-
# Use a directory within the user's home
|
| 6 |
nltk_data_dir = os.path.expanduser("~/.nltk_data")
|
| 7 |
os.makedirs(nltk_data_dir, exist_ok=True)
|
| 8 |
nltk.data.path.append(nltk_data_dir)
|
|
@@ -101,106 +100,71 @@ question_prompt = (
|
|
| 101 |
"Unless retrievable from the documents, don't ask questions which cannot be compared to previous periods."
|
| 102 |
)
|
| 103 |
|
| 104 |
-
import asyncio
|
| 105 |
-
|
| 106 |
-
async def send_heartbeat(interval=300):
|
| 107 |
-
while True:
|
| 108 |
-
await asyncio.sleep(interval) # Send heartbeat every 5 minutes
|
| 109 |
-
await cl.Message(content="Still waiting for your input...").send()
|
| 110 |
-
|
| 111 |
@cl.on_chat_start
|
| 112 |
async def on_chat_start():
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
try:
|
| 117 |
-
# Set a timeout for the user input
|
| 118 |
-
ticker_response = await asyncio.wait_for(
|
| 119 |
-
cl.AskUserMessage(
|
| 120 |
-
content="Please enter the ticker symbol for the company you want to analyze:"
|
| 121 |
-
).send(),
|
| 122 |
-
timeout=300 # Timeout after 300 seconds (5 minutes)
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# Cancel the heartbeat once the user provides input
|
| 126 |
-
heartbeat_task.cancel()
|
| 127 |
-
|
| 128 |
-
# Check if ticker_response is None or doesn't contain 'content'
|
| 129 |
-
if ticker_response is None or 'content' not in ticker_response:
|
| 130 |
-
await cl.Message(content="No input received. Please restart and enter a valid ticker symbol.").send()
|
| 131 |
-
return
|
| 132 |
|
| 133 |
-
|
| 134 |
-
if not ticker_symbol:
|
| 135 |
-
await cl.Message(content="No valid ticker symbol provided. Please restart and enter a valid ticker symbol.").send()
|
| 136 |
-
return
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
# Extract company information
|
| 146 |
-
company_info = company.info
|
| 147 |
-
|
| 148 |
-
# Extract analyst price targets
|
| 149 |
-
analysts_target = company.analyst_price_targets
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
quarterly_balance_sheet = company.quarterly_balance_sheet
|
| 154 |
-
quarterly_cash_flow = company.quarterly_cashflow
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
company_info, analysts_target, quarterly_income_statement,
|
| 159 |
-
quarterly_balance_sheet, quarterly_cash_flow
|
| 160 |
-
)
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
questions_format = str(questions_response).split('\n')
|
| 181 |
-
relevant_questions = [question.strip() for question in questions_format if question.strip() and question.strip()[0].isdigit()]
|
| 182 |
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
await cl.Message(content=f"**{question}**\n{response}").send()
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
|
|
|
|
|
|
| 191 |
|
| 192 |
-
|
| 193 |
-
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
heartbeat_task.cancel()
|
| 198 |
-
await cl.Message(content="Session timed out due to inactivity. Please restart and enter a ticker symbol.").send()
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
@cl.on_message
|
| 205 |
async def main(message: cl.Message):
|
| 206 |
index = cl.user_session.get("index")
|
|
|
|
| 1 |
import os
|
| 2 |
import nltk
|
|
|
|
| 3 |
|
| 4 |
+
# Use a directory within the user's home directory
|
| 5 |
nltk_data_dir = os.path.expanduser("~/.nltk_data")
|
| 6 |
os.makedirs(nltk_data_dir, exist_ok=True)
|
| 7 |
nltk.data.path.append(nltk_data_dir)
|
|
|
|
| 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):
|
| 170 |
index = cl.user_session.get("index")
|