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

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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})