Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import os
|
| 4 |
+
import shutil
|
| 5 |
+
|
| 6 |
+
# Hàm kiểm tra mật khẩu
|
| 7 |
+
def check_password(password):
|
| 8 |
+
if password == "thienphuc":
|
| 9 |
+
return gr.update(visible=False), gr.update(visible=True), ""
|
| 10 |
+
return gr.update(visible=True), gr.update(visible=False), "❌ Mã không đúng, thử lại nhé!"
|
| 11 |
+
|
| 12 |
+
def translate_pdf(pdf_file, provider, custom_api_key):
|
| 13 |
+
if not pdf_file:
|
| 14 |
+
return None, "Vui lòng tải lên một tệp PDF."
|
| 15 |
+
|
| 16 |
+
if provider == "DeepSeek (deepseek-chat)":
|
| 17 |
+
endpoint = "https://api.deepseek.com/v1"
|
| 18 |
+
model_name = "deepseek-chat"
|
| 19 |
+
elif provider == "Qwen (qwen-plus)":
|
| 20 |
+
endpoint = "https://dashscope.aliyuncs.com/compatible-mode/v1"
|
| 21 |
+
model_name = "qwen-plus"
|
| 22 |
+
else:
|
| 23 |
+
return None, "Vui lòng chọn mô hình hợp lệ."
|
| 24 |
+
|
| 25 |
+
api_key = custom_api_key if custom_api_key.strip() else os.environ.get("DEFAULT_API_KEY", "")
|
| 26 |
+
|
| 27 |
+
if not api_key:
|
| 28 |
+
return None, "Lỗi: Không tìm thấy API Key. Vui lòng nhập API Key để tiếp tục."
|
| 29 |
+
|
| 30 |
+
file_path = pdf_file.name
|
| 31 |
+
output_dir = "output_translated"
|
| 32 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 33 |
+
|
| 34 |
+
base_name = os.path.basename(file_path)
|
| 35 |
+
name_without_ext = os.path.splitext(base_name)[0]
|
| 36 |
+
expected_output_file = os.path.join(os.path.dirname(file_path), f"{name_without_ext}-vi.pdf")
|
| 37 |
+
|
| 38 |
+
command = [
|
| 39 |
+
"pdf2zh", file_path,
|
| 40 |
+
"-lo", "vi",
|
| 41 |
+
"-s", "openai",
|
| 42 |
+
"-tk", api_key,
|
| 43 |
+
"-ep", endpoint,
|
| 44 |
+
"-m", model_name
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
subprocess.run(command, check=True, capture_output=True, text=True)
|
| 49 |
+
if os.path.exists(expected_output_file):
|
| 50 |
+
final_path = os.path.join(output_dir, f"{name_without_ext}_ThienPhuc_Translated.pdf")
|
| 51 |
+
shutil.move(expected_output_file, final_path)
|
| 52 |
+
return final_path, "✅ Dịch thành công! Cấu trúc gốc đã được giữ nguyên."
|
| 53 |
+
else:
|
| 54 |
+
return None, "❌ Lỗi: Xử lý xong nhưng không tìm thấy file PDF đầu ra."
|
| 55 |
+
except subprocess.CalledProcessError as e:
|
| 56 |
+
return None, f"❌ Có lỗi xảy ra trong quá trình dịch:\n{e.stderr}"
|
| 57 |
+
|
| 58 |
+
# Thiết kế giao diện chính
|
| 59 |
+
with gr.Blocks(title="PDF Translator by ThienPhuc", theme=gr.themes.Soft()) as app:
|
| 60 |
+
|
| 61 |
+
# 1. Giao diện nhập mã (Hiển thị đầu tiên)
|
| 62 |
+
with gr.Group(visible=True) as login_screen:
|
| 63 |
+
gr.Markdown("## 🔒 Hệ thống nội bộ")
|
| 64 |
+
gr.Markdown("Vui lòng nhập mã truy cập để sử dụng hệ thống dịch PDF.")
|
| 65 |
+
pwd_input = gr.Textbox(label="Mã truy cập", type="password", placeholder="Nhập mã...")
|
| 66 |
+
error_msg = gr.Markdown("")
|
| 67 |
+
login_btn = gr.Button("Mở khóa 🔓", variant="primary")
|
| 68 |
+
|
| 69 |
+
# 2. Giao diện công cụ (Bị ẩn đi, chỉ hiện khi nhập đúng mã)
|
| 70 |
+
with gr.Group(visible=False) as main_screen:
|
| 71 |
+
gr.Markdown("# 📄 Hệ thống Dịch PDF của ThienPhuc")
|
| 72 |
+
gr.Markdown("Tải tệp PDF lên, chọn bộ não AI (DeepSeek/Qwen) và nhập API Key để dịch sang Tiếng Việt mà không làm vỡ cấu trúc.")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
with gr.Column(scale=1):
|
| 76 |
+
file_input = gr.File(label="Tải lên tệp PDF cần dịch", file_types=[".pdf"])
|
| 77 |
+
provider_dropdown = gr.Dropdown(
|
| 78 |
+
choices=["DeepSeek (deepseek-chat)", "Qwen (qwen-plus)"],
|
| 79 |
+
value="DeepSeek (deepseek-chat)",
|
| 80 |
+
label="Chọn Mô hình AI"
|
| 81 |
+
)
|
| 82 |
+
api_key_input = gr.Textbox(
|
| 83 |
+
label="Nhập API Key",
|
| 84 |
+
placeholder="Dán API Key của bạn (hoặc bạn bè) vào đây...",
|
| 85 |
+
type="password"
|
| 86 |
+
)
|
| 87 |
+
translate_btn = gr.Button("🚀 Bắt đầu dịch PDF", variant="primary")
|
| 88 |
+
|
| 89 |
+
with gr.Column(scale=1):
|
| 90 |
+
file_output = gr.File(label="Tải xuống bản dịch")
|
| 91 |
+
status_output = gr.Textbox(label="Trạng thái hệ thống", interactive=False)
|
| 92 |
+
|
| 93 |
+
translate_btn.click(
|
| 94 |
+
fn=translate_pdf,
|
| 95 |
+
inputs=[file_input, provider_dropdown, api_key_input],
|
| 96 |
+
outputs=[file_output, status_output]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
# Xử lý sự kiện bấm nút mở khóa
|
| 100 |
+
login_btn.click(
|
| 101 |
+
fn=check_password,
|
| 102 |
+
inputs=[pwd_input],
|
| 103 |
+
outputs=[login_screen, main_screen, error_msg]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
app.launch()
|