Skip to content

Commit 1ec4ff0

Browse files
committed
cache_resource
1 parent 1ab3c1a commit 1ec4ff0

1 file changed

Lines changed: 36 additions & 29 deletions

File tree

web.py

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
11
import os
2-
import streamlit as st
2+
import glob
3+
import sys
34
import shutil
4-
from transformers import CLIPTokenizerFast, CLIPProcessor, CLIPModel
5-
import torch
65

6+
import streamlit as st
7+
import torch
78
from PIL import Image
8-
import os
99
from tqdm import tqdm
1010
import numpy as np
1111

12-
import glob
1312
from func import *
1413

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+
1544

1645
def make_images_and_embedding(video_urls, seconds_step=10):
1746
# Удаляем папку images, если она уже существует
@@ -60,7 +89,6 @@ def make_images_and_embedding(video_urls, seconds_step=10):
6089
np.save('image_embeddings.npy', image_arr)
6190

6291

63-
6492
def compute_k_nearest_imaget_to_text_prompt(text_imput, top_k):
6593
prompt = "a photo of " + text_imput
6694
# tokenize the prompt
@@ -106,20 +134,7 @@ def compute_k_nearest_imaget_to_image_prompt(image_array, top_k):
106134
return images_out, list_of_links
107135

108136

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-
120137
def main():
121-
set_page_static_info()
122-
123138
st.sidebar.title("Загрузчик ютуб видео")
124139
text_input_url = st.sidebar.text_area("Введите URL-адреса видео (каждый youtube url на новой строке):")
125140
seconds_step = st.sidebar.number_input("Введите шаг нарезки в секундах:", min_value=1, value=10, step=1)
@@ -173,14 +188,6 @@ def main():
173188

174189

175190
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()
186193
main()

0 commit comments

Comments
 (0)