Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,38 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
-
import tempfile, shutil, os
|
| 5 |
-
import pandas as pd
|
| 6 |
-
from pipeline import run_pipeline
|
| 7 |
-
|
| 8 |
-
app = FastAPI(title="SC API", version="1.0")
|
| 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 |
-
dataset = pd.
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return JSONResponse(content={"results": json_result})
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import tempfile, shutil, os
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from pipeline import run_pipeline
|
| 7 |
+
|
| 8 |
+
app = FastAPI(title="SC API", version="1.0")
|
| 9 |
+
|
| 10 |
+
app.add_middleware(
|
| 11 |
+
CORSMiddleware,
|
| 12 |
+
allow_origins=["*"],
|
| 13 |
+
allow_methods=["*"],
|
| 14 |
+
allow_headers=["*"],
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
DATASET_PATH = "dataset.csv"
|
| 18 |
+
|
| 19 |
+
@app.post("/analyze-pdfs/")
|
| 20 |
+
async def analyze_pdfs(files: list[UploadFile] = File(...)):
|
| 21 |
+
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 22 |
+
for file in files:
|
| 23 |
+
file_path = os.path.join(tmpdirname, file.filename)
|
| 24 |
+
with open(file_path, "wb") as buffer:
|
| 25 |
+
shutil.copyfileobj(file.file, buffer)
|
| 26 |
+
|
| 27 |
+
results = run_pipeline(tmpdirname)
|
| 28 |
+
json_result = results.to_dict(orient="records")
|
| 29 |
+
|
| 30 |
+
# Save dataset
|
| 31 |
+
if os.path.exists(DATASET_PATH):
|
| 32 |
+
dataset = pd.read_csv(DATASET_PATH)
|
| 33 |
+
dataset = pd.concat([dataset, results], ignore_index=True)
|
| 34 |
+
else:
|
| 35 |
+
dataset = results
|
| 36 |
+
dataset.to_csv(DATASET_PATH, index=False)
|
| 37 |
+
|
| 38 |
+
return JSONResponse(content={"results": json_result})
|
|
|