File size: 1,490 Bytes
706a020
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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"
)

@app.post("/analyze-pdf/")
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)})