xtlyxt commited on
Commit
449534b
·
verified ·
1 Parent(s): d231871

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -15
app.py CHANGED
@@ -4,7 +4,7 @@ from transformers import pipeline
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
6
  import seaborn as sns
7
- from pandas.plotting import parallel_coordinates
8
 
9
  # Initialize session state for results, image names, and image sizes if not already present
10
  if 'results' not in st.session_state:
@@ -64,7 +64,6 @@ if st.button("Generate HeatMap & DataFrame"):
64
  for i, result_set in enumerate(results):
65
  # Initialize a dictionary for the current set with zeros
66
  current_data = {
67
-
68
  'Happy': 0,
69
  'Surprise': 0,
70
  'Neutral': 0,
@@ -72,12 +71,7 @@ if st.button("Generate HeatMap & DataFrame"):
72
  'Disgust': 0,
73
  'Angry': 0,
74
  'Fear': 0,
75
-
76
-
77
-
78
- # Add other emotions if necessary
79
  'Image Name': image_names[i],
80
- #'Image Size (KB)': image_sizes[i]
81
  'Image Size (KB)': f"{image_sizes[i]:.1f}" # Format the size to one decimal place
82
  }
83
 
@@ -103,24 +97,48 @@ if st.button("Generate HeatMap & DataFrame"):
103
  plt.xlabel('Emotion Categories')
104
  plt.ylabel('Data Points')
105
  st.pyplot(plt)
106
-
107
 
108
-
109
- # Optional: Save the DataFrame to a CSV file
110
  df_emotions.to_csv('emotion_scores.csv', index=False)
111
  st.success('DataFrame generated and saved as emotion_scores.csv')
112
-
113
  with open('emotion_scores.csv', 'r') as f:
114
  csv_file = f.read()
115
-
116
  st.download_button(
117
  label='Download Emotion Scores as CSV',
118
  data=csv_file,
119
  file_name='emotion_scores.csv',
120
  mime='text/csv',
121
  )
122
-
123
- st.success('DataFrame generated and available for download.')
124
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  else:
126
  st.error("No results to generate DataFrame. Please predict emotions first.")
 
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
6
  import seaborn as sns
7
+ import xlsxwriter # Required for handling images in Excel
8
 
9
  # Initialize session state for results, image names, and image sizes if not already present
10
  if 'results' not in st.session_state:
 
64
  for i, result_set in enumerate(results):
65
  # Initialize a dictionary for the current set with zeros
66
  current_data = {
 
67
  'Happy': 0,
68
  'Surprise': 0,
69
  'Neutral': 0,
 
71
  'Disgust': 0,
72
  'Angry': 0,
73
  'Fear': 0,
 
 
 
 
74
  'Image Name': image_names[i],
 
75
  'Image Size (KB)': f"{image_sizes[i]:.1f}" # Format the size to one decimal place
76
  }
77
 
 
97
  plt.xlabel('Emotion Categories')
98
  plt.ylabel('Data Points')
99
  st.pyplot(plt)
 
100
 
101
+ # Save the DataFrame to a CSV file
 
102
  df_emotions.to_csv('emotion_scores.csv', index=False)
103
  st.success('DataFrame generated and saved as emotion_scores.csv')
104
+
105
  with open('emotion_scores.csv', 'r') as f:
106
  csv_file = f.read()
107
+
108
  st.download_button(
109
  label='Download Emotion Scores as CSV',
110
  data=csv_file,
111
  file_name='emotion_scores.csv',
112
  mime='text/csv',
113
  )
114
+
115
+ # Save the DataFrame to an Excel file with images
116
+ if st.button("Save to Excel"):
117
+ # Create a new Excel writer object
118
+ writer = pd.ExcelWriter('emotion_scores.xlsx', engine='xlsxwriter')
119
+ df_emotions.to_excel(writer, index=False)
120
+
121
+ # Access the xlsxwriter workbook and worksheet objects
122
+ workbook = writer.book
123
+ worksheet = writer.sheets['Sheet1']
124
+
125
+ # Iterate over the images and insert them into the 'Image View' column
126
+ for idx, image_path in enumerate(st.session_state['image_names']):
127
+ # Your logic to define the image_path
128
+ worksheet.insert_image(f'J{idx + 2}', image_path) # 'J' is the 10th column
129
+
130
+ # Close the writer and save the Excel file
131
+ writer.save()
132
+ st.success('DataFrame generated and saved as emotion_scores.xlsx')
133
+
134
+ with open('emotion_scores.xlsx', 'rb') as f:
135
+ excel_file = f.read()
136
+
137
+ st.download_button(
138
+ label='Download Emotion Scores as Excel',
139
+ data=excel_file,
140
+ file_name='emotion_scores.xlsx',
141
+ mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
142
+ )
143
  else:
144
  st.error("No results to generate DataFrame. Please predict emotions first.")