File size: 872 Bytes
cc21f11
 
0303b9b
 
cc21f11
 
0303b9b
 
 
 
cc21f11
 
0303b9b
 
 
 
 
 
 
cc21f11
 
0303b9b
 
 
 
cc21f11
 
 
0303b9b
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
import fitz  # PyMuPDF

MAX_PAGES = 5  # fazla token yememesi için sınır

def extract_text_from_pdf(pdf_input):
    try:
        # Hugging Face ortamında pdf_input bir file-like objedir (upload edilen dosya)
        if hasattr(pdf_input, "read"):
            doc = fitz.open(stream=pdf_input.read(), filetype="pdf")
        elif isinstance(pdf_input, str):
            doc = fitz.open(pdf_input)
        else:
            return "[ERROR] Geçersiz PDF girişi"

        total_pages = len(doc)
        text = ""

        for i in range(min(MAX_PAGES, total_pages)):
            text += doc[i].get_text()

        doc.close()

        if total_pages > MAX_PAGES:
            text += f"\n\n[INFO] PDF {total_pages} sayfa. Yalnızca ilk {MAX_PAGES} sayfa işlendi."

        return text

    except Exception as e:
        return f"[ERROR] PDF İşleme Hatası: {str(e)}"