entropy25 commited on
Commit
10754da
·
verified ·
1 Parent(s): 201eb05

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -27
app.py CHANGED
@@ -435,7 +435,7 @@ class PlotFactory:
435
 
436
  return fig
437
 
438
- # Unified Data Handler
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
- if file.name.endswith('.csv'):
482
- import io
483
- csv_file = io.StringIO(content)
484
- reader = csv.reader(csv_file)
485
- try:
486
- next(reader)
487
- texts = []
488
- for row in reader:
489
- if row and row[0].strip():
490
- text = row[0].strip().strip('"')
491
- if text:
492
- texts.append(text)
493
- return '\n'.join(texts)
494
- except Exception as e:
495
- lines = content.strip().split('\n')[1:]
496
- texts = []
497
- for line in lines:
498
- if line.strip():
499
- text = line.strip().strip('"')
500
- if text:
501
- texts.append(text)
502
- return '\n'.join(texts)
503
- return content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: