File size: 1,588 Bytes
f533f22
b401e5e
c8ca2bb
b401e5e
f533f22
b401e5e
 
 
 
c8ca2bb
 
 
b401e5e
c8ca2bb
b401e5e
 
 
 
 
c8ca2bb
b401e5e
 
 
c8ca2bb
 
b401e5e
 
 
 
 
59452bd
b401e5e
59452bd
b401e5e
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import gradio as gr
from huggingface_hub import login
from transformers import AutoModelForVideoClassification, AutoFeatureExtractor, pipeline
import torch

# Load the Hugging Face API token from environment variables or enter directly
# HUGGINGFACEHUB_API_TOKEN = "your_huggingface_api_token"
# login(HUGGINGFACEHUB_API_TOKEN)

# Define the model and feature extractor from Hugging Face
# model_name = "microsoft/xclip-base-patch32"
model_name = "facebook/timesformer-base-finetuned-k400"
model = AutoModelForVideoClassification.from_pretrained(model_name)
feature_extractor = AutoFeatureExtractor.from_pretrained(model_name)

# Create a video classification pipeline
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

video_pipeline = pipeline("video-classification", model=model, feature_extractor=feature_extractor, device=0 if torch.cuda.is_available() else -1)

# Define the function for video classification
def classify_video(video_path):
    predictions = video_pipeline(video_path)
    return {prediction['label']: prediction['score'] for prediction in predictions}

# Create a Gradio interface
interface = gr.Interface(
    fn=classify_video,
    inputs=gr.Video(label="Upload a video for classification"),
    outputs=gr.Label(num_top_classes=5, label="Top 5 Predicted Classes"),
    title="Video Classification using Hugging Face",
    description="Upload a video file and get the top 5 predicted classes using a Hugging Face video classification model."
)

# Launch the Gradio interface
if __name__ == "__main__":
    interface.launch()