Spaces:
Runtime error
Runtime error
File size: 1,622 Bytes
6b78f9c d7adeec 6b78f9c d7adeec 6b78f9c 1854fbd 6b78f9c d7adeec 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 |
# main.py
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
from typing import Dict, Any
import io
import base64 # Import base64 for encoding
from .annotations import analyze_pdf # Use relative import
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"
)
@app.get("/", summary="Root Endpoint")
def read_root():
"""
Root endpoint to verify that the API is running.
"""
return {"message": "PDF Language Issue Analyzer API is running."}
@app.post("/analyze", summary="Analyze PDF for Language Issues")
async def analyze_pdf_endpoint(file: UploadFile = File(...)):
"""
Analyze an uploaded PDF file for language issues.
- **file**: PDF file to be analyzed.
"""
if file.content_type != "application/pdf":
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 = {
"language_issues": language_issues
}
if annotated_pdf:
# Properly encode the annotated PDF in Base64
encoded_pdf = base64.b64encode(annotated_pdf).decode('utf-8')
response["annotated_pdf"] = f"data:application/pdf;base64,{encoded_pdf}"
return JSONResponse(content=response)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e)) |