Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 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.")
|