Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
import PyPDF2
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
from io import BytesIO
|
6 |
+
from llama_index.core import Settings, VectorStoreIndex, SimpleDirectoryReader
|
7 |
+
from llama_index.embeddings.fastembed import FastEmbedEmbedding
|
8 |
+
from llama_index.llms.gemini import Gemini
|
9 |
+
import re
|
10 |
+
from crewai import Agent, Task, Crew, Process
|
11 |
+
import json
|
12 |
+
|
13 |
+
# Configure Google Gemini
|
14 |
+
Settings.embed_model = FastEmbedEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
15 |
+
Settings.llm = Gemini(api_key=os.getenv("GOOGLE_API_KEY"), temperature=0.5, model_name="models/gemini-pro")
|
16 |
+
|
17 |
+
class FinAnalyst:
|
18 |
+
def __init__(self):
|
19 |
+
self.configure_agents()
|
20 |
+
|
21 |
+
def configure_agents(self):
|
22 |
+
self.document_processor = Agent(
|
23 |
+
role='Document Processor',
|
24 |
+
goal='Process and extract text from financial documents',
|
25 |
+
backstory='Expert in handling various document formats and extracting relevant information',
|
26 |
+
allow_delegation=False
|
27 |
+
)
|
28 |
+
|
29 |
+
self.data_extractor = Agent(
|
30 |
+
role='Data Extractor',
|
31 |
+
goal='Extract key financial data from processed documents',
|
32 |
+
backstory='Specialist in identifying and parsing financial information from text',
|
33 |
+
allow_delegation=False
|
34 |
+
)
|
35 |
+
|
36 |
+
self.financial_analyst = Agent(
|
37 |
+
role='Financial Analyst',
|
38 |
+
goal='Analyze financial data and provide insightful summaries',
|
39 |
+
backstory='Experienced financial expert with deep knowledge of Fortune 500 companies',
|
40 |
+
allow_delegation=False
|
41 |
+
)
|
42 |
+
|
43 |
+
self.data_visualizer = Agent(
|
44 |
+
role='Data Visualizer',
|
45 |
+
goal='Create visual representations of financial data',
|
46 |
+
backstory='Expert in data visualization techniques and financial charting',
|
47 |
+
allow_delegation=False
|
48 |
+
)
|
49 |
+
|
50 |
+
def write_to_file(self, content, filename="./files/uploaded.pdf"):
|
51 |
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
52 |
+
with open(filename, "wb") as f:
|
53 |
+
f.write(content)
|
54 |
+
|
55 |
+
def process_document(self, file_content):
|
56 |
+
task = Task(
|
57 |
+
description="Process the uploaded financial document and extract its text content",
|
58 |
+
agent=self.document_processor
|
59 |
+
)
|
60 |
+
return task.execute(file_content)
|
61 |
+
|
62 |
+
def extract_financial_data(self, document_text):
|
63 |
+
task = Task(
|
64 |
+
description="Extract key financial data from the document text. Focus on revenue figures and corresponding dates. Return the data as a JSON string with 'Revenue' and 'Date' lists.",
|
65 |
+
agent=self.data_extractor
|
66 |
+
)
|
67 |
+
return task.execute(document_text)
|
68 |
+
|
69 |
+
def analyze_financials(self, financial_data, query):
|
70 |
+
task = Task(
|
71 |
+
description=f"Analyze the financial data and answer the query: {query}. Provide a comprehensive analysis covering revenue trends, key metrics, major events, period comparisons, future outlook, and potential risks/opportunities.",
|
72 |
+
agent=self.financial_analyst
|
73 |
+
)
|
74 |
+
return task.execute(financial_data)
|
75 |
+
|
76 |
+
def visualize_data(self, financial_data):
|
77 |
+
task = Task(
|
78 |
+
description="Create a revenue comparison graph based on the financial data. Return the plot as a base64 encoded string.",
|
79 |
+
agent=self.data_visualizer
|
80 |
+
)
|
81 |
+
return task.execute(financial_data)
|
82 |
+
|
83 |
+
def run(self):
|
84 |
+
st.title("FinAnalyst: Fortune 500 Financial Document Analyzer")
|
85 |
+
st.write("Upload a financial document, ask questions, and get detailed analysis!")
|
86 |
+
|
87 |
+
uploaded_file = st.file_uploader("Choose a financial document file", type=["pdf"])
|
88 |
+
|
89 |
+
if uploaded_file is not None:
|
90 |
+
file_content = uploaded_file.getvalue()
|
91 |
+
self.write_to_file(file_content)
|
92 |
+
|
93 |
+
st.write("Analyzing financial document...")
|
94 |
+
|
95 |
+
document_text = self.process_document(file_content)
|
96 |
+
financial_data = self.extract_financial_data(document_text)
|
97 |
+
|
98 |
+
# Parse the JSON string to a Python dictionary
|
99 |
+
financial_dict = json.loads(financial_data)
|
100 |
+
|
101 |
+
query = st.text_input("Enter your financial analysis query (e.g., 'What are the revenue trends?')", "")
|
102 |
+
|
103 |
+
if query:
|
104 |
+
analysis = self.analyze_financials(financial_data, query)
|
105 |
+
st.write("## Financial Analysis Result")
|
106 |
+
st.write(analysis)
|
107 |
+
|
108 |
+
st.write("## Revenue Comparison")
|
109 |
+
if financial_dict["Revenue"] and financial_dict["Date"]:
|
110 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
111 |
+
ax.plot(financial_dict["Date"], financial_dict["Revenue"], marker="o", linestyle="-", color="b", label="Revenue")
|
112 |
+
ax.set_title("Revenue Comparison")
|
113 |
+
ax.set_xlabel("Date")
|
114 |
+
ax.set_ylabel("Revenue (in millions)")
|
115 |
+
ax.grid(True)
|
116 |
+
ax.legend()
|
117 |
+
plt.xticks(rotation=45, ha="right")
|
118 |
+
plt.tight_layout()
|
119 |
+
st.pyplot(fig)
|
120 |
+
else:
|
121 |
+
st.write("No revenue data found for comparison.")
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
fin_analyst = FinAnalyst()
|
125 |
+
fin_analyst.run()
|