Di12 commited on
Commit
dca0786
·
verified ·
1 Parent(s): 71672d1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -24
app.py CHANGED
@@ -219,35 +219,62 @@ def process_input(text_input, file):
219
  comments = [p.strip() for p in parts if p and p.strip()]
220
 
221
  elif file:
222
- # Nếu file là bytes, chuyển đổi thành đối tượng BytesIO
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
223
  if isinstance(file, bytes):
224
- file_content = io.BytesIO(file)
225
  try:
226
- # Thử đọc như tệp văn bản
227
- content = file_content.read().decode('utf-8') + "\n"
 
 
228
  parts = re.split(r'[.?!]\s*|\n+', content)
229
- comments = [p.strip() for p in parts if p and p.strip()]
230
-
231
- except UnicodeDecodeError:
232
- # Nếu không thành công, thử đọc như CSV
233
- file_content.seek(0)
234
- try:
235
- df = pd.read_csv(file_content, header=None, names=["Comment"], encoding='utf-8')
236
- comments += df["Comment"].dropna().astype(str).tolist()
237
- except Exception as e:
238
- raise gr.Error(f"Lỗi khi đọc tệp CSV: {str(e)}")
239
- # Nếu file là đường dẫn chuỗi
240
  elif isinstance(file, str):
241
- try:
242
- with open(file, 'r', encoding='utf-8') as f:
243
- content = f.read()
244
- parts = re.split(r'[.?!]\s*|\n+', content)
245
- comments = [p.strip() for p in parts if p and p.strip()]
246
-
247
- except Exception as e:
248
- raise gr.Error(f"Lỗi khi đọc tệp: {str(e)}")
249
  else:
250
- raise gr.Error("Định dạng tệp không được hỗ trợ.")
 
 
 
 
 
 
 
251
 
252
  if len(comments) == 0:
253
  raise gr.Error("Vui lòng nhập ít nhất một bình luận hoặc tải lên tệp chứa bình luận.")
 
219
  comments = [p.strip() for p in parts if p and p.strip()]
220
 
221
  elif file:
222
+ # # Nếu file là bytes, chuyển đổi thành đối tượng BytesIO
223
+ # if isinstance(file, bytes):
224
+ # file_content = io.BytesIO(file)
225
+ # try:
226
+ # # Thử đọc như tệp văn bản
227
+ # content = file_content.read().decode('utf-8') + "\n"
228
+ # parts = re.split(r'[.?!]\s*|\n+', content)
229
+ # comments = [p.strip() for p in parts if p and p.strip()]
230
+
231
+ # except UnicodeDecodeError:
232
+ # # Nếu không thành công, thử đọc như CSV
233
+ # file_content.seek(0)
234
+ # try:
235
+ # df = pd.read_csv(file_content, header=None, names=["Comment"], encoding='utf-8')
236
+ # comments += df["Comment"].dropna().astype(str).tolist()
237
+ # except Exception as e:
238
+ # raise gr.Error(f"Lỗi khi đọc tệp CSV: {str(e)}")
239
+ # # Nếu file là đường dẫn chuỗi
240
+ # elif isinstance(file, str):
241
+ # try:
242
+ # with open(file, 'r', encoding='utf-8') as f:
243
+ # content = f.read()
244
+ # parts = re.split(r'[.?!]\s*|\n+', content)
245
+ # comments = [p.strip() for p in parts if p and p.strip()]
246
+
247
+ # except Exception as e:
248
+ # raise gr.Error(f"Lỗi khi đọc tệp: {str(e)}")
249
+
250
+ # Đọc file CSV hoặc TXT
251
+ df = None
252
  if isinstance(file, bytes):
253
+ # thử đọc CSV
254
  try:
255
+ df = pd.read_csv(io.BytesIO(file), header=None, names=["Comment"], encoding="utf-8")
256
+ except Exception:
257
+ # fallback: đọc như text, rồi tách câu
258
+ content = io.BytesIO(file).read().decode('utf-8', errors='ignore')
259
  parts = re.split(r'[.?!]\s*|\n+', content)
260
+ comments = [p.strip() for p in parts if p.strip()]
 
 
 
 
 
 
 
 
 
 
261
  elif isinstance(file, str):
262
+ # file path
263
+ if file.lower().endswith('.csv'):
264
+ df = pd.read_csv(file, header=None, names=["Comment"], encoding='utf-8')
265
+ else:
266
+ content = open(file, 'r', encoding='utf-8').read()
267
+ parts = re.split(r'[.?!]\s*|\n+', content)
268
+ comments = [p.strip() for p in parts if p.strip()]
 
269
  else:
270
+ raise gr.Error("Định dạng tệp không được hỗ trợ.")
271
+
272
+ # Nếu đọc được CSV, chuyển mỗi dòng thành comment nguyên bản
273
+ if df is not None:
274
+ df = df.dropna(subset=["Comment"])
275
+ if df["Comment"].empty:
276
+ raise gr.Error("File CSV không chứa comment hợp lệ.")
277
+ comments = df["Comment"].astype(str).tolist()
278
 
279
  if len(comments) == 0:
280
  raise gr.Error("Vui lòng nhập ít nhất một bình luận hoặc tải lên tệp chứa bình luận.")