fffiloni commited on
Commit
0e633ca
1 Parent(s): 98dd8fc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -0
app.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #@title Setup
2
+ import os, subprocess
3
+
4
+ def setup():
5
+ install_cmds = [
6
+ ['pip', 'install', 'ftfy', 'gradio', 'regex', 'tqdm', 'transformers==4.21.2', 'timm', 'fairscale', 'requests'],
7
+ ['pip', 'install', 'open_clip_torch'],
8
+ ['pip', 'install', '-e', 'git+https://github.com/pharmapsychotic/BLIP.git@lib#egg=blip'],
9
+ ['git', 'clone', '-b', 'open-clip', 'https://github.com/pharmapsychotic/clip-interrogator.git']
10
+ ]
11
+ for cmd in install_cmds:
12
+ print(subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8'))
13
+
14
+ setup()
15
+
16
+ # download cache files
17
+ print("Download preprocessed cache files...")
18
+ CACHE_URLS = [
19
+ 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl',
20
+ 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl',
21
+ 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_mediums.pkl',
22
+ 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl',
23
+ 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl',
24
+ ]
25
+ os.makedirs('cache', exist_ok=True)
26
+ for url in CACHE_URLS:
27
+ print(subprocess.run(['wget', url, '-P', 'cache'], stdout=subprocess.PIPE).stdout.decode('utf-8'))
28
+
29
+ import sys
30
+ sys.path.append('src/blip')
31
+ sys.path.append('clip-interrogator')
32
+
33
+ import gradio as gr
34
+ from clip_interrogator import Config, Interrogator
35
+
36
+ config = Config()
37
+ config.blip_offload = True
38
+ config.chunk_size = 2048
39
+ config.flavor_intermediate_count = 512
40
+ config.blip_num_beams = 64
41
+
42
+ ci = Interrogator(config)
43
+
44
+ def inference(image, mode, best_max_flavors):
45
+ image = image.convert('RGB')
46
+ if mode == 'best':
47
+ return ci.interrogate(image, max_flavors=int(best_max_flavors))
48
+ elif mode == 'classic':
49
+ return ci.interrogate_classic(image)
50
+ else:
51
+ return ci.interrogate_fast(image)
52
+
53
+ inputs = [
54
+ gr.inputs.Image(type='pil'),
55
+ gr.Radio(['best', 'classic', 'fast'], label='', value='best'),
56
+ gr.Number(value=4, label='best mode max flavors'),
57
+ ]
58
+ outputs = [
59
+ gr.outputs.Textbox(label="Output"),
60
+ ]
61
+
62
+ io = gr.Interface(
63
+ inference,
64
+ inputs,
65
+ outputs,
66
+ allow_flagging=False,
67
+ )
68
+ io.launch()