Spaces:
Paused
Paused
Add app.py and requirements.txt for video description demo
Browse filesFiles changed (2)
- app.pyAdded+41
- requirements.textAdded+6
1234567891011121314151617181920212223242526272829303132333435363738394041import gradio as grfrom transformers import VideoLlavaProcessor, VideoLlavaForConditionalGenerationimport torchimport numpy as npimport avfrom moviepy.editor import VideoFileClip
# Load model (runs on Space's hardware)model = VideoLlavaForConditionalGeneration.from_pretrained("LanguageBind/Video-LLaVA-7B-hf")processor = VideoLlavaProcessor.from_pretrained("LanguageBind/Video-LLaVA-7B-hf")
def extract_frames(video_path, num_frames=8): container = av.open(video_path) total_frames = container.streams.video[0].frames indices = np.arange(0, total_frames, total_frames // num_frames).astype(int) frames = [] for i, frame in enumerate(container.decode(video=0)): if i in indices: frames.append(frame.to_ndarray(format="rgb24")) return np.stack(frames)
def describe_video(video): if video is None: return "No video uploaded." frames = extract_frames(video) # Simple scene simulation (for full, add PySceneDetect) prompt = "USER: <video> Describe this video scene-by-scene with timestamps, including actions, objects, and any instructions. ASSISTANT:" inputs = processor(text=prompt, videos=frames, return_tensors="pt") generate_ids = model.generate(**inputs, max_new_tokens=500) output = processor.batch_decode(generate_ids, skip_special_tokens=True)[0] return output
# Gradio UIiface = gr.Interface( fn=describe_video, inputs=gr.Video(label="Upload Video (MP4, e.g., TikTok/Reel)"), outputs=gr.Textbox(label="Video Description"), title="Video Description Demo (Video-LLaVA)", description="Upload a short video to get a detailed, timestamped description.")iface.launch()123456gradiotransformerstorchavmoviepynumpyapp.pyAdded+41
requirements.textAdded+6