Spaces:
Running
Running
| 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") | |
| uploaded_file = st.file_uploader("Chọn file PPTX", type=["pptx, excel, csv, docx"]) | |
| 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.") | |