File size: 2,358 Bytes
33fa0fb
 
514e4c2
6b78f9c
 
1e01eb5
 
 
 
 
 
 
6b78f9c
 
 
 
 
 
 
514e4c2
 
 
 
 
1854fbd
33fa0fb
1854fbd
1e01eb5
33fa0fb
1854fbd
514e4c2
 
6b78f9c
514e4c2
6b78f9c
 
1e01eb5
6b78f9c
 
 
 
 
 
 
514e4c2
 
6b78f9c
 
 
 
514e4c2
d7adeec
514e4c2
 
1e01eb5
6b78f9c
1e01eb5
514e4c2
6b78f9c
1e01eb5
6b78f9c
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from fastapi import FastAPI, File, UploadFile, HTTPException, Request
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.templating import Jinja2Templates
from typing import Dict, Any
import io
import base64
import logging
from .annotations import analyze_pdf

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(
    title="PDF Language Issue Analyzer",
    description="An API to analyze PDFs for language issues and provide an annotated PDF.",
    version="1.0.0"
)

# Set up templates
templates = Jinja2Templates(directory="app/templates")

@app.get("/", response_class=HTMLResponse, summary="Root Endpoint")
def read_root(request: Request):
    """
    Render a simple HTML to verify the API is running.
    """
    logger.info("Root endpoint accessed.")
    return HTMLResponse(content="<h1>PDF Language Issue Analyzer API is running.</h1>")

@app.post("/analyze", response_class=HTMLResponse, summary="Analyze PDF for Language Issues")
async def analyze_pdf_endpoint(request: Request, file: UploadFile = File(...)):
    """
    Analyze an uploaded PDF file for language issues and render the results on the frontend.
    """
    if file.content_type != "application/pdf":
        logger.error("Invalid file type uploaded: %s", file.content_type)
        raise HTTPException(status_code=400, detail="Invalid file type. Only PDFs are supported.")

    try:
        contents = await file.read()
        file_stream = io.BytesIO(contents)
        language_issues, annotated_pdf = analyze_pdf(file_stream)

        response_context = {
            "request": request,
            "language_issues": language_issues
        }

        if annotated_pdf:
            # Encode the annotated PDF in Base64
            encoded_pdf = base64.b64encode(annotated_pdf).decode('utf-8')
            annotated_pdf_data_url = f"data:application/pdf;base64,{encoded_pdf}"
            response_context["annotated_pdf"] = annotated_pdf_data_url
            logger.info("Annotated PDF generated and encoded.")

        logger.info("PDF analysis completed successfully.")
        return templates.TemplateResponse("index.html", response_context)
    except Exception as e:
        logger.exception("Error occurred during PDF analysis.")
        raise HTTPException(status_code=500, detail=str(e))