Di12 commited on
Commit
1feb9b0
·
verified ·
1 Parent(s): 1dc6ef0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -37
app.py CHANGED
@@ -211,45 +211,41 @@ def predict_sentiment(model, sentence, vocab, label_mapping=None):
211
 
212
  def process_input(text_input, file):
213
  content = ""
 
 
214
  if text_input:
215
  content += text_input + "\n"
216
- if file is not None:
217
- content += file.read().decode('utf-8') + "\n"
218
- # Tách câu: theo dấu ., ?, ! hoặc xuống dòng
219
- parts = re.split(r'[.?!]\s*|\n+', content)
220
- comments = [p.strip() for p in parts if p and p.strip()]
221
-
222
- # comments = []
223
-
224
- # if text_input:
225
- # comments += [line.strip() for line in text_input.splitlines() if line.strip()]
226
-
227
- # elif file:
228
- # # Nếu file bytes, chuyển đổi thành đối tượng BytesIO
229
- # if isinstance(file, bytes):
230
- # file_content = io.BytesIO(file)
231
- # try:
232
- # # Thử đọc như tệp văn bản
233
- # content = file_content.read().decode('utf-8')
234
- # comments += [line.strip() for line in content.splitlines() if line.strip()]
235
- # except UnicodeDecodeError:
236
- # # Nếu không thành công, thử đọc như CSV
237
- # file_content.seek(0)
238
- # try:
239
- # df = pd.read_csv(file_content, header=None, names=["Comment"], encoding='utf-8')
240
- # comments += df["Comment"].dropna().astype(str).tolist()
241
- # except Exception as e:
242
- # raise gr.Error(f"Lỗi khi đọc tệp CSV: {str(e)}")
243
- # # Nếu file là đường dẫn chuỗi
244
- # elif isinstance(file, str):
245
- # try:
246
- # with open(file, 'r', encoding='utf-8') as f:
247
- # content = f.read()
248
- # comments += [line.strip() for line in content.splitlines() if line.strip()]
249
- # except Exception as e:
250
- # raise gr.Error(f"Lỗi khi đọc tệp: {str(e)}")
251
- # else:
252
- # raise gr.Error("Định dạng tệp không được hỗ trợ.")
253
 
254
  if len(comments) == 0:
255
  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.")
 
211
 
212
  def process_input(text_input, file):
213
  content = ""
214
+ comments = []
215
+
216
  if text_input:
217
  content += text_input + "\n"
218
+
219
+ elif file:
220
+ # Nếu file bytes, chuyển đổi thành đối tượng BytesIO
221
+ if isinstance(file, bytes):
222
+ file_content = io.BytesIO(file)
223
+ try:
224
+ # Thử đọc như tệp văn bản
225
+ content = file_content.read().decode('utf-8') + "\n"
226
+ parts = re.split(r'[.?!]\s*|\n+', content)
227
+ comments = [p.strip() for p in parts if p and p.strip()]
228
+
229
+ except UnicodeDecodeError:
230
+ # Nếu không thành công, thử đọc như CSV
231
+ file_content.seek(0)
232
+ try:
233
+ df = pd.read_csv(file_content, header=None, names=["Comment"], encoding='utf-8')
234
+ comments += df["Comment"].dropna().astype(str).tolist()
235
+ except Exception as e:
236
+ raise gr.Error(f"Lỗi khi đọc tệp CSV: {str(e)}")
237
+ # Nếu file là đường dẫn chuỗi
238
+ elif isinstance(file, str):
239
+ try:
240
+ with open(file, 'r', encoding='utf-8') as f:
241
+ content = f.read()
242
+ parts = re.split(r'[.?!]\s*|\n+', content)
243
+ comments = [p.strip() for p in parts if p and p.strip()]
244
+
245
+ except Exception as e:
246
+ raise gr.Error(f"Lỗi khi đọc tệp: {str(e)}")
247
+ else:
248
+ raise gr.Error("Định dạng tệp không được hỗ trợ.")
 
 
 
 
 
 
249
 
250
  if len(comments) == 0:
251
  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.")