VicGerardoPR's picture
Update main.py
9355575 verified
from fastapi import FastAPI, UploadFile, File, Form, HTTPException
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import os
import json
from dotenv import load_dotenv
from services.ingestion import ingestion_service
from services.profiler import profiler_service
from services.ai_service import ai_service
load_dotenv()
app = FastAPI(title="DashBoardAI Backend")
# Configure CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # In production, replace with your Vercel URL
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/health")
async def health_check():
return {"status": "healthy"}
@app.get("/demo")
async def load_demo():
demo_path = "../demo_data/sample.csv"
if not os.path.exists(demo_path):
# Fallback if path is different in production/docker
demo_path = "demo_data/sample.csv"
if not os.path.exists(demo_path):
return {"error": "Demo file not found"}
with open(demo_path, "rb") as f:
content = f.read()
return ingestion_service.ingest_file(content, "sample.csv")
@app.post("/ingest")
async def ingest_data(file: UploadFile = File(None), connection_string: str = Form(None), table_name: str = Form(None)):
if file:
content = await file.read()
return ingestion_service.ingest_file(content, file.filename)
elif connection_string:
return ingestion_service.ingest_db(connection_string, table_name=table_name)
raise HTTPException(status_code=400, detail="No file or connection string provided")
@app.get("/profile/{dataset_id}")
async def profile_data(dataset_id: str):
df = ingestion_service.get_dataset(dataset_id)
if df is None:
raise HTTPException(status_code=404, detail="Dataset not found")
profile = profiler_service.generate_profile(df)
return profile
@app.post("/chat/{dataset_id}")
async def chat_with_data(dataset_id: str, query: str = Form(...)):
df = ingestion_service.get_dataset(dataset_id)
if df is None:
raise HTTPException(status_code=404, detail="Dataset not found")
response = await ai_service.chat_with_data(dataset_id, df, query)
return {"response": response}
if __name__ == "__main__":
port = int(os.environ.get("PORT", 8000))
uvicorn.run("main:app", host="0.0.0.0", port=port, reload=True)