|
1 | 1 | import os |
2 | | -import streamlit as st |
| 2 | +import glob |
| 3 | +import sys |
3 | 4 | import shutil |
4 | | -from transformers import CLIPTokenizerFast, CLIPProcessor, CLIPModel |
5 | | -import torch |
6 | 5 |
|
| 6 | +import streamlit as st |
| 7 | +import torch |
7 | 8 | from PIL import Image |
8 | | -import os |
9 | 9 | from tqdm import tqdm |
10 | 10 | import numpy as np |
11 | 11 |
|
12 | | -import glob |
13 | 12 | from func import * |
14 | 13 |
|
| 14 | +if not sys.warnoptions: |
| 15 | + import warnings |
| 16 | + warnings.simplefilter("ignore") |
| 17 | + |
| 18 | + |
| 19 | +@st.cache_resource |
| 20 | +def upload_models(): |
| 21 | + # Кэшируем все модели и обработчики CLIP |
| 22 | + from transformers import CLIPTokenizerFast, CLIPProcessor, CLIPModel |
| 23 | + |
| 24 | + print('Начался процесс подгрузки модели:') |
| 25 | + device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') |
| 26 | + print(device) |
| 27 | + |
| 28 | + model_id = 'openai/clip-vit-base-patch32' |
| 29 | + model = CLIPModel.from_pretrained(model_id).to(device) |
| 30 | + tokenizer = CLIPTokenizerFast.from_pretrained(model_id) |
| 31 | + processor = CLIPProcessor.from_pretrained(model_id) |
| 32 | + return model, tokenizer, processor, device |
| 33 | + |
| 34 | + |
| 35 | +def set_page_static_info(): |
| 36 | + st.set_page_config( |
| 37 | + page_title="YouTube-searcher", |
| 38 | + page_icon="configs/logo.png", |
| 39 | + layout="wide", |
| 40 | + initial_sidebar_state="expanded", |
| 41 | + ) |
| 42 | + st.title("Поисковик по ютуб видео") |
| 43 | + |
15 | 44 |
|
16 | 45 | def make_images_and_embedding(video_urls, seconds_step=10): |
17 | 46 | # Удаляем папку images, если она уже существует |
@@ -60,7 +89,6 @@ def make_images_and_embedding(video_urls, seconds_step=10): |
60 | 89 | np.save('image_embeddings.npy', image_arr) |
61 | 90 |
|
62 | 91 |
|
63 | | - |
64 | 92 | def compute_k_nearest_imaget_to_text_prompt(text_imput, top_k): |
65 | 93 | prompt = "a photo of " + text_imput |
66 | 94 | # tokenize the prompt |
@@ -106,20 +134,7 @@ def compute_k_nearest_imaget_to_image_prompt(image_array, top_k): |
106 | 134 | return images_out, list_of_links |
107 | 135 |
|
108 | 136 |
|
109 | | - |
110 | | -def set_page_static_info(): |
111 | | - st.set_page_config( |
112 | | - page_title="YouTube-searcher", |
113 | | - page_icon="configs/logo.png", |
114 | | - layout="wide", |
115 | | - initial_sidebar_state="expanded", |
116 | | - ) |
117 | | - st.title("Поисковик по ютуб видео") |
118 | | - |
119 | | - |
120 | 137 | def main(): |
121 | | - set_page_static_info() |
122 | | - |
123 | 138 | st.sidebar.title("Загрузчик ютуб видео") |
124 | 139 | text_input_url = st.sidebar.text_area("Введите URL-адреса видео (каждый youtube url на новой строке):") |
125 | 140 | seconds_step = st.sidebar.number_input("Введите шаг нарезки в секундах:", min_value=1, value=10, step=1) |
@@ -173,14 +188,6 @@ def main(): |
173 | 188 |
|
174 | 189 |
|
175 | 190 | if __name__ == "__main__": |
176 | | - |
177 | | - print('Начался процесс подгрузки модели:') |
178 | | - device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') |
179 | | - print(device) |
180 | | - |
181 | | - model_id = 'openai/clip-vit-base-patch32' |
182 | | - model = CLIPModel.from_pretrained(model_id).to(device) |
183 | | - tokenizer = CLIPTokenizerFast.from_pretrained(model_id) |
184 | | - processor = CLIPProcessor.from_pretrained(model_id) |
185 | | - |
| 191 | + set_page_static_info() |
| 192 | + model, tokenizer, processor, device = upload_models() |
186 | 193 | main() |
0 commit comments