|
|
|
import gradio as gr |
|
import torch |
|
import pypandoc |
|
from pathlib import Path |
|
from nougat import NougatModel |
|
from nougat.utils.device import move_to_device |
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
print("Nougat modeli yükleniyor...") |
|
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
model = NougatModel.from_pretrained("facebook/nougat-base").to(device) |
|
model.eval() |
|
print(f"Model başarıyla '{device}' üzerine yüklendi.") |
|
|
|
|
|
def convert_pdf_to_epub(pdf_file): |
|
""" |
|
Bu fonksiyon bir PDF dosyası alır, Nougat ile Markdown'a çevirir |
|
ve Pandoc ile EPUB formatına dönüştürür. |
|
""" |
|
if pdf_file is None: |
|
raise gr.Error("Lütfen bir PDF dosyası yükleyin!") |
|
|
|
pdf_path = Path(pdf_file.name) |
|
print(f"İşlem başlıyor: {pdf_path.name}") |
|
|
|
try: |
|
|
|
print("Nougat ile metin ve yapı analizi yapılıyor...") |
|
|
|
|
|
result = model.inference(image_paths=[pdf_path]) |
|
|
|
|
|
markdown_content = result['markdown'] |
|
|
|
if not markdown_content.strip(): |
|
raise gr.Error("PDF'den metin çıkarılamadı. Dosya bozuk veya sadece resim içeriyor olabilir.") |
|
|
|
print("Markdown içeriği başarıyla oluşturuldu.") |
|
|
|
|
|
print("Pandoc ile EPUB dosyası oluşturuluyor...") |
|
|
|
|
|
output_filename = pdf_path.stem + ".epub" |
|
|
|
output_path = Path("/tmp") / output_filename |
|
|
|
|
|
|
|
pypandoc.convert_text( |
|
source=markdown_content, |
|
to='epub', |
|
format='markdown', |
|
outputfile=str(output_path), |
|
extra_args=[f'--metadata=title:{pdf_path.stem}'] |
|
) |
|
|
|
print(f"EPUB dosyası başarıyla oluşturuldu: {output_path}") |
|
|
|
|
|
return str(output_path) |
|
|
|
except Exception as e: |
|
print(f"Bir hata oluştu: {e}") |
|
|
|
raise gr.Error(f"Dönüştürme sırasında bir hata oluştu: {e}") |
|
|
|
|
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as demo: |
|
gr.Markdown( |
|
""" |
|
# 📖 PDF'den EPUB'a Dönüştürücü |
|
Facebook'un **Nougat** modelini kullanarak akademik makaleler ve diğer PDF dosyalarınızı |
|
kolayca okunabilir EPUB formatına dönüştürün. |
|
""" |
|
) |
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
pdf_input = gr.File( |
|
label="PDF Dosyası Yükle", |
|
file_types=[".pdf"] |
|
) |
|
convert_button = gr.Button("Dönüştür", variant="primary") |
|
with gr.Column(scale=1): |
|
epub_output = gr.File( |
|
label="İndirilebilir EPUB Dosyası", |
|
interactive=False |
|
) |
|
|
|
|
|
|
|
convert_button.click( |
|
fn=convert_pdf_to_epub, |
|
inputs=pdf_input, |
|
outputs=epub_output, |
|
api_name="pdf_to_epub" |
|
) |
|
|
|
gr.Examples( |
|
[["ornek_makale.pdf"]], |
|
inputs=[pdf_input], |
|
label="Örnek Dosyalar (Space'e bir örnek PDF yüklediyseniz buraya ekleyebilirsiniz)" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|