daphnai commited on
Commit
f50a1ff
·
verified ·
1 Parent(s): 68b7067

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -31
app.py CHANGED
@@ -56,47 +56,23 @@ ENDING = """For search acceleration capabilities, please refer to [Searchium.ai]
56
  """
57
 
58
 
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'
90
 
91
- #load database features:
92
  ft_visual_features_database_bin = np.load(ft_visual_features_file_bin)
93
  ft_visual_features_database = np.load(ft_visual_features_file, mmap_mode='r')
94
 
95
-
96
-
97
  database_csv_path = downloaded_path + '/new_data/video_half_dataset.csv'
98
  database_df = pd.read_csv(database_csv_path)
99
 
 
100
  class NearestNeighbors:
101
  """
102
  Class for NearestNeighbors.
 
56
  """
57
 
58
 
 
 
59
  DATA_PATH = '/home/user'
60
+ downloaded_path = huggingface_hub.snapshot_download(
61
+ repo_id="Searchium-ai/clip4clip-webvid150k",
62
+ repo_type="model",
63
+ cache_dir=DATA_PATH)
 
 
 
 
 
 
 
 
 
 
 
64
 
65
+ ft_visual_features_file = downloaded_path + '/new_data/video_half_dataset_visual_features.npy'
 
 
 
 
 
 
 
 
 
 
66
  ft_visual_features_file_bin = downloaded_path + '/new_data/video_half_dataset_visual_features_binary_packed.npy'
67
 
68
+ # load database features:
69
  ft_visual_features_database_bin = np.load(ft_visual_features_file_bin)
70
  ft_visual_features_database = np.load(ft_visual_features_file, mmap_mode='r')
71
 
 
 
72
  database_csv_path = downloaded_path + '/new_data/video_half_dataset.csv'
73
  database_df = pd.read_csv(database_csv_path)
74
 
75
+
76
  class NearestNeighbors:
77
  """
78
  Class for NearestNeighbors.