MT_deploy / pages /upload.py
mintlee's picture
'fix'
bdcb5e5
raw
history blame
4.59 kB
import streamlit as st
import google.generativeai as genai
from db.mongodb import save_file_to_mongodb, fetch_file_from_mongodb, detect_file_type
from powerpoint.pptx import translate_pptx
from excel.xlsx import translate_xlsx
from excel.excel_translate import translate_csv
from word.word_helper import translate_docx
import dotenv
import os
dotenv.load_dotenv(".env")
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
# Cấu hình trang
st.set_page_config(page_title="Translate Your File", page_icon="🌍", layout="centered")
# CSS custom
st.markdown("""
<style>
body {
background-color: #ffffff;
color: #333333;
}
.main {
background-color: #ffffff;
color: #333333;
}
h1, h2, h3 {
color: #007acc;
text-align: center;
}
.stButton>button {
background-color: #007acc;
color: white;
border-radius: 10px;
padding: 0.75em 2em;
font-size: 1.1em;
border: none;
transition: 0.3s;
}
.stButton>button:hover {
background-color: #005f99;
color: white;
}
.stFileUploader {
border: 2px dashed #007acc;
padding: 20px;
border-radius: 10px;
text-align: center;
background-color: #f9f9f9;
}
div[data-baseweb="select"] > div {
background-color: white !important;
color: black !important;
border-radius: 8px;
}
/* Thu hẹp khoảng cách giữa label và selectbox */
.stSelectbox label {
margin-bottom: 0.2rem;
font-weight: bold;
color: #333333;
}
footer {visibility: hidden;}
</style>
""", unsafe_allow_html=True)
# Upload file section
with st.container():
st.markdown("### 📂 Chọn file để dịch")
uploaded_file = st.file_uploader("Kéo thả hoặc chọn file", type=['pptx', 'xlsx', 'csv', 'docx'])
with st.container():
col1, col2 = st.columns(2)
with col1:
st.markdown('<p style="font-size:16px; font-weight:bold; margin-bottom:4px;">🌐 Ngôn ngữ của tài liệu</p>', unsafe_allow_html=True)
source_lang = st.selectbox(" ", ["english", "vietnamese"], key="source_lang")
with col2:
st.markdown('<p style="font-size:16px; font-weight:bold; margin-bottom:4px;">🌐 Ngôn ngữ muốn dịch sang</p>', unsafe_allow_html=True)
target_lang = st.selectbox(" ", ["english", "vietnamese"], key="target_lang")
def process_file(file, file_type):
progress_bar = st.progress(0)
with st.spinner("🔄 Đang lưu file lên hệ thống..."):
file_id, file_name = save_file_to_mongodb(uploaded_file=file, db_name=file_type.lower(), collection_name="root_file")
progress_bar.progress(20)
st.write(f"📂 **File ID:** `{file_id}`")
with st.spinner("🔍 Đang xử lý và dịch tài liệu..."):
if file_type == "PPTX":
final_id = translate_pptx(file_id, file_name, source_lang=source_lang, target_lang=target_lang, slides_per_batch=5)
progress_bar.progress(60)
elif file_type == "Excel":
final_id = translate_xlsx(file_id=file_id, file_name=file_name, source_lang=source_lang, target_lang=target_lang)
elif file_type == "CSV":
final_id = translate_csv(file_id=file_id, source_lang=source_lang, target_lang=target_lang)
elif file_type == "Word":
final_id = translate_docx(file_id=file_id, file_name=file_name, source_lang=source_lang, target_lang=target_lang)
else:
st.error("❌ Loại file không hỗ trợ!")
return
progress_bar.progress(80)
with st.spinner("📦 Đang tải file đã dịch..."):
file_io, file_name = fetch_file_from_mongodb(file_type.lower(), "final_file", final_id)
progress_bar.progress(100)
if file_io:
st.success("🎉 File đã được dịch thành công!")
st.download_button("⬇️ Tải file về", data=file_io.getvalue(), file_name=file_name)
else:
st.error("❌ Không thể tải xuống file. Vui lòng thử lại!")
if uploaded_file and st.button("🚀 Upload và dịch ngay!"):
with st.spinner("🔎 Đang phát hiện loại file..."):
file_type = detect_file_type(uploaded_file)
st.write(f"🔍 **Loại file phát hiện:** `{file_type}`")
process_file(uploaded_file, file_type)