Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,7 @@ from transformers import NougatProcessor, VisionEncoderDecoderModel
|
|
| 6 |
from PIL import Image
|
| 7 |
import fitz # PyMuPDF
|
| 8 |
from typing import List
|
|
|
|
| 9 |
|
| 10 |
# --- Model ve İşlemci Yükleme ---
|
| 11 |
# Modelin yalnızca bir kez yüklenmesini sağlamak için global olarak tanımlıyoruz.
|
|
@@ -34,14 +35,14 @@ def process_single_image(image: Image.Image) -> str:
|
|
| 34 |
return "Model yüklenemedi veya geçersiz görüntü."
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
# Görüntüyü modelin beklediği formata dönüştürme
|
| 38 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 39 |
|
| 40 |
-
# Metin üretimi
|
| 41 |
outputs = model.generate(
|
| 42 |
pixel_values.to(device),
|
| 43 |
min_length=1,
|
| 44 |
-
max_new_tokens=4096,
|
| 45 |
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
| 46 |
)
|
| 47 |
|
|
@@ -49,6 +50,7 @@ def process_single_image(image: Image.Image) -> str:
|
|
| 49 |
sequence = processor.batch_decode(outputs, skip_special_tokens=True)
|
| 50 |
sequence = processor.post_process_generation(sequence, fix_markdown=False)
|
| 51 |
|
|
|
|
| 52 |
return "".join(sequence)
|
| 53 |
except Exception as e:
|
| 54 |
return f"Görüntü işlenirken bir hata oluştu: {e}"
|
|
@@ -58,8 +60,9 @@ def process_pdf_file(pdf_file) -> str:
|
|
| 58 |
if not MODEL_LOADED or pdf_file is None:
|
| 59 |
return "Model yüklenemedi veya PDF dosyası yüklenmedi."
|
| 60 |
|
| 61 |
-
#
|
| 62 |
full_markdown_content =
|
|
|
|
| 63 |
try:
|
| 64 |
# Gelen dosya nesnesinden PDF'i oku
|
| 65 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
|
@@ -79,7 +82,7 @@ def process_pdf_file(pdf_file) -> str:
|
|
| 79 |
except Exception as e:
|
| 80 |
return f"PDF işlenirken bir hata oluştu: {e}"
|
| 81 |
finally:
|
| 82 |
-
if
|
| 83 |
doc.close()
|
| 84 |
|
| 85 |
|
|
@@ -96,15 +99,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 96 |
with gr.Tabs():
|
| 97 |
# PDF İşleme Sekmesi
|
| 98 |
with gr.TabItem("PDF Dosyasını İşle"):
|
| 99 |
-
|
| 100 |
-
pdf_input = gr.File(label="PDF Dosyası Yükle", file_types=[".pdf"])
|
| 101 |
pdf_process_button = gr.Button("PDF'i Dönüştür", variant="primary")
|
| 102 |
pdf_output = gr.Markdown(label="Dönüştürülen Metin (Markdown)")
|
| 103 |
|
| 104 |
# Tek Görüntü İşleme Sekmesi
|
| 105 |
with gr.TabItem("Tek Görüntü İşle"):
|
| 106 |
-
|
| 107 |
-
image_input = gr.Image(label="Belge Sayfası Görüntüsü Yükle", type="pil")
|
| 108 |
image_process_button = gr.Button("Görüntüyü Dönüştür", variant="primary")
|
| 109 |
image_output = gr.Markdown(label="Dönüştürülen Metin (Markdown)")
|
| 110 |
|
|
@@ -124,14 +125,33 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 124 |
)
|
| 125 |
|
| 126 |
gr.Markdown("### Örnek Kullanım")
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
|
| 136 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
demo.launch(debug=True)
|
|
|
|
| 6 |
from PIL import Image
|
| 7 |
import fitz # PyMuPDF
|
| 8 |
from typing import List
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
# --- Model ve İşlemci Yükleme ---
|
| 12 |
# Modelin yalnızca bir kez yüklenmesini sağlamak için global olarak tanımlıyoruz.
|
|
|
|
| 35 |
return "Model yüklenemedi veya geçersiz görüntü."
|
| 36 |
|
| 37 |
try:
|
| 38 |
+
# Görüntüyü modelin beklediği formata dönüştürme
|
| 39 |
pixel_values = processor(images=image, return_tensors="pt").pixel_values
|
| 40 |
|
| 41 |
+
# Metin üretimi
|
| 42 |
outputs = model.generate(
|
| 43 |
pixel_values.to(device),
|
| 44 |
min_length=1,
|
| 45 |
+
max_new_tokens=4096,
|
| 46 |
bad_words_ids=[[processor.tokenizer.unk_token_id]],
|
| 47 |
)
|
| 48 |
|
|
|
|
| 50 |
sequence = processor.batch_decode(outputs, skip_special_tokens=True)
|
| 51 |
sequence = processor.post_process_generation(sequence, fix_markdown=False)
|
| 52 |
|
| 53 |
+
# batch_decode bir liste döndürür, bu yüzden ilk elemanı alıyoruz.
|
| 54 |
return "".join(sequence)
|
| 55 |
except Exception as e:
|
| 56 |
return f"Görüntü işlenirken bir hata oluştu: {e}"
|
|
|
|
| 60 |
if not MODEL_LOADED or pdf_file is None:
|
| 61 |
return "Model yüklenemedi veya PDF dosyası yüklenmedi."
|
| 62 |
|
| 63 |
+
# DÜZELTME: Değişken, boş bir liste olarak doğru şekilde başlatıldı.
|
| 64 |
full_markdown_content =
|
| 65 |
+
doc = None # doc değişkenini try bloğundan önce tanımla
|
| 66 |
try:
|
| 67 |
# Gelen dosya nesnesinden PDF'i oku
|
| 68 |
doc = fitz.open(stream=pdf_file.read(), filetype="pdf")
|
|
|
|
| 82 |
except Exception as e:
|
| 83 |
return f"PDF işlenirken bir hata oluştu: {e}"
|
| 84 |
finally:
|
| 85 |
+
if doc:
|
| 86 |
doc.close()
|
| 87 |
|
| 88 |
|
|
|
|
| 99 |
with gr.Tabs():
|
| 100 |
# PDF İşleme Sekmesi
|
| 101 |
with gr.TabItem("PDF Dosyasını İşle"):
|
| 102 |
+
pdf_input = gr.File(label="PDF Dosyası Yükle", file_types=[".pdf"])
|
|
|
|
| 103 |
pdf_process_button = gr.Button("PDF'i Dönüştür", variant="primary")
|
| 104 |
pdf_output = gr.Markdown(label="Dönüştürülen Metin (Markdown)")
|
| 105 |
|
| 106 |
# Tek Görüntü İşleme Sekmesi
|
| 107 |
with gr.TabItem("Tek Görüntü İşle"):
|
| 108 |
+
image_input = gr.Image(label="Belge Sayfası Görüntüsü Yükle", type="pil")
|
|
|
|
| 109 |
image_process_button = gr.Button("Görüntüyü Dönüştür", variant="primary")
|
| 110 |
image_output = gr.Markdown(label="Dönüştürülen Metin (Markdown)")
|
| 111 |
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
gr.Markdown("### Örnek Kullanım")
|
| 128 |
+
# Örnek bir resim dosyası oluşturun veya yükleyin ve adını buraya yazın.
|
| 129 |
+
# Örneğin, projenizin kök dizininde 'nougat_paper_example.png' adında bir dosya olmalı.
|
| 130 |
+
example_image_path = "nougat_paper_example.png"
|
| 131 |
+
if os.path.exists(example_image_path):
|
| 132 |
+
gr.Examples(
|
| 133 |
+
examples=[example_image_path],
|
| 134 |
+
inputs=image_input,
|
| 135 |
+
outputs=image_output,
|
| 136 |
+
fn=process_single_image,
|
| 137 |
+
cache_examples=True,
|
| 138 |
+
label="Örnek Görüntü"
|
| 139 |
+
)
|
| 140 |
|
| 141 |
if __name__ == "__main__":
|
| 142 |
+
# Örnek resim dosyasını indirme (eğer yoksa)
|
| 143 |
+
if not os.path.exists("nougat_paper_example.png"):
|
| 144 |
+
try:
|
| 145 |
+
import requests
|
| 146 |
+
from PIL import Image
|
| 147 |
+
from io import BytesIO
|
| 148 |
+
|
| 149 |
+
url = "https://huggingface.co/datasets/hf-internal-testing/fixtures_docvqa/resolve/main/nougat_paper.png"
|
| 150 |
+
response = requests.get(url)
|
| 151 |
+
img = Image.open(BytesIO(response.content))
|
| 152 |
+
img.save("nougat_paper_example.png")
|
| 153 |
+
print("Örnek resim 'nougat_paper_example.png' indirildi.")
|
| 154 |
+
except Exception as e:
|
| 155 |
+
print(f"Örnek resim indirilemedi: {e}")
|
| 156 |
+
|
| 157 |
demo.launch(debug=True)
|