Update app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,18 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 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 CORS so any frontend can call the 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 |
-
|
| 21 |
-
async def analyze_pdfs(files: list[UploadFile] = File(...)):
|
| 22 |
-
"""
|
| 23 |
-
Upload PDFs → process with SC pipeline → return JSON → save to dataset.csv
|
| 24 |
-
"""
|
| 25 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
shutil.copyfileobj(file.file, buffer)
|
| 30 |
-
|
| 31 |
results = run_pipeline(tmpdirname)
|
| 32 |
-
json_result = results.to_dict(orient="records")
|
| 33 |
|
| 34 |
-
# Save
|
| 35 |
if os.path.exists(DATASET_PATH):
|
| 36 |
dataset = pd.read_csv(DATASET_PATH)
|
| 37 |
dataset = pd.concat([dataset, results], ignore_index=True)
|
|
@@ -39,4 +20,13 @@ async def analyze_pdfs(files: list[UploadFile] = File(...)):
|
|
| 39 |
dataset = results
|
| 40 |
dataset.to_csv(DATASET_PATH, index=False)
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tempfile, os
|
|
|
|
|
|
|
|
|
|
| 3 |
from pipeline import run_pipeline
|
| 4 |
+
import pandas as pd
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
DATASET_PATH = "dataset.csv"
|
| 7 |
|
| 8 |
+
def analyze_pdf(file):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
with tempfile.TemporaryDirectory() as tmpdirname:
|
| 10 |
+
file_path = os.path.join(tmpdirname, file.name)
|
| 11 |
+
with open(file_path, "wb") as f:
|
| 12 |
+
f.write(file.read())
|
|
|
|
|
|
|
| 13 |
results = run_pipeline(tmpdirname)
|
|
|
|
| 14 |
|
| 15 |
+
# Save to dataset.csv
|
| 16 |
if os.path.exists(DATASET_PATH):
|
| 17 |
dataset = pd.read_csv(DATASET_PATH)
|
| 18 |
dataset = pd.concat([dataset, results], ignore_index=True)
|
|
|
|
| 20 |
dataset = results
|
| 21 |
dataset.to_csv(DATASET_PATH, index=False)
|
| 22 |
|
| 23 |
+
return results.to_dict(orient="records")
|
| 24 |
+
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=analyze_pdf,
|
| 27 |
+
inputs=gr.File(file_types=[".pdf"]),
|
| 28 |
+
outputs=gr.JSON(),
|
| 29 |
+
title="SC API"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
iface.launch()
|