jschwab21 commited on
Commit
55ea255
·
verified ·
1 Parent(s): 284005c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -14
app.py CHANGED
@@ -70,11 +70,19 @@ def display_results(video_url, video_file, description):
70
  if not scenes:
71
  return "No scenes detected", None, None
72
 
73
- final_clip_path, sentiments = analyze_scenes(video_path, scenes, description)
74
- if final_clip_path:
75
- return final_clip_path, sentiments
 
 
 
 
 
 
 
76
  else:
77
- return "No matching scene found", None, None
 
78
 
79
 
80
  # Custom CSS for additional styling
@@ -137,14 +145,27 @@ def save_uploaded_file(uploaded_file):
137
 
138
  import matplotlib.pyplot as plt
139
 
140
- def create_plot(sentiments):
141
- categories = ["Joy", "Trust", "Fear", "Surprise", "Sadness", "Disgust", "Anger", "Anticipation"]
142
- fig, ax = plt.subplots()
143
- ax.bar(categories, sentiments)
144
- ax.set_ylabel('Probability')
145
- ax.set_title('Sentiment Distribution')
146
- plt.setp(ax.get_xticklabels(), rotation=45, horizontalalignment='right')
147
- return fig
 
 
 
 
 
 
 
 
 
 
 
 
 
148
 
149
  with gr.Blocks(theme=custom_theme, css=css) as demo:
150
  with gr.Column():
@@ -154,6 +175,6 @@ with gr.Blocks(theme=custom_theme, css=css) as demo:
154
  description = gr.Textbox(label="Describe your clip:", elem_id="description")
155
  submit_button = gr.Button("Process Video", elem_id="submit_button")
156
  video_output = gr.Video(label="Processed Video", elem_id="video_output")
157
- sentiment_plot = gr.Plot(label="Sentiment Analysis", elem_id="sentiment_plot")
158
- submit_button.click(fn=display_results, inputs=[video_url, video_file, description], outputs=[video_output, sentiment_plot])
159
  demo.launch()
 
70
  if not scenes:
71
  return "No scenes detected", None, None
72
 
73
+ best_scene, sentiment_distribution = analyze_scenes(video_path, scenes, description)
74
+ final_clip = extract_best_scene(video_path, best_scene)
75
+ if final_clip:
76
+ output_dir = "output"
77
+ os.makedirs(output_dir, exist_ok=True)
78
+ final_clip_path = os.path.join(output_dir, f"{uuid.uuid4()}_final_clip.mp4")
79
+ final_clip.write_videofile(final_clip_path, codec='libx264', audio_codec='aac')
80
+ cleanup_temp_files()
81
+ plot = create_radial_plot(sentiment_distribution)
82
+ return final_clip_path, plot
83
  else:
84
+ return "No matching scene found", None
85
+
86
 
87
 
88
  # Custom CSS for additional styling
 
145
 
146
  import matplotlib.pyplot as plt
147
 
148
+ def create_radial_plot(sentiments):
149
+ # Convert sentiment dictionary to lists
150
+ labels = list(sentiments.keys())
151
+ values = list(sentiments.values())
152
+
153
+ # Create radial plot
154
+ angles = np.linspace(0, 2 * np.pi, len(labels), endpoint=False).tolist()
155
+
156
+ fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
157
+ ax.fill(angles, values, color='red', alpha=0.25)
158
+ ax.set_yticklabels([])
159
+ ax.set_xticks(angles)
160
+ ax.set_xticklabels(labels)
161
+
162
+ # Save plot to a string buffer
163
+ from io import BytesIO
164
+ buf = BytesIO()
165
+ plt.savefig(buf, format='png')
166
+ plt.close(fig)
167
+ buf.seek(0)
168
+ return buf
169
 
170
  with gr.Blocks(theme=custom_theme, css=css) as demo:
171
  with gr.Column():
 
175
  description = gr.Textbox(label="Describe your clip:", elem_id="description")
176
  submit_button = gr.Button("Process Video", elem_id="submit_button")
177
  video_output = gr.Video(label="Processed Video", elem_id="video_output")
178
+ sentiment_plot = gr.Plot(label="Sentiment Distribution")
179
+ submit_button.click(fn=display_results, inputs=[video_url, video_file, description], outputs=[video_output, download_output, sentiment_plot])
180
  demo.launch()