samyak152002's picture
Create main.py
6b78f9c verified
raw
history blame
1.32 kB
# main.py
from fastapi import FastAPI, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse, StreamingResponse
from typing import Dict, Any
import io
from annotations import analyze_pdf
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.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:
response["annotated_pdf"] = "data:application/pdf;base64," + io.BytesIO(annotated_pdf).getvalue().decode('latin1') # Adjust encoding as needed
return JSONResponse(content=response)
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))