Zatimm commited on
Commit
4ec8292
·
verified ·
1 Parent(s): e22969f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import google.generativeai as genai
4
+ from ebooklib import epub
5
+ import uuid
6
+ from dotenv import load_dotenv
7
+
8
+ load_dotenv()
9
+
10
+ try:
11
+ api_key = os.environ.get("GOOGLE_API_KEY")
12
+ if not api_key:
13
+ raise ValueError("GOOGLE_API_KEY secret'ı bulunamadı! Lütfen Hugging Face Space ayarlarından ekleyin.")
14
+
15
+ genai.configure(api_key=api_key)
16
+
17
+ model = genai.GenerativeModel('gemini-1.5-pro-latest')
18
+ print("Gemini 1.5 Pro modeli başarıyla yüklendi.")
19
+
20
+ except Exception as e:
21
+ print(f"API Yapılandırma Hatası: {e}")
22
+ model = None
23
+
24
+ def pdf_to_epub(pdf_file):
25
+ if model is None:
26
+ raise gr.Error("Model yüklenemedi. Lütfen API anahtarınızı ve yapılandırmayı kontrol edin.")
27
+
28
+ if pdf_file is None:
29
+ raise gr.Error("Lütfen bir PDF dosyası yükleyin.")
30
+
31
+ pdf_path = pdf_file.name
32
+
33
+ try:
34
+ print(f"'{os.path.basename(pdf_path)}' dosyası işleniyor...")
35
+
36
+ print("Dosya Gemini API'ye yükleniyor...")
37
+ uploaded_file = genai.upload_file(path=pdf_path, display_name="Uploaded PDF")
38
+ print(f"Dosya başarıyla yüklendi: {uploaded_file.display_name}")
39
+
40
+ prompt = [
41
+ uploaded_file,
42
+ "Bu yüklediğim PDF dosyasının tamamını analiz et. İçindeki metinleri, başlıkları ve paragrafları anlamsal bütünlüğü ve sırayı koruyarak baştan sona düz metin olarak çıkar. Hiçbir yorum, özet veya ek bilgi ekleme, sadece dokümanın ham metnini ver.",
43
+ ]
44
+
45
+ print("Gemini metin çıkarma işlemini başlatıyor...")
46
+ response = model.generate_content(prompt)
47
+
48
+ full_text_content = response.text
49
+ print("Metin çıkarma işlemi tamamlandı.")
50
+
51
+ print("EPUB dosyası oluşturuluyor...")
52
+ epub_filename = f"converted_{uuid.uuid4()}.epub"
53
+
54
+ book = epub.EpubBook()
55
+ book.set_identifier(f'urn:uuid:{uuid.uuid4()}')
56
+ book.set_title(os.path.basename(pdf_path).replace('.pdf', ''))
57
+ book.set_language('tr')
58
+
59
+ html_content = "<html><head></head><body>"
60
+ for paragraph in full_text_content.split('\n'):
61
+ if paragraph.strip():
62
+ html_content += f"<p>{paragraph}</p>"
63
+ html_content += "</body></html>"
64
+
65
+ chapter = epub.EpubHtml(title='İçerik', file_name='chap_1.xhtml', lang='tr')
66
+ chapter.content = html_content
67
+
68
+ book.add_item(chapter)
69
+ book.toc = [epub.Link('chap_1.xhtml', 'İçerik', 'content')]
70
+ book.spine = ['nav', chapter]
71
+ book.add_item(epub.EpubNcx())
72
+ book.add_item(epub.EpubNav())
73
+
74
+ epub.write_epub(epub_filename, book, {})
75
+ print(f"EPUB dosyası başarıyla oluşturuldu: {epub_filename}")
76
+
77
+ return epub_filename
78
+
79
+ except Exception as e:
80
+ print(f"Bir hata oluştu: {e}")
81
+ raise gr.Error(f"İşlem sırasında bir hata meydana geldi: {e}")
82
+
83
+ with gr.Blocks() as iface:
84
+ gr.Markdown(
85
+ """
86
+ # 📚 PDF'ten EPUB'a Gemini ile Dönüştürücü
87
+ Bu araç, yüklediğiniz bir PDF dosyasını analiz etmesi için Google Gemini'ye gönderir
88
+ ve metinleri birleştirerek indirilebilir bir EPUB dosyası oluşturur.
89
+ **Not:** İşlem, PDF'in sayfa sayısına bağlı olarak uzun sürebilir.
90
+ """
91
+ )
92
+
93
+ pdf_input = gr.File(label="PDF Dosyasını Yükle", file_types=[".pdf"])
94
+ epub_output = gr.File(label="İndirilebilir EPUB Dosyası")
95
+
96
+ convert_button = gr.Button("Dönüştür", variant="primary")
97
+
98
+ convert_button.click(
99
+ fn=pdf_to_epub,
100
+ inputs=pdf_input,
101
+ outputs=epub_output
102
+ )
103
+
104
+ if __name__ == "__main__":
105
+ iface.launch()