File size: 2,367 Bytes
57b9fae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afe7d9b
 
 
 
 
57b9fae
 
 
 
afe7d9b
57b9fae
11a1a72
57b9fae
afe7d9b
 
9355575
 
 
 
57b9fae
9355575
 
 
57b9fae
11a1a72
 
9355575
 
 
 
 
afe7d9b
57b9fae
11a1a72
9355575
afe7d9b
9355575
 
 
 
57b9fae
9355575
 
afe7d9b
9355575
 
 
 
57b9fae
 
afe7d9b
9355575
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
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)