Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -36,30 +36,23 @@ service_context = ServiceContext.from_defaults(
|
|
| 36 |
node_parser=SentenceSplitter(chunk_size=1000, chunk_overlap=200)
|
| 37 |
)
|
| 38 |
|
| 39 |
-
def
|
| 40 |
"""
|
| 41 |
-
Fetch the latest
|
|
|
|
| 42 |
Args:
|
| 43 |
- symbol (str): The stock ticker symbol (e.g., 'AAPL').
|
|
|
|
| 44 |
Returns:
|
| 45 |
-
- str: The
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
try:
|
| 50 |
-
response = requests.get(
|
| 51 |
response.raise_for_status()
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
if not transcript_data:
|
| 55 |
-
return f"No transcript available for {symbol}."
|
| 56 |
-
|
| 57 |
-
# Extract the first available transcript
|
| 58 |
-
latest_transcript = transcript_data[0].get("content", "")
|
| 59 |
-
if not latest_transcript:
|
| 60 |
-
return f"No transcript content found for {symbol}."
|
| 61 |
-
|
| 62 |
-
return latest_transcript
|
| 63 |
|
| 64 |
except requests.exceptions.HTTPError as http_err:
|
| 65 |
return f"HTTP error occurred: {http_err}"
|
|
@@ -70,26 +63,24 @@ def fetch_earnings_transcript(symbol: str) -> str:
|
|
| 70 |
|
| 71 |
# Prompts
|
| 72 |
summary_prompt = (
|
| 73 |
-
"You are a world-class financial analyst with extensive experience analyzing
|
| 74 |
-
"
|
| 75 |
-
"Answer in extensive bullet points
|
| 76 |
)
|
| 77 |
|
| 78 |
question_prompt = (
|
| 79 |
-
"You are a financial analyst with extensive experience analyzing
|
| 80 |
-
"Read the
|
| 81 |
-
"Ask questions that
|
| 82 |
-
"
|
| 83 |
-
"Please format the questions as a list with a simple '1. Question 1', '2. Question 2', etc. structure. "
|
| 84 |
-
"Unless retrievable from the documents, don't ask questions which cannot be compared to previous periods."
|
| 85 |
)
|
| 86 |
|
| 87 |
@cl.on_chat_start
|
| 88 |
async def on_chat_start():
|
| 89 |
ticker_response = await cl.AskUserMessage(
|
| 90 |
content=(
|
| 91 |
-
"This tool is designed to analyze
|
| 92 |
-
"Provide the company's ticker symbol, and the tool will fetch the latest
|
| 93 |
"It generates summaries and strategic due diligence.\n\n"
|
| 94 |
"Please enter the ticker symbol for the company you want to analyze:"
|
| 95 |
)
|
|
@@ -97,23 +88,22 @@ async def on_chat_start():
|
|
| 97 |
|
| 98 |
ticker_symbol = ticker_response['content'].upper()
|
| 99 |
|
| 100 |
-
msg = cl.Message(content=f"Retrieving
|
| 101 |
await msg.send()
|
| 102 |
|
| 103 |
try:
|
| 104 |
-
# Fetch the
|
| 105 |
-
|
| 106 |
|
| 107 |
# Check if an error message was returned
|
| 108 |
-
if
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
await cl.Message(content=transcript_text).send()
|
| 113 |
return
|
| 114 |
|
| 115 |
-
# Create a Document object with the
|
| 116 |
-
document = Document(text=
|
| 117 |
|
| 118 |
# Create index
|
| 119 |
index = VectorStoreIndex.from_documents(
|
|
@@ -133,13 +123,12 @@ async def on_chat_start():
|
|
| 133 |
questions_format = str(questions_response).split('\n')
|
| 134 |
relevant_questions = [question.strip() for question in questions_format if question.strip() and question.strip()[0].isdigit()]
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
await cl.Message(content="Generated
|
| 138 |
for question in relevant_questions:
|
| 139 |
-
|
| 140 |
-
await cl.Message(content=f"**{question}**\n{response}").send()
|
| 141 |
|
| 142 |
-
msg.content = "Processing done. You can now ask more questions about the
|
| 143 |
await msg.update()
|
| 144 |
|
| 145 |
except Exception as e:
|
|
|
|
| 36 |
node_parser=SentenceSplitter(chunk_size=1000, chunk_overlap=200)
|
| 37 |
)
|
| 38 |
|
| 39 |
+
def fetch_annual_report_10k(symbol: str) -> str:
|
| 40 |
"""
|
| 41 |
+
Fetch the latest annual report on Form 10-K for a specific company.
|
| 42 |
+
|
| 43 |
Args:
|
| 44 |
- symbol (str): The stock ticker symbol (e.g., 'AAPL').
|
| 45 |
+
|
| 46 |
Returns:
|
| 47 |
+
- str: The entire JSON response as a string or an error message.
|
| 48 |
"""
|
| 49 |
+
current_year = datetime.datetime.now().year
|
| 50 |
+
url = f"https://financialmodelingprep.com/api/v4/financial-reports-json?symbol={symbol}&year={current_year}&period=FY&apikey={FMP_API_KEY}"
|
| 51 |
+
|
| 52 |
try:
|
| 53 |
+
response = requests.get(url, timeout=10)
|
| 54 |
response.raise_for_status()
|
| 55 |
+
return response.text # Return the full API response as a string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
except requests.exceptions.HTTPError as http_err:
|
| 58 |
return f"HTTP error occurred: {http_err}"
|
|
|
|
| 63 |
|
| 64 |
# Prompts
|
| 65 |
summary_prompt = (
|
| 66 |
+
"You are a world-class financial analyst with extensive experience analyzing annual reports. "
|
| 67 |
+
"Provide a comprehensive summary of the 10-K report. Focus on Strategic Insights, Key Financial Figures, and Risk Factors. "
|
| 68 |
+
"Answer in extensive bullet points, summarizing the company's performance, strengths, and weaknesses."
|
| 69 |
)
|
| 70 |
|
| 71 |
question_prompt = (
|
| 72 |
+
"You are a financial analyst with extensive experience analyzing annual reports. "
|
| 73 |
+
"Read the 10-K report and generate 10 strategic questions focusing on the company's performance, risks, and financial figures. "
|
| 74 |
+
"Ask questions that provide strategic insights into the company's long-term goals, revenue trends, competitive position, and more. "
|
| 75 |
+
"Format the questions as a numbered list (e.g., '1. Question')."
|
|
|
|
|
|
|
| 76 |
)
|
| 77 |
|
| 78 |
@cl.on_chat_start
|
| 79 |
async def on_chat_start():
|
| 80 |
ticker_response = await cl.AskUserMessage(
|
| 81 |
content=(
|
| 82 |
+
"This tool is designed to analyze 10-K annual reports for publicly traded companies. "
|
| 83 |
+
"Provide the company's ticker symbol, and the tool will fetch the latest available 10-K report. "
|
| 84 |
"It generates summaries and strategic due diligence.\n\n"
|
| 85 |
"Please enter the ticker symbol for the company you want to analyze:"
|
| 86 |
)
|
|
|
|
| 88 |
|
| 89 |
ticker_symbol = ticker_response['content'].upper()
|
| 90 |
|
| 91 |
+
msg = cl.Message(content=f"Retrieving the latest 10-K report for {ticker_symbol}...")
|
| 92 |
await msg.send()
|
| 93 |
|
| 94 |
try:
|
| 95 |
+
# Fetch the 10-K report using the adjusted function
|
| 96 |
+
annual_report_text = fetch_annual_report_10k(ticker_symbol)
|
| 97 |
|
| 98 |
# Check if an error message was returned
|
| 99 |
+
if annual_report_text.startswith("HTTP error") or \
|
| 100 |
+
annual_report_text.startswith("Request error") or \
|
| 101 |
+
annual_report_text.startswith("An unexpected error occurred"):
|
| 102 |
+
await cl.Message(content=annual_report_text).send()
|
|
|
|
| 103 |
return
|
| 104 |
|
| 105 |
+
# Create a Document object with the raw JSON response
|
| 106 |
+
document = Document(text=annual_report_text, metadata={"company": ticker_symbol})
|
| 107 |
|
| 108 |
# Create index
|
| 109 |
index = VectorStoreIndex.from_documents(
|
|
|
|
| 123 |
questions_format = str(questions_response).split('\n')
|
| 124 |
relevant_questions = [question.strip() for question in questions_format if question.strip() and question.strip()[0].isdigit()]
|
| 125 |
|
| 126 |
+
# Display generated questions
|
| 127 |
+
await cl.Message(content="Generated strategic questions:").send()
|
| 128 |
for question in relevant_questions:
|
| 129 |
+
await cl.Message(content=f"**{question}**").send()
|
|
|
|
| 130 |
|
| 131 |
+
msg.content = "Processing done. You can now ask more questions about the 10-K report!"
|
| 132 |
await msg.update()
|
| 133 |
|
| 134 |
except Exception as e:
|