Spaces:
Running
Running
File size: 3,063 Bytes
aabd32c 8e19d1c aabd32c 8e19d1c aabd32c 8e19d1c aabd32c c103654 8e19d1c aabd32c 8e19d1c aabd32c 8e19d1c aabd32c 8e19d1c |
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 |
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from agentLogic.graph import app as rag_app
from processingPdf.indexer import Indexer
import shutil
import os
import logging
# Configurazione logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
# Middleware CORS aggiornato
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Più sicuro per il deploy unificato
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Inizializzazione dell'Indexer
# (Nota: Se vedi che lo Space va in timeout, sposta questa riga dentro upload_pdf)
indexer_worker = Indexer()
# --- GESTIONE FRONTEND STATICO ---
# Assicurati che la cartella 'static' esista (creata dal Dockerfile)
if os.path.exists("static"):
app.mount("/assets", StaticFiles(directory="static/assets"), name="assets")
@app.get("/")
async def serve_frontend():
index_path = os.path.join("static", "index.html")
if os.path.exists(index_path):
return FileResponse(index_path)
return {"message": "Backend is running, but static/index.html was not found."}
# --- ENDPOINT API ---
@app.post("/upload")
async def upload_pdf(file: UploadFile = File(...), user_id: str = Form(...)):
upload_dir = "temp_uploads"
os.makedirs(upload_dir, exist_ok=True)
file_path = os.path.join(upload_dir, file.filename)
try:
logger.info(f"Ricezione file: {file.filename} per l'utente: {user_id}")
with open(file_path, "wb") as buffer:
shutil.copyfileobj(file.file, buffer)
indexer_worker.index_pdf(file_path, user_id)
return {
"status": "success",
"message": "Indicizzazione completata con successo",
"filename": file.filename
}
except Exception as e:
logger.error(f"Errore durante l'upload/indicizzazione: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
finally:
if os.path.exists(file_path):
os.remove(file_path)
@app.post("/chat")
async def chat(query: str, filename: str, user_id: str):
try:
initial_state = {
"query": query,
"user_id": user_id,
"filename": filename,
"intent_data": {},
"context_chunks": [],
"final_answer": ""
}
result = rag_app.invoke(initial_state)
return {"answer": result["final_answer"]}
except Exception as e:
logger.error(f"Errore nella chat endpoint: {str(e)}")
raise HTTPException(status_code=500, detail="Errore durante l'elaborazione.")
# Gestione rotte React (SPA) per evitare 404 al refresh
@app.exception_handler(404)
async def custom_404_handler(request, __):
return FileResponse("static/index.html")
# Rimosso il blocco if __name__ == "__main__":
# perché l'avvio è gestito dal Dockerfile tramite CMD |