Spaces:
Runtime error
Runtime error
Create main.py
Browse files- app/main.py +38 -0
app/main.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 3 |
+
from fastapi.responses import JSONResponse, StreamingResponse
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
import io
|
| 6 |
+
from annotations import analyze_pdf
|
| 7 |
+
|
| 8 |
+
app = FastAPI(
|
| 9 |
+
title="PDF Language Issue Analyzer",
|
| 10 |
+
description="An API to analyze PDFs for language issues and provide an annotated PDF.",
|
| 11 |
+
version="1.0.0"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
@app.post("/analyze", summary="Analyze PDF for Language Issues")
|
| 15 |
+
async def analyze_pdf_endpoint(file: UploadFile = File(...)):
|
| 16 |
+
"""
|
| 17 |
+
Analyze an uploaded PDF file for language issues.
|
| 18 |
+
|
| 19 |
+
- **file**: PDF file to be analyzed.
|
| 20 |
+
"""
|
| 21 |
+
if file.content_type != "application/pdf":
|
| 22 |
+
raise HTTPException(status_code=400, detail="Invalid file type. Only PDFs are supported.")
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
contents = await file.read()
|
| 26 |
+
file_stream = io.BytesIO(contents)
|
| 27 |
+
language_issues, annotated_pdf = analyze_pdf(file_stream)
|
| 28 |
+
|
| 29 |
+
response = {
|
| 30 |
+
"language_issues": language_issues
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
if annotated_pdf:
|
| 34 |
+
response["annotated_pdf"] = "data:application/pdf;base64," + io.BytesIO(annotated_pdf).getvalue().decode('latin1') # Adjust encoding as needed
|
| 35 |
+
|
| 36 |
+
return JSONResponse(content=response)
|
| 37 |
+
except Exception as e:
|
| 38 |
+
raise HTTPException(status_code=500, detail=str(e))
|