Spaces:
Sleeping
Sleeping
Add CORS(Using "*" for debugging)
Browse files
app.py
CHANGED
|
@@ -13,33 +13,35 @@ from agents.rlhf_workflows import run_rlhf_workflow
|
|
| 13 |
from agents.rlhf_routes import rlhf_router
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
|
| 16 |
-
|
| 17 |
load_dotenv()
|
| 18 |
|
| 19 |
logging.basicConfig(level=logging.INFO)
|
| 20 |
logger = logging.getLogger("financial_notes_api")
|
| 21 |
|
| 22 |
-
|
| 23 |
app = FastAPI(
|
| 24 |
title="Financial Notes Generator API",
|
| 25 |
description="API for generating financial notes, balance sheets, cash flow statements, and P&L reports with RLHF capabilities and Interactive Feedback.",
|
| 26 |
version="1.0.0"
|
| 27 |
)
|
| 28 |
|
|
|
|
|
|
|
| 29 |
app.add_middleware(
|
| 30 |
CORSMiddleware,
|
| 31 |
-
allow_origins=[
|
| 32 |
-
"http://localhost:3000",
|
| 33 |
-
"https://point9-finryver-dev.hf.space"
|
| 34 |
-
],
|
| 35 |
allow_credentials=True,
|
| 36 |
-
allow_methods=["*"],
|
| 37 |
-
allow_headers=["*"],
|
|
|
|
| 38 |
)
|
| 39 |
|
|
|
|
| 40 |
feedback_manager = InteractiveFeedbackManager()
|
| 41 |
|
|
|
|
| 42 |
app.include_router(rlhf_router)
|
|
|
|
| 43 |
@app.on_event("startup")
|
| 44 |
async def startup_event():
|
| 45 |
logger.info("Financial Notes Generator API has started.")
|
|
@@ -48,6 +50,7 @@ async def startup_event():
|
|
| 48 |
async def shutdown_event():
|
| 49 |
logger.info("Financial Notes Generator API is shutting down.")
|
| 50 |
|
|
|
|
| 51 |
router = APIRouter()
|
| 52 |
|
| 53 |
|
|
@@ -274,7 +277,6 @@ async def generate_with_feedback(
|
|
| 274 |
raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
|
| 275 |
|
| 276 |
|
| 277 |
-
|
| 278 |
@router.post("/notes")
|
| 279 |
async def notes_route(file: UploadFile = File(...)):
|
| 280 |
try:
|
|
@@ -295,6 +297,7 @@ async def notes_route(file: UploadFile = File(...)):
|
|
| 295 |
logger.error(f"Error in notes generation: {e}")
|
| 296 |
raise HTTPException(status_code=500, detail=f"Error generating notes: {str(e)}")
|
| 297 |
|
|
|
|
| 298 |
@router.post("/pnl")
|
| 299 |
async def pnl_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 300 |
file_path = f"data/input/{file.filename}"
|
|
@@ -308,7 +311,10 @@ async def pnl_route(file: UploadFile = File(...), use_rlhf: bool = Query(False))
|
|
| 308 |
result = run_workflow(file_path, "pnl")
|
| 309 |
|
| 310 |
if result["status"] == "success":
|
| 311 |
-
response = FileResponse(
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
if "rlhf_metadata" in result.get("result", {}):
|
| 314 |
rlhf_data = result["result"]["rlhf_metadata"]
|
|
@@ -319,6 +325,7 @@ async def pnl_route(file: UploadFile = File(...), use_rlhf: bool = Query(False))
|
|
| 319 |
return response
|
| 320 |
raise HTTPException(status_code=500, detail=result["error"])
|
| 321 |
|
|
|
|
| 322 |
@router.post("/bs")
|
| 323 |
async def bs_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 324 |
file_path = f"data/input/{file.filename}"
|
|
@@ -353,6 +360,7 @@ async def bs_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
|
| 353 |
else:
|
| 354 |
raise HTTPException(status_code=500, detail=result["error"])
|
| 355 |
|
|
|
|
| 356 |
@router.post("/cf")
|
| 357 |
async def cf_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 358 |
file_path = f"data/input/{file.filename}"
|
|
@@ -366,7 +374,10 @@ async def cf_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
|
| 366 |
result = run_workflow(file_path, "cf")
|
| 367 |
|
| 368 |
if result["status"] == "success":
|
| 369 |
-
response = FileResponse(
|
|
|
|
|
|
|
|
|
|
| 370 |
|
| 371 |
if "rlhf_metadata" in result.get("result", {}):
|
| 372 |
rlhf_data = result["result"]["rlhf_metadata"]
|
|
@@ -376,6 +387,9 @@ async def cf_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
|
| 376 |
|
| 377 |
return response
|
| 378 |
raise HTTPException(status_code=500, detail=result["error"])
|
|
|
|
|
|
|
|
|
|
| 379 |
app.include_router(router)
|
| 380 |
|
| 381 |
if __name__ == "__main__":
|
|
|
|
| 13 |
from agents.rlhf_routes import rlhf_router
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
|
|
|
|
| 16 |
load_dotenv()
|
| 17 |
|
| 18 |
logging.basicConfig(level=logging.INFO)
|
| 19 |
logger = logging.getLogger("financial_notes_api")
|
| 20 |
|
| 21 |
+
# Initialize FastAPI app first
|
| 22 |
app = FastAPI(
|
| 23 |
title="Financial Notes Generator API",
|
| 24 |
description="API for generating financial notes, balance sheets, cash flow statements, and P&L reports with RLHF capabilities and Interactive Feedback.",
|
| 25 |
version="1.0.0"
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# Add CORS middleware immediately after app initialization
|
| 29 |
+
# Using "*" for debugging - restrict this in production
|
| 30 |
app.add_middleware(
|
| 31 |
CORSMiddleware,
|
| 32 |
+
allow_origins=["*"], # Temporarily allow all origins for debugging
|
|
|
|
|
|
|
|
|
|
| 33 |
allow_credentials=True,
|
| 34 |
+
allow_methods=["*"], # Allow all methods (GET, POST, OPTIONS, etc.)
|
| 35 |
+
allow_headers=["*"], # Allow all headers
|
| 36 |
+
expose_headers=["*"], # Expose all custom headers to frontend
|
| 37 |
)
|
| 38 |
|
| 39 |
+
# Initialize feedback manager
|
| 40 |
feedback_manager = InteractiveFeedbackManager()
|
| 41 |
|
| 42 |
+
# Include RLHF router
|
| 43 |
app.include_router(rlhf_router)
|
| 44 |
+
|
| 45 |
@app.on_event("startup")
|
| 46 |
async def startup_event():
|
| 47 |
logger.info("Financial Notes Generator API has started.")
|
|
|
|
| 50 |
async def shutdown_event():
|
| 51 |
logger.info("Financial Notes Generator API is shutting down.")
|
| 52 |
|
| 53 |
+
# Initialize router for main endpoints
|
| 54 |
router = APIRouter()
|
| 55 |
|
| 56 |
|
|
|
|
| 277 |
raise HTTPException(status_code=500, detail=f"Generation failed: {str(e)}")
|
| 278 |
|
| 279 |
|
|
|
|
| 280 |
@router.post("/notes")
|
| 281 |
async def notes_route(file: UploadFile = File(...)):
|
| 282 |
try:
|
|
|
|
| 297 |
logger.error(f"Error in notes generation: {e}")
|
| 298 |
raise HTTPException(status_code=500, detail=f"Error generating notes: {str(e)}")
|
| 299 |
|
| 300 |
+
|
| 301 |
@router.post("/pnl")
|
| 302 |
async def pnl_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 303 |
file_path = f"data/input/{file.filename}"
|
|
|
|
| 311 |
result = run_workflow(file_path, "pnl")
|
| 312 |
|
| 313 |
if result["status"] == "success":
|
| 314 |
+
response = FileResponse(
|
| 315 |
+
result["result"].get("output_path", "data/pnl_statement.xlsx"),
|
| 316 |
+
filename=os.path.basename(result["result"].get("output_path", "data/pnl_statement.xlsx"))
|
| 317 |
+
)
|
| 318 |
|
| 319 |
if "rlhf_metadata" in result.get("result", {}):
|
| 320 |
rlhf_data = result["result"]["rlhf_metadata"]
|
|
|
|
| 325 |
return response
|
| 326 |
raise HTTPException(status_code=500, detail=result["error"])
|
| 327 |
|
| 328 |
+
|
| 329 |
@router.post("/bs")
|
| 330 |
async def bs_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 331 |
file_path = f"data/input/{file.filename}"
|
|
|
|
| 360 |
else:
|
| 361 |
raise HTTPException(status_code=500, detail=result["error"])
|
| 362 |
|
| 363 |
+
|
| 364 |
@router.post("/cf")
|
| 365 |
async def cf_route(file: UploadFile = File(...), use_rlhf: bool = Query(False)):
|
| 366 |
file_path = f"data/input/{file.filename}"
|
|
|
|
| 374 |
result = run_workflow(file_path, "cf")
|
| 375 |
|
| 376 |
if result["status"] == "success":
|
| 377 |
+
response = FileResponse(
|
| 378 |
+
result["result"].get("output_path", "data/cash_flow_statements.xlsx"),
|
| 379 |
+
filename=os.path.basename(result["result"].get("output_path", "data/cash_flow_statements.xlsx"))
|
| 380 |
+
)
|
| 381 |
|
| 382 |
if "rlhf_metadata" in result.get("result", {}):
|
| 383 |
rlhf_data = result["result"]["rlhf_metadata"]
|
|
|
|
| 387 |
|
| 388 |
return response
|
| 389 |
raise HTTPException(status_code=500, detail=result["error"])
|
| 390 |
+
|
| 391 |
+
|
| 392 |
+
# Include router after all route definitions
|
| 393 |
app.include_router(router)
|
| 394 |
|
| 395 |
if __name__ == "__main__":
|