Tanxshh commited on
Commit
1f0bb64
·
verified ·
1 Parent(s): 338dec1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -39
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
- # Allow frontend to call API
11
- app.add_middleware(
12
- CORSMiddleware,
13
- allow_origins=["*"],
14
- allow_methods=["*"],
15
- allow_headers=["*"],
16
- )
17
-
18
- DATASET_PATH = "dataset.csv"
19
-
20
- @app.post("/analyze-pdfs/")
21
- async def analyze_pdfs(files: list[UploadFile] = File(...)):
22
- with tempfile.TemporaryDirectory() as tmpdirname:
23
- for file in files:
24
- file_path = os.path.join(tmpdirname, file.filename)
25
- with open(file_path, "wb") as buffer:
26
- shutil.copyfileobj(file.file, buffer)
27
-
28
- results = run_pipeline(tmpdirname)
29
- json_result = results.to_dict(orient="records")
30
-
31
- # Save to dataset.csv
32
- if os.path.exists(DATASET_PATH):
33
- dataset = pd.read_csv(DATASET_PATH)
34
- dataset = pd.concat([dataset, results], ignore_index=True)
35
- else:
36
- dataset = results
37
-
38
- dataset.to_csv(DATASET_PATH, index=False)
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})