-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
83 lines (58 loc) · 2.51 KB
/
main.py
File metadata and controls
83 lines (58 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import os
from dotenv import load_dotenv
load_dotenv()
os.environ["LITELLM_PROVIDER"] = "gemini"
print("GOOGLE_API_KEY =", os.environ.get("GOOGLE_API_KEY"))
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY", "")
import traceback
os.environ["GOOGLE_API_KEY"] = os.getenv("GOOGLE_API_KEY")
from fastapi import FastAPI, File, UploadFile, Form, HTTPException
import uvicorn
import asyncio
from concurrent.futures import ThreadPoolExecutor
from crewai import Crew, Process
from agents import financial_analyst, verifier, investment_advisor, risk_assessor, shared_llm
from task import verification_task, analyze_financial_document, risk_assessment_task, investment_analysis_task
app = FastAPI(title="Financial Document Analyzer")
executor = ThreadPoolExecutor()
def run_crew(query: str, file_path: str):
"""To run the whole crew"""
financial_crew = Crew(
agents=[verifier, financial_analyst, risk_assessor, investment_advisor],
tasks=[verification_task, analyze_financial_document, risk_assessment_task, investment_analysis_task],
process=Process.sequential,
verbose=True,
)
result = financial_crew.kickoff(inputs={'query': query, 'file_path': file_path})
return result
@app.get("/")
async def root():
"""Health check endpoint"""
return {"message": "Financial Document Analyzer API is running"}
@app.post("/analyze")
async def analyze_endpoint(
file: UploadFile = File(...),
query: str = Form(default="Analyze this financial document for investment insights")
):
"""Analyze financial document and provide comprehensive investment recommendations"""
file_path = f"data/{file.filename}"
try:
os.makedirs("data", exist_ok=True)
with open(file_path, "wb") as f:
content = await file.read()
f.write(content)
if not query or query.strip() == "":
query = "Analyze this financial document for investment insights"
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(executor, run_crew, query.strip(), file_path)
return {
"status": "success",
"query": query,
"analysis": str(response),
"file_processed": file.filename
}
except Exception as e:
traceback.print_exc()
raise HTTPException(status_code=500, detail=f"Error processing document: {str(e)}")
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=False)