Spaces:
Sleeping
Sleeping
Update app.py
#17
by
sahil-1-garg
- opened
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
from fastapi import FastAPI, APIRouter, UploadFile, File, HTTPException, Query, Form
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from typing import Optional
|
|
|
|
| 4 |
import os
|
| 5 |
import shutil
|
| 6 |
import logging
|
| 7 |
import json
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
from agents.simple_tools import generate_notes_full_pipeline_from_path
|
| 10 |
from agents.generator_validator import create_notes_pipeline, InteractiveFeedbackManager
|
| 11 |
from agents.langgraph import run_workflow
|
|
@@ -13,16 +13,23 @@ 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 |
-
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
|
|
@@ -42,14 +49,6 @@ feedback_manager = InteractiveFeedbackManager()
|
|
| 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.")
|
| 48 |
-
|
| 49 |
-
@app.on_event("shutdown")
|
| 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 |
|
|
@@ -128,7 +127,7 @@ async def notes_llm_route(
|
|
| 128 |
async def submit_feedback(
|
| 129 |
session_id: str = Form(...),
|
| 130 |
feedback_text: str = Form(...),
|
| 131 |
-
feedback_type: str = Form(...,
|
| 132 |
):
|
| 133 |
try:
|
| 134 |
udf_version = feedback_manager.add_feedback(session_id, feedback_text, feedback_type)
|
|
@@ -212,6 +211,12 @@ async def generate_with_feedback(
|
|
| 212 |
file: UploadFile = File(...),
|
| 213 |
user_api_key: Optional[str] = Form(None)
|
| 214 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
try:
|
| 216 |
session = feedback_manager.get_session(session_id)
|
| 217 |
|
|
|
|
| 1 |
from fastapi import FastAPI, APIRouter, UploadFile, File, HTTPException, Query, Form
|
| 2 |
from fastapi.responses import FileResponse
|
| 3 |
from typing import Optional
|
| 4 |
+
from contextlib import asynccontextmanager
|
| 5 |
import os
|
| 6 |
import shutil
|
| 7 |
import logging
|
| 8 |
import json
|
|
|
|
| 9 |
from agents.simple_tools import generate_notes_full_pipeline_from_path
|
| 10 |
from agents.generator_validator import create_notes_pipeline, InteractiveFeedbackManager
|
| 11 |
from agents.langgraph import run_workflow
|
|
|
|
| 13 |
from agents.rlhf_routes import rlhf_router
|
| 14 |
from fastapi.middleware.cors import CORSMiddleware
|
| 15 |
|
|
|
|
|
|
|
| 16 |
logging.basicConfig(level=logging.INFO)
|
| 17 |
logger = logging.getLogger("financial_notes_api")
|
| 18 |
|
| 19 |
+
@asynccontextmanager
|
| 20 |
+
async def lifespan(app: FastAPI):
|
| 21 |
+
# Startup
|
| 22 |
+
logger.info("Financial Notes Generator API has started.")
|
| 23 |
+
yield
|
| 24 |
+
# Shutdown
|
| 25 |
+
logger.info("Financial Notes Generator API is shutting down.")
|
| 26 |
+
|
| 27 |
# Initialize FastAPI app first
|
| 28 |
app = FastAPI(
|
| 29 |
title="Financial Notes Generator API",
|
| 30 |
description="API for generating financial notes, balance sheets, cash flow statements, and P&L reports with RLHF capabilities and Interactive Feedback.",
|
| 31 |
+
version="1.0.0",
|
| 32 |
+
lifespan=lifespan
|
| 33 |
)
|
| 34 |
|
| 35 |
# Add CORS middleware immediately after app initialization
|
|
|
|
| 49 |
# Include RLHF router
|
| 50 |
app.include_router(rlhf_router)
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# Initialize router for main endpoints
|
| 53 |
router = APIRouter()
|
| 54 |
|
|
|
|
| 127 |
async def submit_feedback(
|
| 128 |
session_id: str = Form(...),
|
| 129 |
feedback_text: str = Form(...),
|
| 130 |
+
feedback_type: str = Form(..., pattern="^(text|numeric|formula|suggestion)$")
|
| 131 |
):
|
| 132 |
try:
|
| 133 |
udf_version = feedback_manager.add_feedback(session_id, feedback_text, feedback_type)
|
|
|
|
| 211 |
file: UploadFile = File(...),
|
| 212 |
user_api_key: Optional[str] = Form(None)
|
| 213 |
):
|
| 214 |
+
if not user_api_key or user_api_key.strip() == "":
|
| 215 |
+
raise HTTPException(
|
| 216 |
+
status_code=400,
|
| 217 |
+
detail="Missing required parameter: 'user_api_key'. Please provide your OpenRouter API key as a form parameter (not in JSON body)."
|
| 218 |
+
)
|
| 219 |
+
|
| 220 |
try:
|
| 221 |
session = feedback_manager.get_session(session_id)
|
| 222 |
|