Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,37 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import login
|
3 |
+
from transformers import VideoClassificationPipeline, AutoModelForVideoClassification, AutoProcessor
|
4 |
+
import torch
|
5 |
|
6 |
+
# Load the Hugging Face API token from environment variables or enter directly
|
7 |
+
# HUGGINGFACEHUB_API_TOKEN = "your_huggingface_api_token"
|
8 |
+
# login(HUGGINGFACEHUB_API_TOKEN)
|
9 |
+
|
10 |
+
# Define the model and processor from Hugging Face
|
11 |
+
model_name = "microsoft/xclip-base-patch32"
|
12 |
+
model = AutoModelForVideoClassification.from_pretrained(model_name)
|
13 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
14 |
+
|
15 |
+
# Create a video classification pipeline
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
model.to(device)
|
18 |
+
|
19 |
+
pipeline = VideoClassificationPipeline(model=model, feature_extractor=processor, device=0 if torch.cuda.is_available() else -1)
|
20 |
+
|
21 |
+
# Define the function for video classification
|
22 |
+
def classify_video(video_path):
|
23 |
+
predictions = pipeline(video_path)
|
24 |
+
return predictions
|
25 |
+
|
26 |
+
# Create a Gradio interface
|
27 |
+
interface = gr.Interface(
|
28 |
+
fn=classify_video,
|
29 |
+
inputs=gr.Video(label="Upload a video for classification"),
|
30 |
+
outputs=gr.Label(num_top_classes=3, label="Top 3 Predicted Classes"),
|
31 |
+
title="Video Classification using Hugging Face",
|
32 |
+
description="Upload a video file and get the top 3 predicted classes using a Hugging Face video classification model."
|
33 |
+
)
|
34 |
+
|
35 |
+
# Launch the Gradio interface
|
36 |
+
if __name__ == "__main__":
|
37 |
+
interface.launch()
|