File size: 920 Bytes
4fee431
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4f755d
4fee431
 
 
 
 
 
 
 
87a735b
b4f755d
87a735b
 
 
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
import fitz  # PyMuPDF
import docx
from io import BytesIO
import logging
from fastapi import HTTPException


def parse_docx(file: BytesIO):
    doc = docx.Document(file)
    text = ""
    for para in doc.paragraphs:
        text += para.text + "\n"
    return text


def parse_pdf(file: BytesIO):
    try:
        doc = fitz.open(stream=file, filetype="pdf")
        text = ""
        for page_num in range(doc.page_count):
            page = doc.load_page(page_num)
            text += page.get_text()
        return text 
    except Exception as e:
        logging.error(f"Error while processing PDF: {str(e)}")
        raise HTTPException(
            status_code=500, detail="Error processing PDF file")

def parse_txt(file: BytesIO):
    return file.read().decode("utf-8")

def end_symbol_for_NP_text(text: str) -> str:
    text = text.strip()
    if not text.endswith("।"):
        text += "।"
    return text