mintlee commited on
Commit
fad6c52
·
1 Parent(s): 8414cb0

minor change

Browse files
db/__pycache__/mongodb.cpython-310.pyc CHANGED
Binary files a/db/__pycache__/mongodb.cpython-310.pyc and b/db/__pycache__/mongodb.cpython-310.pyc differ
 
db/mongodb.py CHANGED
@@ -13,44 +13,31 @@ def connect_mongodb(db_name, collection_name):
13
 
14
 
15
 
16
- def save_file_to_mongodb(uploaded_file, db_name="ppt", collection_name="root_file", file_name=None, file_tail=".pptx"):
17
  """
18
- Lưu file PowerPoint (pptx) vào MongoDB bằng GridFS
19
- nhưng không lưu nếu tên file đã tồn tại.
20
 
21
  :param uploaded_file: đối tượng UploadedFile từ Streamlit
22
  :param db_name: Tên database trong MongoDB
23
  :param collection_name: Tên collection GridFS
24
- :param file_name: Tên file muốn lưu (không cần .pptx). Nếu để None, lấy tên gốc.
25
- :return: file_id nếu lưu thành công, None nếu file đã tồn tại
26
  """
27
  client = MongoClient("mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority&appName=Cluster0")
28
  db = client[db_name]
29
  fs = gridfs.GridFS(db, collection=collection_name)
30
 
31
  # Xác định tên file
32
- if not file_name:
33
- # Lấy tên file từ uploaded_file (VD: "slide.pptx")
34
- file_name = uploaded_file.name
35
- else:
36
- # Nếu người dùng chỉ truyền tên, thêm .pptx nếu chưa có
37
- if not file_name.endswith(file_tail):
38
- file_name = file_name + file_tail
39
-
40
- # Kiểm tra file đã tồn tại trong MongoDB chưa
41
- existing_file = fs.find_one({"filename": file_name})
42
- if existing_file:
43
- print(f"⚠️ File '{file_name}' đã tồn tại trong MongoDB. Không lưu lại. Xin vui lòng đổi tên.")
44
- client.close()
45
- return None
46
 
47
  # Đảm bảo con trỏ file đang ở đầu
48
  uploaded_file.seek(0)
49
  file_bytes = uploaded_file.read()
50
 
51
- # Lưu nội dung file (bytes) vào MongoDB
52
  file_id = fs.put(file_bytes, filename=file_name)
53
  print(f"✅ File '{file_name}' đã được lưu vào '{collection_name}' với ID: {file_id}")
 
54
  client.close()
55
  return file_id
56
 
 
13
 
14
 
15
 
16
+ def save_file_to_mongodb(uploaded_file, db_name="ppt", collection_name="root_file", file_tail=".pptx"):
17
  """
18
+ Lưu file vào MongoDB bằng GridFS mà không kiểm tra trùng lặp.
 
19
 
20
  :param uploaded_file: đối tượng UploadedFile từ Streamlit
21
  :param db_name: Tên database trong MongoDB
22
  :param collection_name: Tên collection GridFS
23
+ :param file_tail: Phần mở rộng mặc định của file nếu không
24
+ :return: file_id nếu lưu thành công
25
  """
26
  client = MongoClient("mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority&appName=Cluster0")
27
  db = client[db_name]
28
  fs = gridfs.GridFS(db, collection=collection_name)
29
 
30
  # Xác định tên file
31
+ file_name = uploaded_file.name
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  # Đảm bảo con trỏ file đang ở đầu
34
  uploaded_file.seek(0)
35
  file_bytes = uploaded_file.read()
36
 
37
+ # Lưu file vào MongoDB (không kiểm tra trùng lặp)
38
  file_id = fs.put(file_bytes, filename=file_name)
39
  print(f"✅ File '{file_name}' đã được lưu vào '{collection_name}' với ID: {file_id}")
40
+
41
  client.close()
42
  return file_id
43
 
excel/excel_translate.py CHANGED
@@ -48,7 +48,7 @@ def translate_xlsx(file_id: str, sheet_name: str = None, from_lang: str = 'en',
48
  cell_map[key] = cell
49
 
50
  # Gọi hàm dịch theo dạng bulk
51
- translated_dict = translate_text_dict(text_dict, source_lang=from_lang, target_lang=target_lang, gemini_api=gemini_api)
52
 
53
  # Cập nhật lại các cell với nội dung đã dịch
54
  for key, cell in cell_map.items():
 
48
  cell_map[key] = cell
49
 
50
  # Gọi hàm dịch theo dạng bulk
51
+ translated_dict = translate_text_dict(text_dict, target_lang=target_lang, gemini_api=gemini_api)
52
 
53
  # Cập nhật lại các cell với nội dung đã dịch
54
  for key, cell in cell_map.items():
pages/upload.py CHANGED
@@ -24,7 +24,6 @@ st.title("Upload PPTX to MongoDB")
24
 
25
  # st.set_option("server.fileUploader.allowMediaFiles", True)
26
  uploaded_file = st.file_uploader("Chọn file")
27
- file_name_input = st.text_input("Tên file để lưu (không cần .pptx)", value="")
28
  target_lang = st.selectbox("Chọn ngôn ngữ dịch", ["english", "vietnamese"])
29
 
30
  final_pptx_id = None # Biến lưu ID file sau khi xử lý
@@ -35,7 +34,7 @@ if uploaded_file is not None:
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, file_name=file_name_input)
39
  st.write(f"File ID: {file_id}")
40
 
41
  xml_file_id = ppt_to_xml_mongodb(file_id)
 
24
 
25
  # st.set_option("server.fileUploader.allowMediaFiles", True)
26
  uploaded_file = st.file_uploader("Chọn file")
 
27
  target_lang = st.selectbox("Chọn ngôn ngữ dịch", ["english", "vietnamese"])
28
 
29
  final_pptx_id = None # Biến lưu ID file sau khi xử lý
 
34
  st.write(f"Detected file type: {file_type}")
35
  if file_type == "PPTX":
36
 
37
+ file_id = save_file_to_mongodb(uploaded_file=uploaded_file)
38
  st.write(f"File ID: {file_id}")
39
 
40
  xml_file_id = ppt_to_xml_mongodb(file_id)
powerpoint/__pycache__/xml_handling.cpython-310.pyc CHANGED
Binary files a/powerpoint/__pycache__/xml_handling.cpython-310.pyc and b/powerpoint/__pycache__/xml_handling.cpython-310.pyc differ
 
powerpoint/xml_handling.py CHANGED
@@ -83,7 +83,14 @@ def ppt_to_xml_mongodb(ppt_file_id: str, db_name="ppt"):
83
  :return: ID của file XML trong MongoDB (original_xml)
84
  """
85
  # Kết nối MongoDB
86
- client = MongoClient("mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority&appName=Cluster0")
 
 
 
 
 
 
 
87
  db = client[db_name]
88
 
89
  fs_ppt = gridfs.GridFS(db, collection="root_file") # PPT gốc
@@ -120,6 +127,7 @@ def ppt_to_xml_mongodb(ppt_file_id: str, db_name="ppt"):
120
  xml_file_id = fs_xml.put(xml_output, filename=f"{ppt_file.filename}.xml")
121
 
122
  print(f"✅ XML đã được lưu vào MongoDB (original_xml) với file_id: {xml_file_id}")
 
123
 
124
  return xml_file_id
125
 
 
83
  :return: ID của file XML trong MongoDB (original_xml)
84
  """
85
  # Kết nối MongoDB
86
+ client = MongoClient(
87
+ "mongodb+srv://admin:[email protected]/?retryWrites=true&w=majority&appName=Cluster0",
88
+ connectTimeoutMS=60000, # 60 giây thay vì 20 giây
89
+ serverSelectionTimeoutMS=60000, # Chờ phản hồi lâu hơn
90
+ socketTimeoutMS=60000, # Tăng thời gian chờ socket
91
+ tls=True,
92
+ tlsAllowInvalidCertificates=True # Giữ kết nối lâu hơn
93
+ )
94
  db = client[db_name]
95
 
96
  fs_ppt = gridfs.GridFS(db, collection="root_file") # PPT gốc
 
127
  xml_file_id = fs_xml.put(xml_output, filename=f"{ppt_file.filename}.xml")
128
 
129
  print(f"✅ XML đã được lưu vào MongoDB (original_xml) với file_id: {xml_file_id}")
130
+ client.close()
131
 
132
  return xml_file_id
133
 
translate/__pycache__/translator.cpython-310.pyc CHANGED
Binary files a/translate/__pycache__/translator.cpython-310.pyc and b/translate/__pycache__/translator.cpython-310.pyc differ