File size: 973 Bytes
9ba3ade
 
 
 
 
 
 
9c62372
 
 
 
d92c861
9c62372
d92c861
 
 
 
 
 
 
9c62372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f0c7dc
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
try: from pip._internal.operations import freeze
except ImportError: # pip < 10.0
    from pip.operations import freeze

pkgs = freeze.freeze()
for pkg in pkgs: print(pkg)

from fastapi import FastAPI, HTTPException, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from PyPDF2 import PdfReader

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/get_ocr_data/")
async def get_data(pdf: UploadFile = File(...)):
    try:
        # Read PDF file using PyPDF2
        pdf_reader = PdfReader(pdf.file)
        text = ""
        
        # Extract text from each page
        for page in pdf_reader.pages:
            text += page.extract_text()

        # Return extracted text
        return {"text": text.strip()}

    except Exception as e:
        raise HTTPException(status_code=500, detail=f"Error processing PDF: {str(e)}")