app.py
CHANGED
@@ -63,6 +63,7 @@ def create_ui(examples_path: str):
|
|
63 |
gr.Markdown(f"#Summary: {example['analysis']['video_description']}")
|
64 |
gr.Markdown(f"#Highlights to search for: {example['analysis']['highlight_types']}")
|
65 |
|
|
|
66 |
gr.Markdown("## Try It Yourself!")
|
67 |
with gr.Row():
|
68 |
with gr.Column(scale=1):
|
@@ -81,113 +82,116 @@ def create_ui(examples_path: str):
|
|
81 |
|
82 |
status = gr.Markdown()
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
|
|
|
|
|
|
87 |
video_description = gr.Markdown("", elem_id="video_desc")
|
88 |
highlight_types = gr.Markdown("", elem_id="highlight_types")
|
89 |
|
90 |
@spaces.GPU
|
91 |
-
def
|
92 |
if not video:
|
93 |
-
|
94 |
-
"Please upload a video",
|
95 |
-
"",
|
96 |
-
"",
|
97 |
-
gr.update(visible=False),
|
98 |
-
gr.update(visible=False)
|
99 |
-
|
|
|
100 |
|
101 |
try:
|
102 |
duration = get_video_duration_seconds(video)
|
103 |
if duration > 1200: # 20 minutes
|
104 |
-
|
105 |
-
"Video must be shorter than 20 minutes",
|
106 |
-
"",
|
107 |
-
"",
|
108 |
-
gr.update(visible=False),
|
109 |
-
gr.update(visible=False)
|
110 |
-
|
111 |
-
|
112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
model, processor = load_model()
|
114 |
detector = BatchedVideoHighlightDetector(model, processor, batch_size=8)
|
115 |
|
116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
video_desc = detector.analyze_video_content(video)
|
118 |
formatted_desc = f"#Summary: {video_desc[:500] + '...' if len(video_desc) > 500 else video_desc}"
|
119 |
|
120 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
highlights = detector.determine_highlights(video_desc)
|
122 |
formatted_highlights = f"#Highlights to search for: {highlights[:500] + '...' if len(highlights) > 500 else highlights}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
-
# Create highlight video
|
125 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp_file:
|
126 |
temp_output = tmp_file.name
|
127 |
detector.create_highlight_video(video, temp_output)
|
128 |
|
129 |
-
|
130 |
-
"Processing complete!",
|
131 |
-
formatted_desc,
|
132 |
-
formatted_highlights,
|
133 |
-
gr.update(value=temp_output, visible=True),
|
134 |
-
gr.update(visible=True)
|
135 |
-
|
136 |
|
137 |
except Exception as e:
|
138 |
-
|
139 |
-
f"Error processing video: {str(e)}",
|
140 |
-
"",
|
141 |
-
"",
|
142 |
-
gr.update(visible=False),
|
143 |
-
gr.update(visible=False)
|
144 |
-
|
145 |
-
|
146 |
-
def process_with_updates(video):
|
147 |
-
# Initial state
|
148 |
-
yield [
|
149 |
-
"Loading model...",
|
150 |
-
"",
|
151 |
-
"",
|
152 |
-
gr.update(visible=False),
|
153 |
-
gr.update(visible=True)
|
154 |
-
]
|
155 |
-
|
156 |
-
# Analyzing video
|
157 |
-
yield [
|
158 |
-
"Analyzing video content...",
|
159 |
-
"",
|
160 |
-
"",
|
161 |
-
gr.update(visible=False),
|
162 |
-
gr.update(visible=True)
|
163 |
-
]
|
164 |
-
|
165 |
-
# Get final results
|
166 |
-
results = process_video(video)
|
167 |
-
|
168 |
-
# If we're still processing, show an intermediate state
|
169 |
-
if results[0] != "Processing complete!":
|
170 |
-
yield [
|
171 |
-
"Detecting and extracting highlights...",
|
172 |
-
results[1], # description
|
173 |
-
results[2], # highlights
|
174 |
-
gr.update(visible=False),
|
175 |
-
gr.update(visible=True)
|
176 |
-
]
|
177 |
-
|
178 |
-
# Return final state
|
179 |
-
yield results
|
180 |
|
181 |
process_btn.click(
|
182 |
-
|
183 |
inputs=[input_video],
|
184 |
outputs=[
|
185 |
status,
|
186 |
video_description,
|
187 |
highlight_types,
|
188 |
output_video,
|
189 |
-
|
190 |
-
]
|
|
|
191 |
)
|
192 |
|
193 |
return app
|
|
|
63 |
gr.Markdown(f"#Summary: {example['analysis']['video_description']}")
|
64 |
gr.Markdown(f"#Highlights to search for: {example['analysis']['highlight_types']}")
|
65 |
|
66 |
+
|
67 |
gr.Markdown("## Try It Yourself!")
|
68 |
with gr.Row():
|
69 |
with gr.Column(scale=1):
|
|
|
82 |
|
83 |
status = gr.Markdown()
|
84 |
|
85 |
+
analysis_accordion = gr.Accordion(
|
86 |
+
"Model chain of thought details",
|
87 |
+
open=True,
|
88 |
+
visible=False
|
89 |
+
)
|
90 |
+
|
91 |
+
with analysis_accordion:
|
92 |
video_description = gr.Markdown("", elem_id="video_desc")
|
93 |
highlight_types = gr.Markdown("", elem_id="highlight_types")
|
94 |
|
95 |
@spaces.GPU
|
96 |
+
def on_process(video):
|
97 |
if not video:
|
98 |
+
yield {
|
99 |
+
"status": "Please upload a video", # Changed to string key
|
100 |
+
"video_description": gr.update(value=""), # Added gr.update
|
101 |
+
"highlight_types": gr.update(value=""), # Added gr.update
|
102 |
+
"output_video": gr.update(visible=False),
|
103 |
+
"analysis_accordion": gr.update(visible=False)
|
104 |
+
}
|
105 |
+
return
|
106 |
|
107 |
try:
|
108 |
duration = get_video_duration_seconds(video)
|
109 |
if duration > 1200: # 20 minutes
|
110 |
+
yield {
|
111 |
+
"status": "Video must be shorter than 20 minutes",
|
112 |
+
"video_description": gr.update(value=""),
|
113 |
+
"highlight_types": gr.update(value=""),
|
114 |
+
"output_video": gr.update(visible=False),
|
115 |
+
"analysis_accordion": gr.update(visible=False)
|
116 |
+
}
|
117 |
+
return
|
118 |
+
|
119 |
+
# Make accordion visible as soon as processing starts
|
120 |
+
yield {
|
121 |
+
"status": "Loading model...",
|
122 |
+
"video_description": gr.update(value=""),
|
123 |
+
"highlight_types": gr.update(value=""),
|
124 |
+
"output_video": gr.update(visible=False),
|
125 |
+
"analysis_accordion": gr.update(visible=True)
|
126 |
+
}
|
127 |
+
|
128 |
model, processor = load_model()
|
129 |
detector = BatchedVideoHighlightDetector(model, processor, batch_size=8)
|
130 |
|
131 |
+
yield {
|
132 |
+
"status": "Analyzing video content...",
|
133 |
+
"video_description": gr.update(value=""),
|
134 |
+
"highlight_types": gr.update(value=""),
|
135 |
+
"output_video": gr.update(visible=False),
|
136 |
+
"analysis_accordion": gr.update(visible=True)
|
137 |
+
}
|
138 |
+
|
139 |
video_desc = detector.analyze_video_content(video)
|
140 |
formatted_desc = f"#Summary: {video_desc[:500] + '...' if len(video_desc) > 500 else video_desc}"
|
141 |
|
142 |
+
# Update description as soon as it's available
|
143 |
+
yield {
|
144 |
+
"status": "Determining highlight types...",
|
145 |
+
"video_description": gr.update(value=formatted_desc),
|
146 |
+
"highlight_types": gr.update(value=""),
|
147 |
+
"output_video": gr.update(visible=False),
|
148 |
+
"analysis_accordion": gr.update(visible=True)
|
149 |
+
}
|
150 |
+
|
151 |
highlights = detector.determine_highlights(video_desc)
|
152 |
formatted_highlights = f"#Highlights to search for: {highlights[:500] + '...' if len(highlights) > 500 else highlights}"
|
153 |
+
|
154 |
+
# Update highlights as soon as they're available
|
155 |
+
yield {
|
156 |
+
"status": "Detecting and extracting highlights...",
|
157 |
+
"video_description": gr.update(value=formatted_desc),
|
158 |
+
"highlight_types": gr.update(value=formatted_highlights),
|
159 |
+
"output_video": gr.update(visible=False),
|
160 |
+
"analysis_accordion": gr.update(visible=True)
|
161 |
+
}
|
162 |
|
|
|
163 |
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as tmp_file:
|
164 |
temp_output = tmp_file.name
|
165 |
detector.create_highlight_video(video, temp_output)
|
166 |
|
167 |
+
yield {
|
168 |
+
"status": "Processing complete!",
|
169 |
+
"video_description": gr.update(value=formatted_desc),
|
170 |
+
"highlight_types": gr.update(value=formatted_highlights),
|
171 |
+
"output_video": gr.update(value=temp_output, visible=True),
|
172 |
+
"analysis_accordion": gr.update(visible=True)
|
173 |
+
}
|
174 |
|
175 |
except Exception as e:
|
176 |
+
yield {
|
177 |
+
"status": f"Error processing video: {str(e)}",
|
178 |
+
"video_description": gr.update(value=""),
|
179 |
+
"highlight_types": gr.update(value=""),
|
180 |
+
"output_video": gr.update(visible=False),
|
181 |
+
"analysis_accordion": gr.update(visible=False)
|
182 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
process_btn.click(
|
185 |
+
on_process,
|
186 |
inputs=[input_video],
|
187 |
outputs=[
|
188 |
status,
|
189 |
video_description,
|
190 |
highlight_types,
|
191 |
output_video,
|
192 |
+
analysis_accordion
|
193 |
+
],
|
194 |
+
queue=True, # Added queue=True
|
195 |
)
|
196 |
|
197 |
return app
|