VietKien commited on
Commit
46bcb9e
·
verified ·
1 Parent(s): 3649e04

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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__":
40
+ demo.launch()