Files changed (1) hide show
  1. app.py +25 -1
app.py CHANGED
@@ -59,7 +59,31 @@ ENDING = """For search acceleration capabilities, please refer to [Searchium.ai]
59
 
60
 
61
  DATA_PATH = '/home/user'
62
- downloaded_path = huggingface_hub.snapshot_download(repo_id="Searchium-ai/clip4clip-webvid150k", repo_type="model", cache_dir=DATA_PATH)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
 
64
  ft_visual_features_file = downloaded_path +'/new_data/video_half_dataset_visual_features.npy'
65
  ft_visual_features_file_bin = downloaded_path + '/new_data/video_half_dataset_visual_features_binary_packed.npy'
 
59
 
60
 
61
  DATA_PATH = '/home/user'
62
+ # 1. 加载模型和处理器(只下载模型权重,约几百MB)
63
+ model = CLIPModel.from_pretrained("Searchium-ai/clip4clip-webvid150k")
64
+ processor = CLIPProcessor.from_pretrained("Searchium-ai/clip4clip-webvid150k")
65
+
66
+ # 2. 将模型移到GPU(如果可用)或CPU
67
+ device = "cuda" if torch.cuda.is_available() else "cpu"
68
+ model = model.to(device)
69
+
70
+ # 3. 如果需要提取文本嵌入,可以这样做:
71
+ def get_text_embedding(text):
72
+ inputs = processor(text=text, return_tensors="pt", padding=True, truncation=True)
73
+ inputs = {k: v.to(device) for k, v in inputs.items()}
74
+ with torch.no_grad():
75
+ text_features = model.get_text_features(**inputs)
76
+ return text_features.cpu().numpy()
77
+
78
+ # 4. 如果需要提取视频帧嵌入,可以这样做:
79
+ def get_video_embedding(frames):
80
+ # frames: 一个PIL Image列表,每个元素是一帧
81
+ inputs = processor(images=frames, return_tensors="pt", padding=True)
82
+ inputs = {k: v.to(device) for k, v in inputs.items()}
83
+ with torch.no_grad():
84
+ video_features = model.get_image_features(**inputs)
85
+ # 对于多帧视频,可以对帧特征取平均
86
+ return video_features.mean(dim=0).cpu().numpy()
87
 
88
  ft_visual_features_file = downloaded_path +'/new_data/video_half_dataset_visual_features.npy'
89
  ft_visual_features_file_bin = downloaded_path + '/new_data/video_half_dataset_visual_features_binary_packed.npy'