Spaces:
Running
Running
update UI/UX
Browse files- home.py +18 -13
- pages/upload.py +45 -121
home.py
CHANGED
|
@@ -2,21 +2,26 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
-
st.
|
| 6 |
|
| 7 |
-
st.
|
| 8 |
-
num_rows = st.sidebar.slider("Number of rows", min_value=10, max_value=100, value=20)
|
| 9 |
-
num_cols = st.sidebar.slider("Number of columns", min_value=2, max_value=10, value=3)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
st.
|
| 16 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
st.subheader("Line Chart of the Data")
|
| 19 |
-
st.line_chart(df)
|
| 20 |
|
| 21 |
-
st.subheader("Statistics")
|
| 22 |
-
st.write(df.describe())
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
|
| 5 |
+
st.set_page_config(page_title="Machine Translation App", page_icon="🌍", layout="centered")
|
| 6 |
|
| 7 |
+
st.title("🌍 Machine Translation App")
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
st.write(
|
| 10 |
+
"Chào mừng bạn đến với ứng dụng dịch máy tự động!\n"
|
| 11 |
+
"Ứng dụng này giúp bạn dịch tài liệu một cách nhanh chóng và chính xác."
|
| 12 |
+
)
|
| 13 |
|
| 14 |
+
st.header("📌 Hướng dẫn sử dụng")
|
| 15 |
+
st.write("1. Truy cập [trang upload](#).")
|
| 16 |
+
st.write("2. Chọn tệp tin cần dịch (hỗ trợ .docx, .txt, .pdf).")
|
| 17 |
+
st.write("3. Chọn ngôn ngữ muốn dịch sang.")
|
| 18 |
+
st.write("4. Nhấn **Upload** để bắt đầu dịch.")
|
| 19 |
+
st.write("5. Chờ một vài phút để hệ thống xử lý.")
|
| 20 |
+
st.write("6. Khi hoàn tất, nhấn **Tải về** để nhận tệp đã dịch.")
|
| 21 |
+
|
| 22 |
+
# Điều hướng đến trang Upload
|
| 23 |
+
to_upload = st.button("📤 Đi đến trang Upload")
|
| 24 |
+
if to_upload:
|
| 25 |
+
st.switch_page("upload.py") # Điều hướng đến trang upload
|
| 26 |
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
pages/upload.py
CHANGED
|
@@ -8,129 +8,53 @@ from translate.translator import translate_text_dict
|
|
| 8 |
from powerpoint.pptx_object import create_translated_ppt
|
| 9 |
from excel.excel_translate import translate_xlsx, translate_csv
|
| 10 |
from word.word_translate import translate_docx_from_mongodb
|
| 11 |
-
|
| 12 |
import dotenv
|
| 13 |
import os
|
| 14 |
|
| 15 |
dotenv.load_dotenv(".env")
|
| 16 |
-
# st.set_option("server.maxUploadSize", 1024)
|
| 17 |
-
|
| 18 |
-
# Cấu hình API key
|
| 19 |
-
api_key = os.getenv("GEMINI_API_KEY")
|
| 20 |
-
genai.configure(api_key=api_key)
|
| 21 |
-
model = genai.GenerativeModel("gemini-2.0-flash")
|
| 22 |
-
|
| 23 |
-
# Giao diện Streamlit
|
| 24 |
-
st.title("Please chose your PPTX, Excel file to translate")
|
| 25 |
-
|
| 26 |
-
# st.set_option("server.fileUploader.allowMediaFiles", True)
|
| 27 |
-
uploaded_file = st.file_uploader("Chọn file")
|
| 28 |
-
target_lang = st.selectbox("Chọn ngôn ngữ bạn muốn dịch sang", ["english", "vietnamese"])
|
| 29 |
-
|
| 30 |
-
final_pptx_id = None # Biến lưu ID file sau khi xử lý
|
| 31 |
-
|
| 32 |
-
if uploaded_file is not None:
|
| 33 |
-
if st.button("Upload"):
|
| 34 |
-
file_type = detect_file_type(uploaded_file)
|
| 35 |
-
st.write(f"Detected file type: {file_type}")
|
| 36 |
-
if file_type == "PPTX":
|
| 37 |
-
|
| 38 |
-
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="ppt", collection_name="root_file")
|
| 39 |
-
st.write(f"File ID: {file_id}")
|
| 40 |
-
|
| 41 |
-
xml_file_id = ppt_to_xml_mongodb(file_id)
|
| 42 |
-
text_dict = extract_text_from_xml(file_id=xml_file_id)
|
| 43 |
-
translated_dict = translate_text_dict(text_dict, target_lang=target_lang, gemini_api=api_key)
|
| 44 |
-
|
| 45 |
-
final_xml_id = update_xml_with_translated_text_mongodb(xml_file_id, translated_dict)
|
| 46 |
-
st.write(f"Final XML ID: {final_xml_id}")
|
| 47 |
-
|
| 48 |
-
# Lưu ID file PPTX cuối cùng
|
| 49 |
-
final_pptx_id = create_translated_ppt(
|
| 50 |
-
db_name="ppt", original_ppt_id=file_id,
|
| 51 |
-
translated_xml_id=final_xml_id, output_collection="final_pptx"
|
| 52 |
-
)
|
| 53 |
-
st.write(f"Final PPTX ID: {final_pptx_id}")
|
| 54 |
-
|
| 55 |
-
# Hiển thị ảnh slide trước khi tải xuống
|
| 56 |
-
if final_pptx_id:
|
| 57 |
-
st.write("✅ File đã sẵn sàng để tải xuống!")
|
| 58 |
-
|
| 59 |
-
pptx_io, pptx_filename = fetch_file_from_mongodb("ppt", "final_pptx", final_pptx_id)
|
| 60 |
-
|
| 61 |
-
if pptx_io:
|
| 62 |
-
# Nút tải file sau khi xem trước
|
| 63 |
-
st.download_button(
|
| 64 |
-
label="Click to Download",
|
| 65 |
-
data=pptx_io.getvalue(), # Chuyển thành bytes để tải về
|
| 66 |
-
file_name=pptx_filename,
|
| 67 |
-
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
| 68 |
-
)
|
| 69 |
-
else:
|
| 70 |
-
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
elif file_type == "Excel":
|
| 75 |
-
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="excel", collection_name="root_file")
|
| 76 |
-
st.write(f"File ID: {file_id}")
|
| 77 |
-
|
| 78 |
-
final_id = translate_xlsx(file_id=file_id, from_lang="en", target_lang="vi", gemini_api=api_key)
|
| 79 |
-
st.write(f"Final Excel ID: {final_id}")
|
| 80 |
-
if final_id:
|
| 81 |
-
st.write("✅ File đã sẵn sàng để tải xuống!")
|
| 82 |
-
|
| 83 |
-
excel_io, excel_filename = fetch_file_from_mongodb("excel", "final_file", final_id)
|
| 84 |
-
|
| 85 |
-
if excel_io:
|
| 86 |
-
st.download_button(
|
| 87 |
-
label="Click to Download",
|
| 88 |
-
data=excel_io.getvalue(),
|
| 89 |
-
file_name=excel_filename,
|
| 90 |
-
mime="application/vnd.ms-excel"
|
| 91 |
-
)
|
| 92 |
-
else:
|
| 93 |
-
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
|
| 94 |
-
elif file_type == "CSV":
|
| 95 |
-
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="csv", collection_name="root_file")
|
| 96 |
-
st.write(f"File ID: {file_id}")
|
| 97 |
-
|
| 98 |
-
final_id = translate_csv(file_id=file_id, source_lang="en", target_lang="vi", gemini_api=api_key)
|
| 99 |
-
st.write(f"Final CSV ID: {final_id}")
|
| 100 |
-
if final_id:
|
| 101 |
-
st.write("✅ File đã sẵn sàng để tải xuống!")
|
| 102 |
-
|
| 103 |
-
csv_io, csv_filename = fetch_file_from_mongodb("csv", "final_file", final_id)
|
| 104 |
-
|
| 105 |
-
if csv_io:
|
| 106 |
-
st.download_button(
|
| 107 |
-
label="Click to Download",
|
| 108 |
-
data=csv_io.getvalue(),
|
| 109 |
-
file_name=csv_filename,
|
| 110 |
-
mime="text/csv"
|
| 111 |
-
)
|
| 112 |
-
else:
|
| 113 |
-
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
|
| 114 |
-
|
| 115 |
-
elif file_type == "Word":
|
| 116 |
-
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="word", collection_name="root_file")
|
| 117 |
-
st.write(f"File ID: {file_id}")
|
| 118 |
-
|
| 119 |
-
final_id = translate_docx_from_mongodb(file_id = file_id, target_lang="Vietnamese")
|
| 120 |
-
st.write(f"Final CSV ID: {final_id}")
|
| 121 |
-
if final_id:
|
| 122 |
-
st.write("✅ File đã sẵn sàng để tải xuống!")
|
| 123 |
-
|
| 124 |
-
docx_io, docx_filename = fetch_file_from_mongodb("word", "final_file", final_id)
|
| 125 |
-
|
| 126 |
-
if docx_io:
|
| 127 |
-
st.download_button(
|
| 128 |
-
label="Click to Download",
|
| 129 |
-
data=docx_io.getvalue(),
|
| 130 |
-
file_name=docx_filename,
|
| 131 |
-
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 132 |
-
)
|
| 133 |
-
else:
|
| 134 |
-
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
|
| 135 |
|
| 136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from powerpoint.pptx_object import create_translated_ppt
|
| 9 |
from excel.excel_translate import translate_xlsx, translate_csv
|
| 10 |
from word.word_translate import translate_docx_from_mongodb
|
|
|
|
| 11 |
import dotenv
|
| 12 |
import os
|
| 13 |
|
| 14 |
dotenv.load_dotenv(".env")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 17 |
+
|
| 18 |
+
st.title("Translate Your File Easily! 🌍")
|
| 19 |
+
|
| 20 |
+
uploaded_file = st.file_uploader("📂 Chọn file để dịch")
|
| 21 |
+
target_lang = st.selectbox("🌐 Chọn ngôn ngữ", ["english", "vietnamese"])
|
| 22 |
+
|
| 23 |
+
def process_file(file, file_type):
|
| 24 |
+
progress_bar = st.progress(0)
|
| 25 |
+
file_id = save_file_to_mongodb(uploaded_file=file, db_name=file_type.lower(), collection_name="root_file")
|
| 26 |
+
progress_bar.progress(20)
|
| 27 |
+
st.write(f"📂 File ID: {file_id}")
|
| 28 |
+
|
| 29 |
+
if file_type == "PPTX":
|
| 30 |
+
xml_file_id = ppt_to_xml_mongodb(file_id)
|
| 31 |
+
progress_bar.progress(40)
|
| 32 |
+
text_dict = extract_text_from_xml(file_id=xml_file_id)
|
| 33 |
+
translated_dict = translate_text_dict(text_dict, target_lang=target_lang)
|
| 34 |
+
progress_bar.progress(60)
|
| 35 |
+
final_xml_id = update_xml_with_translated_text_mongodb(xml_file_id, translated_dict)
|
| 36 |
+
final_id = create_translated_ppt("ppt", file_id, final_xml_id, "final_pptx")
|
| 37 |
+
elif file_type == "Excel":
|
| 38 |
+
final_id = translate_xlsx(file_id, "en", target_lang, os.getenv("GEMINI_API_KEY"))
|
| 39 |
+
elif file_type == "CSV":
|
| 40 |
+
final_id = translate_csv(file_id, "en", target_lang, os.getenv("GEMINI_API_KEY"))
|
| 41 |
+
elif file_type == "Word":
|
| 42 |
+
final_id = translate_docx_from_mongodb(file_id, target_lang)
|
| 43 |
+
else:
|
| 44 |
+
st.error("❌ Loại file không hỗ trợ!")
|
| 45 |
+
return
|
| 46 |
+
|
| 47 |
+
progress_bar.progress(80)
|
| 48 |
+
st.write("✅ File đã được dịch xong!")
|
| 49 |
+
file_io, file_name = fetch_file_from_mongodb(file_type.lower(), "final_file", final_id)
|
| 50 |
+
progress_bar.progress(100)
|
| 51 |
+
|
| 52 |
+
if file_io:
|
| 53 |
+
st.download_button("⬇️ Tải file về", data=file_io.getvalue(), file_name=file_name)
|
| 54 |
+
else:
|
| 55 |
+
st.error("❌ Không thể tải xuống file. Vui lòng thử lại!")
|
| 56 |
+
|
| 57 |
+
if uploaded_file and st.button("🚀 Upload và dịch ngay!"):
|
| 58 |
+
file_type = detect_file_type(uploaded_file)
|
| 59 |
+
st.write(f"🔍 Loại file phát hiện: {file_type}")
|
| 60 |
+
process_file(uploaded_file, file_type)
|