|
|
|
|
|
import gradio as gr |
|
import torch |
|
from transformers import NougatProcessor, VisionEncoderDecoderModel |
|
from PIL import Image |
|
import fitz |
|
from typing import List |
|
|
|
|
|
|
|
MODEL_ID = "facebook/nougat-base" |
|
|
|
try: |
|
processor = NougatProcessor.from_pretrained(MODEL_ID) |
|
model = VisionEncoderDecoderModel.from_pretrained(MODEL_ID) |
|
|
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model.to(device) |
|
print(f"Model '{MODEL_ID}' başarıyla yüklendi ve '{device.upper()}' cihazına taşındı.") |
|
MODEL_LOADED = True |
|
except Exception as e: |
|
print(f"Model yüklenirken bir hata oluştu: {e}") |
|
MODEL_LOADED = False |
|
model = None |
|
processor = None |
|
|
|
|
|
|
|
def process_single_image(image: Image.Image) -> str: |
|
"""Tek bir PIL görüntüsünü işler ve Markdown metnini döndürür.""" |
|
if not MODEL_LOADED or image is None: |
|
return "Model yüklenemedi veya geçersiz görüntü." |
|
|
|
try: |
|
|
|
pixel_values = processor(images=image, return_tensors="pt").pixel_values |
|
|
|
|
|
outputs = model.generate( |
|
pixel_values.to(device), |
|
min_length=1, |
|
max_new_tokens=4096, |
|
bad_words_ids=[[processor.tokenizer.unk_token_id]], |
|
) |
|
|
|
|
|
sequence = processor.batch_decode(outputs, skip_special_tokens=True) |
|
sequence = processor.post_process_generation(sequence, fix_markdown=False) |
|
|
|
return sequence |
|
except Exception as e: |
|
return f"Görüntü işlenirken bir hata oluştu: {e}" |
|
|
|
def process_pdf_file(pdf_file) -> str: |
|
"""Yüklenen bir PDF dosyasını işler, her sayfasını dönüştürür ve birleştirir.""" |
|
if not MODEL_LOADED or pdf_file is None: |
|
return "Model yüklenemedi veya PDF dosyası yüklenmedi." |
|
|
|
full_markdown_content = |
|
try: |
|
doc = fitz.open(stream=pdf_file.read(), filetype="pdf") |
|
|
|
for page_num in range(len(doc)): |
|
page = doc.load_page(page_num) |
|
|
|
|
|
pix = page.get_pixmap(dpi=150) |
|
image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples).convert("RGB") |
|
|
|
|
|
page_markdown = process_single_image(image) |
|
full_markdown_content.append(f"## Sayfa {page_num + 1}\n\n{page_markdown}") |
|
|
|
return "\n\n---\n\n".join(full_markdown_content) |
|
except Exception as e: |
|
return f"PDF işlenirken bir hata oluştu: {e}" |
|
finally: |
|
if 'doc' in locals() and doc: |
|
doc.close() |
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
gr.Markdown( |
|
""" |
|
# 📄 Facebook Nougat Belge Dönüştürücü |
|
Bu arayüz, Meta AI tarafından geliştirilen **facebook/nougat-base** modelini kullanarak belgelerinizi (PDF veya resim) yapılandırılmış Markdown metnine dönüştürmenizi sağlar. |
|
Lütfen bir PDF dosyası veya bir belge sayfası görüntüsü yükleyin. |
|
""" |
|
) |
|
|
|
with gr.Tabs(): |
|
|
|
with gr.TabItem("PDF Dosyasını İşle"): |
|
with gr.Row(): |
|
pdf_input = gr.File(label="PDF Dosyası Yükle", file_types=[".pdf"]) |
|
pdf_process_button = gr.Button("PDF'i Dönüştür", variant="primary") |
|
pdf_output = gr.Markdown(label="Dönüştürülen Metin (Markdown)") |
|
|
|
|
|
with gr.TabItem("Tek Görüntü İşle"): |
|
with gr.Row(): |
|
image_input = gr.Image(label="Belge Sayfası Görüntüsü Yükle", type="pil") |
|
image_process_button = gr.Button("Görüntüyü Dönüştür", variant="primary") |
|
image_output = gr.Markdown(label="Dönüştürülen Metin (Markdown)") |
|
|
|
|
|
pdf_process_button.click( |
|
fn=process_pdf_file, |
|
inputs=[pdf_input], |
|
outputs=[pdf_output], |
|
api_name="process_pdf" |
|
) |
|
|
|
image_process_button.click( |
|
fn=process_single_image, |
|
inputs=[image_input], |
|
outputs=[image_output], |
|
api_name="process_image" |
|
) |
|
|
|
gr.Examples( |
|
examples=["nougat_paper_example.png"], |
|
inputs=image_input, |
|
outputs=image_output, |
|
fn=process_single_image, |
|
cache_examples=True, |
|
label="Örnek Görüntü" |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch(debug=True) |