Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PyPDF2 import PdfReader
|
3 |
from pdf2image import convert_from_path
|
4 |
from PIL import Image
|
5 |
|
6 |
def process_file(file, file_type):
|
|
|
|
|
|
|
7 |
if file_type == "PDF":
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
13 |
elif file_type == "Image":
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
# Buat antarmuka Gradio
|
19 |
file_type_radio = gr.Radio(choices=["Image", "PDF"], label="Pilih tipe file")
|
@@ -28,4 +36,4 @@ interface = gr.Interface(
|
|
28 |
)
|
29 |
|
30 |
# Jalankan antarmuka dengan opsi share=True
|
31 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from pdf2image import convert_from_path
|
3 |
from PIL import Image
|
4 |
|
5 |
def process_file(file, file_type):
|
6 |
+
if file is None:
|
7 |
+
return None, "File tidak diunggah dengan benar. Silakan coba lagi."
|
8 |
+
|
9 |
if file_type == "PDF":
|
10 |
+
try:
|
11 |
+
# Mengonversi PDF ke gambar
|
12 |
+
images = convert_from_path(file.name)
|
13 |
+
# Menyimpan gambar pertama sebagai output
|
14 |
+
image = images[0] # Mengambil halaman pertama
|
15 |
+
return image, None
|
16 |
+
except Exception as e:
|
17 |
+
return None, f"Terjadi kesalahan saat memproses file PDF: {str(e)}"
|
18 |
elif file_type == "Image":
|
19 |
+
try:
|
20 |
+
# Menampilkan gambar
|
21 |
+
image = Image.open(file.name)
|
22 |
+
return image, None
|
23 |
+
except Exception as e:
|
24 |
+
return None, f"Terjadi kesalahan saat memproses file gambar: {str(e)}"
|
25 |
|
26 |
# Buat antarmuka Gradio
|
27 |
file_type_radio = gr.Radio(choices=["Image", "PDF"], label="Pilih tipe file")
|
|
|
36 |
)
|
37 |
|
38 |
# Jalankan antarmuka dengan opsi share=True
|
39 |
+
interface.launch(share=True)
|