xtlyxt commited on
Commit
030c58e
·
verified ·
1 Parent(s): e99de43

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -42
app.py CHANGED
@@ -8,6 +8,10 @@ import matplotlib.pyplot as plt
8
  # Disable PyplotGlobalUseWarning
9
  st.set_option('deprecation.showPyplotGlobalUse', False)
10
 
 
 
 
 
11
  # Create an image classification pipeline with scores
12
  pipe = pipeline("image-classification", model="trpakov/vit-face-expression", top_k=None)
13
 
@@ -159,45 +163,45 @@ st.write("results info:", results)
159
 
160
  # Generate DataFrame from results
161
  if st.button("Generate DataFrame"):
162
-
163
- #if st.button("Generate DataFrame") and results:
164
-
165
- st.write("results info inner loop:", results)
166
-
167
- # Initialize an empty dictionary to store the scores
168
- emotion_scores = {
169
- 'Neutral': [],
170
- 'Happy': [],
171
- 'Surprise': [],
172
- 'Disgust': [],
173
- 'Angry': [],
174
- # Add other emotions if necessary
175
- 'Sad': [], # Add this if you have 'sad' scores in your results
176
- 'Fear': [] # Add this if you have 'fear' scores in your results
177
- }
178
-
179
- # Iterate over the results and populate the dictionary
180
- for result_set in results:
181
- # Initialize a dictionary for the current set with zeros
182
- current_scores = {emotion: 0 for emotion in emotion_scores.keys()}
183
-
184
- for result in result_set:
185
- # Capitalize the label and update the score in the current set
186
- emotion = result['label'].capitalize()
187
- score = round(result['score'], 4) # Round the score to 4 decimal places
188
- current_scores[emotion] = score
189
-
190
- # Add the current scores to the emotion_scores dictionary
191
- for emotion, score in current_scores.items():
192
- emotion_scores[emotion].append(score)
193
-
194
- # Convert the dictionary into a pandas DataFrame
195
- df_emotions = pd.DataFrame(emotion_scores)
196
-
197
- # Display the DataFrame
198
- st.write(df_emotions)
199
-
200
- # Optional: Save the DataFrame to a CSV file
201
- df_emotions.to_csv('emotion_scores.csv', index=False)
202
- st.success('DataFrame generated and saved as emotion_scores.csv')
203
-
 
8
  # Disable PyplotGlobalUseWarning
9
  st.set_option('deprecation.showPyplotGlobalUse', False)
10
 
11
+ # Initialize session state for results if not already present
12
+ if 'results' not in st.session_state:
13
+ st.session_state['results'] = []
14
+
15
  # Create an image classification pipeline with scores
16
  pipe = pipeline("image-classification", model="trpakov/vit-face-expression", top_k=None)
17
 
 
163
 
164
  # Generate DataFrame from results
165
  if st.button("Generate DataFrame"):
166
+ # Access the results from the session state
167
+ results = st.session_state['results']
168
+ if results:
169
+ st.write("results info inner loop:", results)
170
+ # Initialize an empty dictionary to store the scores
171
+ emotion_scores = {
172
+ 'Neutral': [],
173
+ 'Happy': [],
174
+ 'Surprise': [],
175
+ 'Disgust': [],
176
+ 'Angry': [],
177
+ # Add other emotions if necessary
178
+ 'Sad': [], # Add this if you have 'sad' scores in your results
179
+ 'Fear': [] # Add this if you have 'fear' scores in your results
180
+ }
181
+
182
+ # Iterate over the results and populate the dictionary
183
+ for result_set in results:
184
+ # Initialize a dictionary for the current set with zeros
185
+ current_scores = {emotion: 0 for emotion in emotion_scores.keys()}
186
+
187
+ for result in result_set:
188
+ # Capitalize the label and update the score in the current set
189
+ emotion = result['label'].capitalize()
190
+ score = round(result['score'], 4) # Round the score to 4 decimal places
191
+ current_scores[emotion] = score
192
+
193
+ # Add the current scores to the emotion_scores dictionary
194
+ for emotion, score in current_scores.items():
195
+ emotion_scores[emotion].append(score)
196
+
197
+ # Convert the dictionary into a pandas DataFrame
198
+ df_emotions = pd.DataFrame(emotion_scores)
199
+
200
+ # Display the DataFrame
201
+ st.write(df_emotions)
202
+
203
+ # Optional: Save the DataFrame to a CSV file
204
+ df_emotions.to_csv('emotion_scores.csv', index=False)
205
+ st.success('DataFrame generated and saved as emotion_scores.csv')
206
+ else:
207
+ st.error("No results to generate DataFrame. Please predict emotions first.")