Di12 commited on
Commit
4c1c582
·
verified ·
1 Parent(s): 5bb637b

Update app.py

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