VietKien commited on
Commit
0d41114
·
verified ·
1 Parent(s): 46bcb9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -15
app.py CHANGED
@@ -1,39 +1,123 @@
1
  import gradio as gr
2
  from pdf2docx import Converter
3
  import os
 
4
 
5
- def convert_pdf_to_word(pdf_file):
 
6
  if pdf_file is None:
7
- return None
8
 
9
  # 1. Định nghĩa tên file đầu ra
10
- docx_filename = "de_thi_converted.docx"
 
 
11
 
12
  try:
 
13
  # 2. Khởi tạo converter
14
  cv = Converter(pdf_file.name)
15
 
 
16
  # 3. Thực hiện chuyển đổi
17
- # start=0, end=None nghĩa là chuyển đổi tất cả các trang
18
  cv.convert(docx_filename, start=0, end=None)
 
 
19
  cv.close()
20
 
21
- return docx_filename
 
 
22
  except Exception as e:
23
- return f"Có lỗi xảy ra: {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
- # Xây dựng giao diện với Gradio
26
- with gr.Blocks(title="Công cụ chuyển đề Tiếng Anh PDF sang Word") as demo:
27
- gr.Markdown("## 📄 Chuyển đổi Đề thi Tiếng Anh PDF sang Word")
28
- gr.Markdown("Tải file PDF lên và nhận về file Word giữ nguyên định dạng (cột, bảng, trắc nghiệm).")
 
 
 
 
 
 
29
 
 
30
  with gr.Row():
31
- input_file = gr.File(label="Upload file PDF", file_types=[".pdf"])
32
- output_file = gr.File(label="Tải về file Word")
33
-
34
- btn = gr.Button("🚀 Chuyển đổi ngay")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- btn.click(fn=convert_pdf_to_word, inputs=input_file, outputs=output_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  # Chạy ứng dụng
39
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  from pdf2docx import Converter
3
  import os
4
+ import time
5
 
6
+ # --- Hàm xử lý chính (có thêm thanh tiến trình) ---
7
+ def convert_pdf_to_word(pdf_file, progress=gr.Progress()):
8
  if pdf_file is None:
9
+ return None, "⚠️ Vui lòng tải file lên trước!"
10
 
11
  # 1. Định nghĩa tên file đầu ra
12
+ # Lấy tên gốc của file pdf để đặt tên cho file docx cho chuyên nghiệp
13
+ original_name = os.path.basename(pdf_file.name)
14
+ docx_filename = os.path.splitext(original_name)[0] + "_converted.docx"
15
 
16
  try:
17
+ progress(0.1, desc="Đang khởi tạo...")
18
  # 2. Khởi tạo converter
19
  cv = Converter(pdf_file.name)
20
 
21
+ progress(0.3, desc="Đang phân tích layout...")
22
  # 3. Thực hiện chuyển đổi
 
23
  cv.convert(docx_filename, start=0, end=None)
24
+
25
+ progress(0.8, desc="Đang hoàn tất...")
26
  cv.close()
27
 
28
+ progress(1.0, desc="Xong!")
29
+ return docx_filename, f"✅ Chuyển đổi thành công: {docx_filename}"
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} /* Ẩn footer mặc định của Gradio */
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",
51
+ ).set(
52
+ button_primary_background_fill="*primary_500",
53
+ button_primary_background_fill_hover="*primary_600",
54
+ )
55
+
56
+ with gr.Blocks(theme=theme, css=custom_css, title="PDF to Word Pro") as demo:
57
 
58
+ # --- Phần Tiêu đề ---
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>") # Đường kẻ ngang
73
+
74
+ # --- Phần Chức năng chính ---
75
+ with gr.Row():
76
+ # Cột bên trái: Upload
77
+ with gr.Column(variant="panel"):
78
+ gr.Markdown("### 1. Tải tài liệu")
79
+ input_file = gr.File(
80
+ label="Upload file PDF",
81
+ file_types=[".pdf"],
82
+ file_count="single",
83
+ height=250
84
+ )
85
+
86
+ # Nút bấm ở giữa (hoặc dưới input)
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 đã chuyển đổi", height=150)
91
+ status_text = gr.Textbox(label="Trạng thái", interactive=False)
92
+
93
+ # Nút bấm to nằm riêng một hàng
94
+ with gr.Row():
95
+ btn = gr.Button("🚀 CHUYỂN ĐỔI NGAY", elem_id="convert_btn", scale=1)
96
+
97
+ # --- Phần thông tin tác giả & Bản quyền ---
98
+ gr.HTML("<br><br>")
99
 
100
+ with gr.Accordion("ℹ️ Thông tin phát triển & Bản quyền", open=False):
101
+ gr.Markdown(
102
+ """
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.0.0
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
+ # --- Kết nối sự kiện ---
116
+ btn.click(
117
+ fn=convert_pdf_to_word,
118
+ inputs=[input_file],
119
+ outputs=[output_file, status_text]
120
+ )
121
 
122
  # Chạy ứng dụng
123
  if __name__ == "__main__":