Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 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 |
-
|
| 246 |
-
|
| 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 là 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.")
|