Spaces:
Sleeping
Sleeping
samyak152002
commited on
Commit
•
706a020
1
Parent(s):
9ae3fcc
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
3 |
+
from annotations import analyze_pdf
|
4 |
+
import io
|
5 |
+
|
6 |
+
app = FastAPI(
|
7 |
+
title="PDF Language Issue Analyzer",
|
8 |
+
description="API endpoint to analyze PDFs for language issues, their locations, and provide an annotated PDF.",
|
9 |
+
version="1.0.0"
|
10 |
+
)
|
11 |
+
|
12 |
+
@app.post("/analyze-pdf/")
|
13 |
+
async def analyze_pdf_endpoint(file: UploadFile = File(...)):
|
14 |
+
"""
|
15 |
+
Endpoint to analyze a PDF file for language issues.
|
16 |
+
|
17 |
+
- **file**: PDF file to be analyzed.
|
18 |
+
|
19 |
+
**Returns**:
|
20 |
+
- **issues**: Dictionary containing total issues and detailed information.
|
21 |
+
- **annotated_pdf**: Annotated PDF file with highlighted issues.
|
22 |
+
"""
|
23 |
+
if file.content_type != "application/pdf":
|
24 |
+
return JSONResponse(status_code=400, content={"error": "Invalid file type. Please upload a PDF file."})
|
25 |
+
|
26 |
+
try:
|
27 |
+
# Analyze the PDF
|
28 |
+
issues, annotated_pdf = analyze_pdf(file.file)
|
29 |
+
|
30 |
+
response = {
|
31 |
+
"issues": issues
|
32 |
+
}
|
33 |
+
|
34 |
+
if annotated_pdf:
|
35 |
+
# Encode the annotated PDF in base64 for transmission
|
36 |
+
annotated_pdf_io = io.BytesIO(annotated_pdf)
|
37 |
+
response["annotated_pdf"] = "data:application/pdf;base64," + io.base64.b64encode(annotated_pdf).decode('utf-8')
|
38 |
+
|
39 |
+
return JSONResponse(content=response)
|
40 |
+
|
41 |
+
except Exception as e:
|
42 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|