kambris commited on
Commit
8814bfe
·
verified ·
1 Parent(s): fce0e3a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -5
app.py CHANGED
@@ -34,21 +34,32 @@ def analyze_sentiment_files(file1, file2, file3, file4, file5, column_name):
34
  all_data = []
35
 
36
  for i, file in enumerate(files, 1):
37
- with open(file.name, 'r', encoding='utf-8') as f:
38
- lines = f.readlines()
 
 
 
 
39
 
40
  texts = [line.strip() for line in lines if line.strip()]
41
 
 
 
 
42
  # Create dataframe for this file
43
  file_df = pd.DataFrame({
44
  'text': texts,
45
  'line_number': range(1, len(texts) + 1),
46
  'file_name': f'File {i}',
47
- 'source_file': file.name.split('/')[-1]
48
  })
49
 
50
  all_data.append(file_df)
51
 
 
 
 
 
52
  # Combine all files
53
  df = pd.concat(all_data, ignore_index=True)
54
  column_name = 'text'
@@ -76,10 +87,18 @@ def analyze_sentiment_files(file1, file2, file3, file4, file5, column_name):
76
  # Get all column names except sentiment columns for filter options
77
  filter_columns = [col for col in df.columns if col not in ['sentiment_label', 'sentiment_score']]
78
 
79
- summary = create_summary(df, "All Data")
 
 
 
 
 
 
 
 
80
 
81
  return (summary, df, None, None, None,
82
- gr.update(choices=filter_columns, value=None),
83
  gr.update(choices=[], value=None),
84
  gr.update(choices=[], value=None))
85
 
 
34
  all_data = []
35
 
36
  for i, file in enumerate(files, 1):
37
+ try:
38
+ with open(file.name, 'r', encoding='utf-8') as f:
39
+ lines = f.readlines()
40
+ except:
41
+ with open(file.name, 'r', encoding='latin-1') as f:
42
+ lines = f.readlines()
43
 
44
  texts = [line.strip() for line in lines if line.strip()]
45
 
46
+ if not texts:
47
+ continue
48
+
49
  # Create dataframe for this file
50
  file_df = pd.DataFrame({
51
  'text': texts,
52
  'line_number': range(1, len(texts) + 1),
53
  'file_name': f'File {i}',
54
+ 'source_file': file.name.split('/')[-1].split('\\')[-1]
55
  })
56
 
57
  all_data.append(file_df)
58
 
59
+ if not all_data:
60
+ return ("Error: No valid text found in uploaded files",
61
+ None, None, None, None, gr.update(choices=[]), gr.update(choices=[]), gr.update(choices=[]))
62
+
63
  # Combine all files
64
  df = pd.concat(all_data, ignore_index=True)
65
  column_name = 'text'
 
87
  # Get all column names except sentiment columns for filter options
88
  filter_columns = [col for col in df.columns if col not in ['sentiment_label', 'sentiment_score']]
89
 
90
+ # Create initial summary with file breakdown if multiple TXT files
91
+ if 'file_name' in df.columns:
92
+ file_summary = "\n\n📁 FILES UPLOADED:\n"
93
+ for fname in df['file_name'].unique():
94
+ count = len(df[df['file_name'] == fname])
95
+ file_summary += f" - {fname}: {count} lines\n"
96
+ summary = create_summary(df, "All Data") + file_summary
97
+ else:
98
+ summary = create_summary(df, "All Data")
99
 
100
  return (summary, df, None, None, None,
101
+ gr.update(choices=filter_columns, value='file_name' if 'file_name' in filter_columns else None),
102
  gr.update(choices=[], value=None),
103
  gr.update(choices=[], value=None))
104