fffiloni commited on
Commit
a280f1b
·
verified ·
1 Parent(s): 286aa52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +146 -94
app.py CHANGED
@@ -1,152 +1,204 @@
1
- import os, subprocess
 
 
2
  import torch
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.device = 'cuda' if torch.cuda.is_available() else 'cpu'
38
  config.blip_offload = False if torch.cuda.is_available() else True
39
  config.chunk_size = 2048
40
  config.flavor_intermediate_count = 512
41
  config.blip_num_beams = 64
 
42
  ci = Interrogator(config)
43
 
44
 
45
  def inference(image, mode, best_max_flavors):
46
- """
47
- Generate a descriptive prompt from an input image using different interrogation modes.
48
-
49
- Args:
50
- image: A PIL Image object representing the input image to be analyzed.
51
- mode: A string specifying the interrogation mode to use.
52
- Can be one of ['best', 'classic', 'fast']:
53
- - 'best': Produces a prompt using the 'best' interrogation mode with max flavors control.
54
- - 'classic': Uses the classic interrogation method.
55
- - 'fast': Uses a faster but less detailed interrogation method.
56
- best_max_flavors: An integer controlling the maximum number of flavor descriptors
57
- when using 'best' mode (ignored in other modes).
58
-
59
- Returns:
60
- A string containing the generated prompt describing the image.
61
- """
62
-
63
-
64
- image = image.convert('RGB')
65
- if mode == 'best':
66
-
67
- prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
68
-
69
- print("mode best: " + prompt_result)
70
-
71
  return prompt_result
72
-
73
- elif mode == 'classic':
74
-
75
  prompt_result = ci.interrogate_classic(image)
76
-
77
- print("mode classic: " + prompt_result)
78
-
79
- return prompt_result
80
-
81
- else:
82
-
83
- prompt_result = ci.interrogate_fast(image)
84
-
85
- print("mode fast: " + prompt_result)
86
-
87
  return prompt_result
88
 
 
 
 
 
 
89
  title = """
90
- <div style="text-align: center; max-width: 500px; margin: 0 auto;">
91
- <div
92
- style="
93
- display: inline-flex;
94
- align-items: center;
95
- gap: 0.8rem;
96
- font-size: 1.75rem;
97
- margin-bottom: 10px;
98
- "
99
- >
100
  <h1 style="font-weight: 600; margin-bottom: 7px;">
101
  CLIP Interrogator 2.1
102
  </h1>
103
- </div>
104
- <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
105
- Want to figure out what a good prompt might be to create new images like an existing one?
106
  <br />The CLIP Interrogator is here to get you answers!
107
  <br />This version is specialized for producing nice prompts for use with Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model!
108
- </p>
109
- </div>
110
  """
111
 
112
  article = """
113
  <div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
114
-
115
  <p>
116
- Server busy? You can also run on <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/open-clip/clip_interrogator.ipynb">Google Colab</a>
 
117
  </p>
118
  <p>
119
- Has this been helpful to you? Follow Pharma on twitter
120
- <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
121
- and check out more tools at his
122
- <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
123
  </p>
124
  </div>
125
  """
126
 
127
- css = '''
128
- #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
129
- a {text-decoration-line: underline; font-weight: 600;}
130
- '''
 
 
 
 
 
 
 
 
 
131
 
132
  with gr.Blocks(css=css) as demo:
133
  with gr.Column(elem_id="col-container"):
134
  gr.HTML(title)
135
 
136
- input_image = gr.Image(type='pil', elem_id="input-img")
 
137
  with gr.Row():
138
- mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
139
- flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='best mode max flavors')
140
-
 
 
 
 
 
 
 
 
 
 
 
141
  submit_btn = gr.Button("Submit")
142
-
143
- output_text = gr.Textbox(label="Description Output", elem_id="output-txt")
144
 
145
- examples=[['27E894C4-9375-48A1-A95D-CB2425416B4B.png', "best",4], ['DB362F56-BA98-4CA1-A999-A25AA94B723B.png',"fast",4]]
146
- ex = gr.Examples(examples=examples, fn=inference, inputs=[input_image, mode_input, flavor_input], outputs=[output_text], cache_examples=False, run_on_click=True)
147
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
148
  gr.HTML(article)
149
 
150
- submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[output_text], api_name="clipi2")
151
-
152
- demo.queue(max_size=32).launch(show_api=True, ssr_mode=False, mcp_server=True)
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import subprocess
4
  import torch
5
 
6
+
7
+ def run(cmd):
8
+ print(f"\nRunning: {' '.join(cmd)}")
9
+ subprocess.run(cmd, check=True)
10
+
11
+
12
  def setup():
13
  install_cmds = [
14
+ ["pip", "install", "setuptools<82", "wheel"],
15
+ [
16
+ "pip", "install",
17
+ "ftfy",
18
+ "regex",
19
+ "tqdm",
20
+ "transformers==4.21.2",
21
+ "timm",
22
+ "fairscale",
23
+ "requests",
24
+ ],
25
+ [
26
+ "pip", "install",
27
+ "--no-build-isolation",
28
+ "-e",
29
+ "git+https://github.com/pharmapsychotic/BLIP.git@lib#egg=blip",
30
+ ],
31
  ]
32
+
33
  for cmd in install_cmds:
34
+ run(cmd)
35
+
36
+ if not os.path.exists("clip-interrogator"):
37
+ run([
38
+ "git", "clone",
39
+ "-b", "open-clip",
40
+ "https://github.com/pharmapsychotic/clip-interrogator.git",
41
+ ])
42
+
43
 
44
  setup()
45
 
46
+
47
  print("Download preprocessed cache files...")
48
+
49
  CACHE_URLS = [
50
+ "https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl",
51
+ "https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl",
52
+ "https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_mediums.pkl",
53
+ "https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl",
54
+ "https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl",
55
  ]
56
+
57
+ os.makedirs("cache", exist_ok=True)
58
+
59
  for url in CACHE_URLS:
60
+ subprocess.run(["wget", "-nc", url, "-P", "cache"], check=False)
61
 
62
+
63
+ sys.path.append("src/blip")
64
+ sys.path.append("clip-interrogator")
65
 
66
  import gradio as gr
67
  from clip_interrogator import Config, Interrogator
68
 
69
+
70
  config = Config()
71
+ config.device = "cuda" if torch.cuda.is_available() else "cpu"
72
  config.blip_offload = False if torch.cuda.is_available() else True
73
  config.chunk_size = 2048
74
  config.flavor_intermediate_count = 512
75
  config.blip_num_beams = 64
76
+
77
  ci = Interrogator(config)
78
 
79
 
80
  def inference(image, mode, best_max_flavors):
81
+ if image is None:
82
+ return "Please upload an image."
83
+
84
+ image = image.convert("RGB")
85
+
86
+ if mode == "best":
87
+ prompt_result = ci.interrogate(
88
+ image,
89
+ max_flavors=int(best_max_flavors),
90
+ )
91
+ print("mode best:", prompt_result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92
  return prompt_result
93
+
94
+ if mode == "classic":
 
95
  prompt_result = ci.interrogate_classic(image)
96
+ print("mode classic:", prompt_result)
 
 
 
 
 
 
 
 
 
 
97
  return prompt_result
98
 
99
+ prompt_result = ci.interrogate_fast(image)
100
+ print("mode fast:", prompt_result)
101
+ return prompt_result
102
+
103
+
104
  title = """
105
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
106
+ <div style="display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem; margin-bottom: 10px;">
 
 
 
 
 
 
 
 
107
  <h1 style="font-weight: 600; margin-bottom: 7px;">
108
  CLIP Interrogator 2.1
109
  </h1>
110
+ </div>
111
+ <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
112
+ Want to figure out what a good prompt might be to create new images like an existing one?
113
  <br />The CLIP Interrogator is here to get you answers!
114
  <br />This version is specialized for producing nice prompts for use with Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model!
115
+ </p>
116
+ </div>
117
  """
118
 
119
  article = """
120
  <div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
 
121
  <p>
122
+ Server busy? You can also run on
123
+ <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/open-clip/clip_interrogator.ipynb">Google Colab</a>
124
  </p>
125
  <p>
126
+ Has this been helpful to you? Follow Pharma on twitter
127
+ <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
128
+ and check out more tools at his
129
+ <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
130
  </p>
131
  </div>
132
  """
133
 
134
+ css = """
135
+ #col-container {
136
+ max-width: 700px;
137
+ margin-left: auto;
138
+ margin-right: auto;
139
+ }
140
+
141
+ a {
142
+ text-decoration-line: underline;
143
+ font-weight: 600;
144
+ }
145
+ """
146
+
147
 
148
  with gr.Blocks(css=css) as demo:
149
  with gr.Column(elem_id="col-container"):
150
  gr.HTML(title)
151
 
152
+ input_image = gr.Image(type="pil", elem_id="input-img")
153
+
154
  with gr.Row():
155
+ mode_input = gr.Radio(
156
+ ["best", "classic", "fast"],
157
+ label="Select mode",
158
+ value="best",
159
+ )
160
+
161
+ flavor_input = gr.Slider(
162
+ minimum=2,
163
+ maximum=24,
164
+ step=2,
165
+ value=4,
166
+ label="best mode max flavors",
167
+ )
168
+
169
  submit_btn = gr.Button("Submit")
 
 
170
 
171
+ output_text = gr.Textbox(
172
+ label="Description Output",
173
+ elem_id="output-txt",
174
+ )
175
+
176
+ examples = [
177
+ ["27E894C4-9375-48A1-A95D-CB2425416B4B.png", "best", 4],
178
+ ["DB362F56-BA98-4CA1-A999-A25AA94B723B.png", "fast", 4],
179
+ ]
180
+
181
+ gr.Examples(
182
+ examples=examples,
183
+ fn=inference,
184
+ inputs=[input_image, mode_input, flavor_input],
185
+ outputs=[output_text],
186
+ cache_examples=False,
187
+ run_on_click=True,
188
+ )
189
+
190
  gr.HTML(article)
191
 
192
+ submit_btn.click(
193
+ fn=inference,
194
+ inputs=[input_image, mode_input, flavor_input],
195
+ outputs=[output_text],
196
+ api_name="clipi2",
197
+ )
198
+
199
+
200
+ demo.queue(max_size=32).launch(
201
+ footer_links=["api"],
202
+ ssr_mode=False,
203
+ mcp_server=True,
204
+ )