-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenhanced_backend.py
More file actions
246 lines (207 loc) · 8.01 KB
/
Copy pathenhanced_backend.py
File metadata and controls
246 lines (207 loc) · 8.01 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env python3
"""
Enhanced Startup Analyst Platform
Integrates existing startup analysis with Smart Report Analyzer features
"""
import os
import sys
import time
import logging
from typing import Dict, Any, Optional
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from pydantic import BaseModel
import pandas as pd
# Add current directory to Python path
sys.path.append('.')
sys.path.append('/Users/sanjay/google')
# Import working components
from agents.startup_analyst_agents import StartupAnalystOrchestrator, StartupData
# Import Smart Report Analyzer utilities
try:
from src.utils.file_handler import load_file
from src.utils.llm_agent import summarize_report, ask_question
from src.utils.eda import generate_eda_report
SMART_ANALYZER_AVAILABLE = True
except ImportError:
SMART_ANALYZER_AVAILABLE = False
logging.warning("Smart Analyzer utilities not available")
# Set up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(title="Enhanced Startup Analyst Platform", version="2.0.0")
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize orchestrator
orchestrator = StartupAnalystOrchestrator()
# Pydantic models
class StartupInput(BaseModel):
company_name: str
business_description: str
industry: Optional[str] = None
stage: Optional[str] = None
founder_name: Optional[str] = None
website: Optional[str] = None
pitch_deck_url: Optional[str] = None
additional_info: Optional[str] = None
class DocumentAnalysisRequest(BaseModel):
analysis_type: str # "pitch_deck", "financial_data", "business_plan"
questions: Optional[list] = []
# Mount static files for the frontend
try:
app.mount("/", StaticFiles(directory="frontend/build", html=True), name="static")
logger.info("✅ Frontend static files mounted from frontend/build")
except Exception as e:
logger.warning(f"⚠️ Could not mount frontend files: {e}")
@app.get("/api/health")
async def health_check():
"""Health check endpoint"""
return {
"status": "healthy",
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"version": "2.0.0",
"features": {
"startup_analysis": True,
"smart_analyzer": SMART_ANALYZER_AVAILABLE,
"document_processing": SMART_ANALYZER_AVAILABLE
}
}
@app.get("/api/status")
async def get_status():
"""Get system status"""
return {
"status": "ready",
"platform": "Enhanced Startup Analyst Platform",
"agents_available": len(orchestrator.agents),
"smart_analyzer": SMART_ANALYZER_AVAILABLE,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}
@app.post("/api/analyze")
async def analyze_startup(startup_input: StartupInput):
"""Analyze a startup using AI agents"""
try:
logger.info(f"Starting analysis for {startup_input.company_name}")
# Convert to StartupData format
startup_data = StartupData(
company_name=startup_input.company_name,
founder_name=startup_input.founder_name or "Unknown",
business_description=startup_input.business_description,
pitch_deck_url=startup_input.pitch_deck_url,
website_url=startup_input.website,
industry=startup_input.industry,
funding_stage=startup_input.stage,
team_size=None
)
# Run analysis
results = orchestrator.analyze_startup(startup_data)
# Convert results to serializable format
serializable_results = {}
for key, result in results.items():
serializable_results[key] = {
"agent_name": result.agent_name,
"analysis_type": result.analysis_type,
"findings": result.findings,
"confidence_score": result.confidence_score,
"timestamp": result.timestamp.isoformat()
}
logger.info(f"✅ Analysis completed for {startup_input.company_name}")
return {
"status": "success",
"results": serializable_results,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
"analysis_time": "10.0 seconds"
}
except Exception as e:
logger.error(f"Analysis failed: {str(e)}")
raise HTTPException(status_code=500, detail=f"Analysis failed: {str(e)}")
@app.post("/api/analyze-document")
async def analyze_document(file: UploadFile = File(...), analysis_type: str = "pitch_deck"):
"""Analyze uploaded documents using Smart Report Analyzer"""
if not SMART_ANALYZER_AVAILABLE:
raise HTTPException(status_code=503, detail="Smart Analyzer not available")
try:
logger.info(f"Analyzing document: {file.filename}")
# Save uploaded file temporarily
temp_path = f"/tmp/{file.filename}"
with open(temp_path, "wb") as buffer:
content = await file.read()
buffer.write(content)
# Load and analyze file
df, raw_text = load_file(temp_path)
result = {
"filename": file.filename,
"analysis_type": analysis_type,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}
if df is not None:
# Structured data analysis
result["data_preview"] = df.head().to_dict()
result["summary"] = summarize_report(df)
result["data_type"] = "structured"
elif raw_text:
# PDF/text analysis
result["text_preview"] = raw_text[:500] + "..." if len(raw_text) > 500 else raw_text
result["summary"] = summarize_report(raw_text)
result["data_type"] = "unstructured"
# Clean up temp file
os.remove(temp_path)
return {
"status": "success",
"result": result
}
except Exception as e:
logger.error(f"Document analysis failed: {str(e)}")
raise HTTPException(status_code=500, detail=f"Document analysis failed: {str(e)}")
@app.post("/api/ask-question")
async def ask_document_question(file: UploadFile = File(...), question: str = ""):
"""Ask questions about uploaded documents"""
if not SMART_ANALYZER_AVAILABLE:
raise HTTPException(status_code=503, detail="Smart Analyzer not available")
try:
# Save uploaded file temporarily
temp_path = f"/tmp/{file.filename}"
with open(temp_path, "wb") as buffer:
content = await file.read()
buffer.write(content)
# Load file and ask question
df, raw_text = load_file(temp_path)
if df is not None:
answer = ask_question(df, question)
elif raw_text:
answer = ask_question(raw_text, question)
else:
answer = "Could not process the uploaded file."
# Clean up temp file
os.remove(temp_path)
return {
"status": "success",
"question": question,
"answer": answer,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}
except Exception as e:
logger.error(f"Question answering failed: {str(e)}")
raise HTTPException(status_code=500, detail=f"Question answering failed: {str(e)}")
@app.get("/api/analysis-progress/{startup_id}")
async def get_analysis_progress(startup_id: str):
"""Get analysis progress (simplified)"""
return {
"status": "success",
"progress": {
"current_agent": "completed",
"percentage": 100,
"message": "Analysis completed"
},
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080)