Spaces:
Running
Running
File size: 6,065 Bytes
0e9ff78 7c2551b 0e9ff78 |
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 126 127 128 129 130 131 132 133 134 135 |
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.xml_handling import (
extract_text_from_xml, update_xml_with_translated_text_mongodb, ppt_to_xml_mongodb
)
from translate.translator import translate_text_dict
from powerpoint.pptx_object import create_translated_ppt
from excel.excel_translate import translate_xlsx, translate_csv
from word.word_translate import translate_docx
import dotenv
import os
dotenv.load_dotenv(".env")
# Cấu hình API key
api_key = os.getenv("GEMINI_API_KEY")
genai.configure(api_key=api_key)
model = genai.GenerativeModel("gemini-1.5-flash")
# Giao diện Streamlit
st.title("Upload PPTX to MongoDB")
# st.set_option("server.fileUploader.allowMediaFiles", True)
uploaded_file = st.file_uploader("Chọn file")
file_name_input = st.text_input("Tên file để lưu (không cần .pptx)", value="")
final_pptx_id = None # Biến lưu ID file sau khi xử lý
if uploaded_file is not None:
if st.button("Upload"):
file_type = detect_file_type(uploaded_file)
st.write(f"Detected file type: {file_type}")
if file_type == "PPTX":
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, file_name=file_name_input)
st.write(f"File ID: {file_id}")
xml_file_id = ppt_to_xml_mongodb(file_id)
text_dict = extract_text_from_xml(file_id=xml_file_id)
translated_dict = translate_text_dict(text_dict, source_lang="VietNamese", target_lang="English", gemini_api=api_key)
final_xml_id = update_xml_with_translated_text_mongodb(xml_file_id, translated_dict)
st.write(f"Final XML ID: {final_xml_id}")
# Lưu ID file PPTX cuối cùng
final_pptx_id = create_translated_ppt(
db_name="ppt", original_ppt_id=file_id,
translated_xml_id=final_xml_id, output_collection="final_pptx"
)
st.write(f"Final PPTX ID: {final_pptx_id}")
# Hiển thị ảnh slide trước khi tải xuống
if final_pptx_id:
st.write("✅ File đã sẵn sàng để tải xuống!")
pptx_io, pptx_filename = fetch_file_from_mongodb("ppt", "final_pptx", final_pptx_id)
if pptx_io:
# Nút tải file sau khi xem trước
st.download_button(
label="Click to Download",
data=pptx_io.getvalue(), # Chuyển thành bytes để tải về
file_name=pptx_filename,
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
)
else:
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
elif file_type == "Excel":
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="excel", collection_name="root_file", file_name=file_name_input, file_tail=".xlsx")
st.write(f"File ID: {file_id}")
final_id = translate_xlsx(file_id=file_id, from_lang="en", target_lang="vi", gemini_api=api_key)
st.write(f"Final Excel ID: {final_id}")
if final_id:
st.write("✅ File đã sẵn sàng để tải xuống!")
excel_io, excel_filename = fetch_file_from_mongodb("excel", "final_file", final_id)
if excel_io:
st.download_button(
label="Click to Download",
data=excel_io.getvalue(),
file_name=excel_filename,
mime="application/vnd.ms-excel"
)
else:
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
elif file_type == "CSV":
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="csv", collection_name="root_file", file_name=file_name_input, file_tail=".csv")
st.write(f"File ID: {file_id}")
final_id = translate_csv(file_id=file_id, source_lang="en", target_lang="vi", gemini_api=api_key)
st.write(f"Final CSV ID: {final_id}")
if final_id:
st.write("✅ File đã sẵn sàng để tải xuống!")
csv_io, csv_filename = fetch_file_from_mongodb("csv", "final_file", final_id)
if csv_io:
st.download_button(
label="Click to Download",
data=csv_io.getvalue(),
file_name=csv_filename,
mime="text/csv"
)
else:
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
elif file_type == "Word":
file_id = save_file_to_mongodb(uploaded_file=uploaded_file, db_name="word", collection_name="root_file", file_name=file_name_input, file_tail=".docx")
st.write(f"File ID: {file_id}")
final_id = translate_docx(file_id=file_id, source_lang="en", target_lang="vi")
st.write(f"Final CSV ID: {final_id}")
if final_id:
st.write("✅ File đã sẵn sàng để tải xuống!")
docx_io, docx_filename = fetch_file_from_mongodb("word", "final_file", final_id)
if docx_io:
st.download_button(
label="Click to Download",
data=docx_io.getvalue(),
file_name=docx_filename,
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
else:
st.error("❌ Không thể tải xuống file. Kiểm tra lại ID hoặc thử lại sau.")
|