Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, File, UploadFile | |
from fastapi.responses import JSONResponse, StreamingResponse | |
from annotations import analyze_pdf | |
import io | |
app = FastAPI( | |
title="PDF Language Issue Analyzer", | |
description="API endpoint to analyze PDFs for language issues, their locations, and provide an annotated PDF.", | |
version="1.0.0" | |
) | |
async def analyze_pdf_endpoint(file: UploadFile = File(...)): | |
""" | |
Endpoint to analyze a PDF file for language issues. | |
- **file**: PDF file to be analyzed. | |
**Returns**: | |
- **issues**: Dictionary containing total issues and detailed information. | |
- **annotated_pdf**: Annotated PDF file with highlighted issues. | |
""" | |
if file.content_type != "application/pdf": | |
return JSONResponse(status_code=400, content={"error": "Invalid file type. Please upload a PDF file."}) | |
try: | |
# Analyze the PDF | |
issues, annotated_pdf = analyze_pdf(file.file) | |
response = { | |
"issues": issues | |
} | |
if annotated_pdf: | |
# Encode the annotated PDF in base64 for transmission | |
annotated_pdf_io = io.BytesIO(annotated_pdf) | |
response["annotated_pdf"] = "data:application/pdf;base64," + io.base64.b64encode(annotated_pdf).decode('utf-8') | |
return JSONResponse(content=response) | |
except Exception as e: | |
return JSONResponse(status_code=500, content={"error": str(e)}) |