Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from pdf2docx import Converter
|
| 3 |
import os
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
-
# --- Hàm xử lý chính (
|
| 7 |
-
def
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
| 15 |
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
progress(1.0, desc="Xong!")
|
| 29 |
-
return
|
| 30 |
-
|
| 31 |
except Exception as e:
|
| 32 |
return None, f"❌ Có lỗi xảy ra: {str(e)}"
|
| 33 |
|
| 34 |
# --- Cấu hình giao diện (UI) ---
|
| 35 |
|
| 36 |
-
# Tùy chỉnh CSS để giao diện đẹp hơn
|
| 37 |
custom_css = """
|
| 38 |
#convert_btn {
|
| 39 |
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 40 |
color: white;
|
| 41 |
border: none;
|
| 42 |
font-weight: bold;
|
|
|
|
| 43 |
}
|
| 44 |
-
footer {visibility: hidden}
|
| 45 |
"""
|
| 46 |
|
| 47 |
-
# Sử dụng Theme Soft để giao diện mềm mại
|
| 48 |
theme = gr.themes.Soft(
|
| 49 |
primary_hue="blue",
|
| 50 |
secondary_hue="slate",
|
|
@@ -55,46 +82,44 @@ theme = gr.themes.Soft(
|
|
| 55 |
|
| 56 |
with gr.Blocks(theme=theme, css=custom_css, title="PDF to Word Pro") as demo:
|
| 57 |
|
| 58 |
-
# ---
|
| 59 |
with gr.Row():
|
| 60 |
-
with gr.Column(scale=1):
|
| 61 |
-
pass # Cột trống để căn lề
|
| 62 |
with gr.Column(scale=8):
|
| 63 |
gr.Markdown(
|
| 64 |
"""
|
| 65 |
-
# 📝 PDF TO WORD CONVERTER
|
| 66 |
-
### Chuyên dụng cho Đề thi Tiếng Anh & Tài liệu Học tập
|
| 67 |
"""
|
| 68 |
)
|
| 69 |
-
with gr.Column(scale=1):
|
| 70 |
-
pass
|
| 71 |
|
| 72 |
-
gr.HTML("<hr>")
|
| 73 |
|
| 74 |
-
# ---
|
| 75 |
with gr.Row():
|
| 76 |
-
# Cột
|
| 77 |
with gr.Column(variant="panel"):
|
| 78 |
gr.Markdown("### 1. Tải tài liệu")
|
| 79 |
-
|
| 80 |
-
|
|
|
|
| 81 |
file_types=[".pdf"],
|
| 82 |
-
file_count="
|
| 83 |
height=250
|
| 84 |
)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
-
# Cột bên phải: Kết quả
|
| 88 |
with gr.Column(variant="panel"):
|
| 89 |
gr.Markdown("### 2. Tải về kết quả")
|
| 90 |
-
output_file = gr.File(label="File Word
|
| 91 |
status_text = gr.Textbox(label="Trạng thái", interactive=False)
|
| 92 |
|
| 93 |
-
# Nút
|
| 94 |
with gr.Row():
|
| 95 |
btn = gr.Button("🚀 CHUYỂN ĐỔI NGAY", elem_id="convert_btn", scale=1)
|
| 96 |
|
| 97 |
-
# ---
|
| 98 |
gr.HTML("<br><br>")
|
| 99 |
|
| 100 |
with gr.Accordion("ℹ️ Thông tin phát triển & Bản quyền", open=False):
|
|
@@ -103,22 +128,22 @@ with gr.Blocks(theme=theme, css=custom_css, title="PDF to Word Pro") as demo:
|
|
| 103 |
### 👨💻 Thông tin tác giả
|
| 104 |
* **Phát triển bởi:** Chu Viết Kiên
|
| 105 |
* **Liên hệ:** kiencv.3107@gmail.com
|
| 106 |
-
* **Phiên bản:** 1.
|
| 107 |
|
| 108 |
### 📜 Bản quyền & Miễn trừ trách nhiệm
|
| 109 |
* Công cụ này sử dụng thư viện mã nguồn mở `pdf2docx`.
|
|
|
|
| 110 |
* Chúng tôi không lưu trữ tài liệu của bạn. File sẽ tự động xóa sau khi phiên làm việc kết thúc.
|
| 111 |
* © 2024 Chu Viet Kien. All rights reserved.
|
| 112 |
"""
|
| 113 |
)
|
| 114 |
|
| 115 |
-
# ---
|
| 116 |
btn.click(
|
| 117 |
-
fn=
|
| 118 |
-
inputs=[
|
| 119 |
outputs=[output_file, status_text]
|
| 120 |
)
|
| 121 |
|
| 122 |
-
# Chạy ứng dụng
|
| 123 |
if __name__ == "__main__":
|
| 124 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from pdf2docx import Converter
|
| 3 |
import os
|
| 4 |
+
import zipfile
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
# --- Hàm xử lý chính (Hỗ trợ Batch Processing) ---
|
| 8 |
+
def convert_pdfs_to_word(pdf_files, progress=gr.Progress()):
|
| 9 |
+
# Kiểm tra đầu vào
|
| 10 |
+
if not pdf_files:
|
| 11 |
+
return None, "⚠️ Vui lòng tải ít nhất một file lên!"
|
| 12 |
|
| 13 |
+
# Nếu pdf_files không phải là list (trường hợp lỗi hiếm gặp), đưa nó vào list
|
| 14 |
+
if not isinstance(pdf_files, list):
|
| 15 |
+
pdf_files = [pdf_files]
|
| 16 |
+
|
| 17 |
+
converted_files = []
|
| 18 |
+
total_files = len(pdf_files)
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# Vòng lặp xử lý từng file
|
| 22 |
+
for index, pdf_file in enumerate(pdf_files):
|
| 23 |
+
original_name = os.path.basename(pdf_file.name)
|
| 24 |
+
docx_filename = os.path.splitext(original_name)[0] + ".docx"
|
| 25 |
+
|
| 26 |
+
# Cập nhật thanh tiến trình
|
| 27 |
+
msg = f"Đang xử lý file {index + 1}/{total_files}: {original_name}"
|
| 28 |
+
progress((index / total_files), desc=msg)
|
| 29 |
+
|
| 30 |
+
# Thực hiện convert
|
| 31 |
+
cv = Converter(pdf_file.name)
|
| 32 |
+
cv.convert(docx_filename, start=0, end=None)
|
| 33 |
+
cv.close()
|
| 34 |
+
|
| 35 |
+
converted_files.append(docx_filename)
|
| 36 |
+
|
| 37 |
+
# --- Giai đoạn đóng gói kết quả ---
|
| 38 |
+
progress(0.95, desc="Đang đóng gói file...")
|
| 39 |
|
| 40 |
+
# Trường hợp 1: Chỉ có 1 file -> Trả về file docx
|
| 41 |
+
if len(converted_files) == 1:
|
| 42 |
+
final_output = converted_files[0]
|
| 43 |
+
status_msg = f"✅ Hoàn tất! Đã chuyển đổi: {os.path.basename(final_output)}"
|
| 44 |
+
|
| 45 |
+
# Trường hợp 2: Có nhiều file -> Nén thành ZIP
|
| 46 |
+
else:
|
| 47 |
+
zip_filename = "All_Converted_Documents.zip"
|
| 48 |
+
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
|
| 49 |
+
for file in converted_files:
|
| 50 |
+
# arcname là tên file bên trong file zip (bỏ đường dẫn dài dòng)
|
| 51 |
+
zipf.write(file, arcname=os.path.basename(file))
|
| 52 |
+
|
| 53 |
+
final_output = zip_filename
|
| 54 |
+
status_msg = f"✅ Hoàn tất! Đã nén {len(converted_files)} file vào ZIP."
|
| 55 |
+
|
| 56 |
progress(1.0, desc="Xong!")
|
| 57 |
+
return final_output, status_msg
|
| 58 |
+
|
| 59 |
except Exception as e:
|
| 60 |
return None, f"❌ Có lỗi xảy ra: {str(e)}"
|
| 61 |
|
| 62 |
# --- Cấu hình giao diện (UI) ---
|
| 63 |
|
|
|
|
| 64 |
custom_css = """
|
| 65 |
#convert_btn {
|
| 66 |
background: linear-gradient(90deg, #4b6cb7 0%, #182848 100%);
|
| 67 |
color: white;
|
| 68 |
border: none;
|
| 69 |
font-weight: bold;
|
| 70 |
+
font-size: 16px;
|
| 71 |
}
|
| 72 |
+
footer {visibility: hidden}
|
| 73 |
"""
|
| 74 |
|
|
|
|
| 75 |
theme = gr.themes.Soft(
|
| 76 |
primary_hue="blue",
|
| 77 |
secondary_hue="slate",
|
|
|
|
| 82 |
|
| 83 |
with gr.Blocks(theme=theme, css=custom_css, title="PDF to Word Pro") as demo:
|
| 84 |
|
| 85 |
+
# --- Header ---
|
| 86 |
with gr.Row():
|
| 87 |
+
with gr.Column(scale=1): pass
|
|
|
|
| 88 |
with gr.Column(scale=8):
|
| 89 |
gr.Markdown(
|
| 90 |
"""
|
| 91 |
+
# 📝 PDF TO WORD CONVERTER PRO
|
| 92 |
+
### Chuyên dụng cho Đề thi Tiếng Anh & Tài liệu Học tập (Hỗ trợ nhiều file)
|
| 93 |
"""
|
| 94 |
)
|
| 95 |
+
with gr.Column(scale=1): pass
|
|
|
|
| 96 |
|
| 97 |
+
gr.HTML("<hr>")
|
| 98 |
|
| 99 |
+
# --- Main Content ---
|
| 100 |
with gr.Row():
|
| 101 |
+
# Cột Input
|
| 102 |
with gr.Column(variant="panel"):
|
| 103 |
gr.Markdown("### 1. Tải tài liệu")
|
| 104 |
+
# file_count="multiple" cho phép chọn nhiều file cùng lúc
|
| 105 |
+
input_files = gr.File(
|
| 106 |
+
label="Chọn các file PDF (Giữ Ctrl để chọn nhiều)",
|
| 107 |
file_types=[".pdf"],
|
| 108 |
+
file_count="multiple",
|
| 109 |
height=250
|
| 110 |
)
|
| 111 |
|
| 112 |
+
# Cột Output
|
|
|
|
| 113 |
with gr.Column(variant="panel"):
|
| 114 |
gr.Markdown("### 2. Tải về kết quả")
|
| 115 |
+
output_file = gr.File(label="File Word hoặc ZIP", height=150)
|
| 116 |
status_text = gr.Textbox(label="Trạng thái", interactive=False)
|
| 117 |
|
| 118 |
+
# Nút Action
|
| 119 |
with gr.Row():
|
| 120 |
btn = gr.Button("🚀 CHUYỂN ĐỔI NGAY", elem_id="convert_btn", scale=1)
|
| 121 |
|
| 122 |
+
# --- Footer Info ---
|
| 123 |
gr.HTML("<br><br>")
|
| 124 |
|
| 125 |
with gr.Accordion("ℹ️ Thông tin phát triển & Bản quyền", open=False):
|
|
|
|
| 128 |
### 👨💻 Thông tin tác giả
|
| 129 |
* **Phát triển bởi:** Chu Viết Kiên
|
| 130 |
* **Liên hệ:** kiencv.3107@gmail.com
|
| 131 |
+
* **Phiên bản:** 1.1.0 (Batch Support)
|
| 132 |
|
| 133 |
### 📜 Bản quyền & Miễn trừ trách nhiệm
|
| 134 |
* Công cụ này sử dụng thư viện mã nguồn mở `pdf2docx`.
|
| 135 |
+
* Nếu bạn tải lên nhiều file, hệ thống sẽ tự động nén thành file `.zip`.
|
| 136 |
* Chúng tôi không lưu trữ tài liệu của bạn. File sẽ tự động xóa sau khi phiên làm việc kết thúc.
|
| 137 |
* © 2024 Chu Viet Kien. All rights reserved.
|
| 138 |
"""
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# --- Events ---
|
| 142 |
btn.click(
|
| 143 |
+
fn=convert_pdfs_to_word,
|
| 144 |
+
inputs=[input_files],
|
| 145 |
outputs=[output_file, status_text]
|
| 146 |
)
|
| 147 |
|
|
|
|
| 148 |
if __name__ == "__main__":
|
| 149 |
demo.launch()
|