Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -435,7 +435,7 @@ class PlotFactory:
|
|
| 435 |
|
| 436 |
return fig
|
| 437 |
|
| 438 |
-
|
| 439 |
class DataHandler:
|
| 440 |
"""Handles all data operations"""
|
| 441 |
|
|
@@ -468,39 +468,52 @@ class DataHandler:
|
|
| 468 |
temp_file.close()
|
| 469 |
return temp_file.name, f"Exported {len(data)} entries"
|
| 470 |
|
| 471 |
-
|
| 472 |
@staticmethod
|
| 473 |
@handle_errors(default_return="")
|
| 474 |
def process_file(file) -> str:
|
| 475 |
"""Process uploaded file"""
|
| 476 |
if not file:
|
| 477 |
return ""
|
| 478 |
-
|
| 479 |
-
content = file.read().decode('utf-8')
|
| 480 |
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
for
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
# Main Application
|
| 506 |
class SentimentApp:
|
|
|
|
| 435 |
|
| 436 |
return fig
|
| 437 |
|
| 438 |
+
|
| 439 |
class DataHandler:
|
| 440 |
"""Handles all data operations"""
|
| 441 |
|
|
|
|
| 468 |
temp_file.close()
|
| 469 |
return temp_file.name, f"Exported {len(data)} entries"
|
| 470 |
|
|
|
|
| 471 |
@staticmethod
|
| 472 |
@handle_errors(default_return="")
|
| 473 |
def process_file(file) -> str:
|
| 474 |
"""Process uploaded file"""
|
| 475 |
if not file:
|
| 476 |
return ""
|
|
|
|
|
|
|
| 477 |
|
| 478 |
+
try:
|
| 479 |
+
# For CSV files
|
| 480 |
+
if file.name.endswith('.csv'):
|
| 481 |
+
# Read the file content
|
| 482 |
+
content = file.read()
|
| 483 |
+
|
| 484 |
+
# Try different encodings
|
| 485 |
+
for encoding in ['utf-8', 'latin-1', 'cp1252']:
|
| 486 |
+
try:
|
| 487 |
+
text_content = content.decode(encoding)
|
| 488 |
+
# Use pandas for robust CSV handling
|
| 489 |
+
df = pd.read_csv(io.StringIO(text_content), encoding=encoding)
|
| 490 |
+
# Assume the first column contains the reviews
|
| 491 |
+
# Get non-empty reviews from the first column
|
| 492 |
+
reviews = df.iloc[:, 0].dropna().astype(str).tolist()
|
| 493 |
+
return '\n'.join(review.strip() for review in reviews if review.strip())
|
| 494 |
+
except Exception as e:
|
| 495 |
+
continue
|
| 496 |
+
|
| 497 |
+
# If all encodings fail, try basic string splitting
|
| 498 |
+
try:
|
| 499 |
+
text_content = content.decode('utf-8', errors='ignore')
|
| 500 |
+
lines = text_content.strip().split('\n')[1:] # Skip header
|
| 501 |
+
return '\n'.join(line.strip() for line in lines if line.strip())
|
| 502 |
+
except:
|
| 503 |
+
return ""
|
| 504 |
+
|
| 505 |
+
# For text files
|
| 506 |
+
else:
|
| 507 |
+
content = file.read()
|
| 508 |
+
try:
|
| 509 |
+
text_content = content.decode('utf-8')
|
| 510 |
+
except:
|
| 511 |
+
text_content = content.decode('utf-8', errors='ignore')
|
| 512 |
+
return text_content.strip()
|
| 513 |
+
|
| 514 |
+
except Exception as e:
|
| 515 |
+
logger.error(f"File processing error: {e}")
|
| 516 |
+
return ""
|
| 517 |
|
| 518 |
# Main Application
|
| 519 |
class SentimentApp:
|