File size: 4,643 Bytes
0e9ff78
 
be5d384
73196e5
4d84219
804add3
ce94f1c
0e9ff78
 
be5d384
0e9ff78
 
a17bcfd
 
ff93898
bdcb5e5
a17bcfd
bdcb5e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73196e5
ff93898
bdcb5e5
 
 
 
ff93898
bdcb5e5
 
 
 
 
d300944
bdcb5e5
 
 
5204efb
a17bcfd
ff93898
a17bcfd
 
ff93898
 
bdcb5e5
 
 
ff93898
bdcb5e5
ff93898
bdcb5e5
ff93898
bdcb5e5
ff93898
bdcb5e5
 
 
a17bcfd
ff93898
a17bcfd
ff93898
bdcb5e5
ff93898
 
14655c7
 
a17bcfd
ff93898
 
a17bcfd
ff93898
a17bcfd
be5d384
 
 
 
bdcb5e5
 
 
a17bcfd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import streamlit as st
import google.generativeai as genai
from db.mongodb import 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
from datetime import datetime, timedelta

dotenv.load_dotenv(".env")
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))

# Cấu hình giao diện
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;
        }
        .stSelectbox label {
            margin-bottom: 0.2rem;
            font-weight: bold;
            color: #333333;
        }
        footer {visibility: hidden;}
    </style>
""", unsafe_allow_html=True)

# Upload file
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'])

# Lựa chọn ngôn ngữ
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(" ", ["Để máy tự xác định", "chinese", "english", "vietnamese", "japanese"], 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("  ", ["vietnamese", "chinese", "english", "japanese"], key="target_lang")

# Xử lý file trực tiếp
def process_file(file, file_type):
    progress_bar = st.progress(0)
    file_name = file.name
    progress_bar.progress(10)

    with st.spinner("🔍 Đang xử lý và dịch tài liệu..."):
        if file_type == "PPTX":
            output_io, output_name = translate_pptx(file, file_name, source_lang=source_lang, target_lang=target_lang)
        elif file_type == "Excel":
            output_io, output_name = translate_xlsx(file, file_name, source_lang=source_lang, target_lang=target_lang)
        elif file_type == "CSV":
            output_io, output_name = translate_csv(file, file_name, source_lang=source_lang, target_lang=target_lang)
        elif file_type == "Word":
            output_io, output_name = translate_docx(file, file_name, source_lang=source_lang, target_lang=target_lang)
        else:
            st.error("❌ Loại file không hỗ trợ!")
            return

    progress_bar.progress(100)

    if output_io:
        st.success("🎉 File đã được dịch thành công!")
        print(f"✅ File đã dịch: {output_name}")
        st.download_button("⬇️ Tải file về", data=output_io.getvalue(), file_name=output_name)
        end_time = datetime.now() + timedelta(hours=5)
        print(f"🕒 Thời gian upload: {end_time.strftime('%Y-%m-%d %H:%M:%S')}")
    else:
        st.error("❌ Xảy ra lỗi khi xử lý file.")


# Nút xử lý
if uploaded_file and st.button("🚀 Upload và dịch ngay!"):
    upload_time = datetime.now() + timedelta(hours=5)
    print("=" * 100)
    print(f"🕒 Thời gian upload: {upload_time.strftime('%Y-%m-%d %H:%M:%S')}")
    st.write(f"🕒 **Thời gian upload (UTC+5):** `{upload_time.strftime('%Y-%m-%d %H:%M:%S')}`")
    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)