Ashrafb commited on
Commit
30b36ce
·
1 Parent(s): b2fcee5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +165 -25
app.py CHANGED
@@ -1,9 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
  from clip_interrogator import Config, Interrogator
3
- import torch
4
  from PIL import Image
5
- import io
6
-
7
  config = Config()
8
  config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
9
  config.blip_offload = False if torch.cuda.is_available() else True
@@ -13,31 +45,139 @@ config.blip_num_beams = 64
13
 
14
  ci = Interrogator(config)
15
 
16
- def inference(input_image, mode, best_max_flavors):
17
- image_bytes = input_image.read()
18
- image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- if mode == 'best':
21
- prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
22
- elif mode == 'classic':
23
- prompt_result = ci.interrogate_classic(image)
24
- else:
25
- prompt_result = ci.interrogate_fast(image)
26
-
27
- return prompt_result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  with gr.Blocks() as block:
30
  with gr.Column(elem_id="col-container"):
31
- input_image = gr.Image(label="Upload Image") # Using gr.Image for image upload
32
- mode_input = gr.Radio(['classic', 'fast', 'best'], label='Select mode', default='fast')
33
- flavor_input = gr.Slider(minimum=2, maximum=24, step=2, default=4, label='Best mode max flavors')
34
- submit_btn = gr.Button("Submit")
35
- output_text = gr.Textbox(label="Output", elem_id="output-txt") # Output Textbox
36
-
37
- def process_image():
38
- prompt_result = inference(input_image, mode_input, flavor_input)
39
- output_text = prompt_result
40
 
41
- submit_btn.click(process_image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- block.queue(max_size=32, concurrency_count=10).launch(show_api=False)
 
 
1
+ from share_btn import community_icon_html, loading_icon_html, share_js
2
+
3
+ import os, subprocess
4
+ import torch
5
+
6
+ # def setup():
7
+ # install_cmds = [
8
+ # ['pip', 'install', 'ftfy', 'gradio', 'regex', 'tqdm', 'transformers==4.21.2', 'timm', 'fairscale', 'requests'],
9
+ # ['pip', 'install', 'open_clip_torch'],
10
+ # ['pip', 'install', '-e', 'git+https://github.com/pharmapsychotic/BLIP.git@lib#egg=blip'],
11
+ # ['git', 'clone', '-b', 'open-clip', 'https://github.com/pharmapsychotic/clip-interrogator.git']
12
+ # ]
13
+ # for cmd in install_cmds:
14
+ # print(subprocess.run(cmd, stdout=subprocess.PIPE).stdout.decode('utf-8'))
15
+
16
+ # setup()
17
+
18
+ # download cache files
19
+ # print("Download preprocessed cache files...")
20
+ # CACHE_URLS = [
21
+ # 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_artists.pkl',
22
+ # 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_flavors.pkl',
23
+ # 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_mediums.pkl',
24
+ # 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_movements.pkl',
25
+ # 'https://huggingface.co/pharma/ci-preprocess/resolve/main/ViT-H-14_laion2b_s32b_b79k_trendings.pkl',
26
+ # ]
27
+ # os.makedirs('cache', exist_ok=True)
28
+ # for url in CACHE_URLS:
29
+ # print(subprocess.run(['wget', url, '-P', 'cache'], stdout=subprocess.PIPE).stdout.decode('utf-8'))
30
+
31
+ import sys
32
+ sys.path.append('src/blip')
33
+ sys.path.append('clip-interrogator')
34
+
35
  import gradio as gr
36
  from clip_interrogator import Config, Interrogator
37
+ import io
38
  from PIL import Image
 
 
39
  config = Config()
40
  config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
41
  config.blip_offload = False if torch.cuda.is_available() else True
 
45
 
46
  ci = Interrogator(config)
47
 
48
+ def inference(input_images, mode, best_max_flavors):
49
+ # Process each image in the list and generate prompt results
50
+ prompt_results = []
51
+ for image_bytes in input_images:
52
+ image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
53
+ if mode == 'best':
54
+ prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
55
+ elif mode == 'classic':
56
+ prompt_result = ci.interrogate_classic(image)
57
+ else:
58
+ prompt_result = ci.interrogate_fast(image)
59
+ prompt_results.append(prompt_result) # Append text prompt result
60
+ return prompt_results
61
+
62
+
63
+ title = """
64
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;">
65
+ <div
66
+ style="
67
+ display: inline-flex;
68
+ align-items: center;
69
+ gap: 0.8rem;
70
+ font-size: 1.75rem;
71
+ margin-bottom: 10px;
72
+ "
73
+ >
74
+ <h1 style="font-weight: 600; margin-bottom: 7px;">
75
+ CLIP Interrogator 2.1
76
+ </h1>
77
+ </div>
78
+ <p style="margin-bottom: 10px;font-size: 94%;font-weight: 100;line-height: 1.5em;">
79
+ Want to figure out what a good prompt might be to create new images like an existing one?
80
+ <br />The CLIP Interrogator is here to get you answers!
81
+ <br />This version is specialized for producing nice prompts for use with Stable Diffusion 2.0 using the ViT-H-14 OpenCLIP model!
82
+ </p>
83
+ </div>
84
+ """
85
+
86
+ article = """
87
+ <div style="text-align: center; max-width: 500px; margin: 0 auto;font-size: 94%;">
88
 
89
+ <p>
90
+ 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>
91
+ </p>
92
+ <p>
93
+ Has this been helpful to you? Follow Pharma on twitter
94
+ <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a>
95
+ and check out more tools at his
96
+ <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
97
+ </p>
98
+ </div>
99
+ """
100
+
101
+ css = '''
102
+ #col-container {width: width: 80%;; margin-left: auto; margin-right: auto;}
103
+ a {text-decoration-line: underline; font-weight: 600;}
104
+ .animate-spin {
105
+ animation: spin 1s linear infinite;
106
+ }
107
+ @keyframes spin {
108
+ from {
109
+ transform: rotate(0deg);
110
+ }
111
+ to {
112
+ transform: rotate(360deg);
113
+ }
114
+ }
115
+ #share-btn-container {
116
+ display: flex; padding-left: 0.5rem !important; padding-right: 0.5rem !important; background-color: #000000; justify-content: center; align-items: center; border-radius: 9999px !important; width: 13rem;
117
+ }
118
+ #share-btn {
119
+ all: initial; color: #ffffff;font-weight: 600; cursor:pointer; font-family: 'IBM Plex Sans', sans-serif; margin-left: 0.5rem !important; padding-top: 0.25rem !important; padding-bottom: 0.25rem !important;
120
+ }
121
+ #share-btn * {
122
+ all: unset;
123
+ }
124
+ #share-btn-container div:nth-child(-n+2){
125
+ width: auto !important;
126
+ min-height: 0px !important;
127
+ }
128
+ #share-btn-container .wrap {
129
+ display: none !important;
130
+ }
131
+ #gallery .caption-label {
132
+ font-size: 15px !important;
133
+ right: 0 !important;
134
+ max-width: 100% !important;
135
+ text-overflow: clip !important;
136
+ white-space: normal !important;
137
+ overflow: auto !important;
138
+ height: 20% !important;
139
+ }
140
+
141
+ #gallery .caption {
142
+ padding: var(--size-2) var(--size-3) !important;
143
+ text-overflow: clip !important;
144
+ white-space: normal !important; /* Allows the text to wrap */
145
+ color: var(--block-label-text-color) !important;
146
+ font-weight: var(--weight-semibold) !important;
147
+ text-align: center !important;
148
+ height: 100% !important;
149
+ font-size: 17px !important;
150
+ }
151
+
152
+ '''
153
 
154
  with gr.Blocks() as block:
155
  with gr.Column(elem_id="col-container"):
156
+ gr.HTML(title)
157
+
 
 
 
 
 
 
 
158
 
159
+ input_images = gr.Files(label="Inputs", file_count="multiple", type='file', elem_id='inputs')
160
+
161
+ # Create a function to display the uploaded images
162
+ def display_uploaded_images(input_images):
163
+ image_display = [] # List to store images for display
164
+
165
+ for image_file in input_images:
166
+ # Get the path or value of the uploaded image
167
+ image_path = image_file.name if isinstance(image_file, gr.File) else None
168
+ image_value = image_file.getvalue() if isinstance(image_file, gr.File) else None
169
+
170
+ if image_path:
171
+ # If using image path
172
+ image_display.append(gr.Image(image_path))
173
+ elif image_value:
174
+ # If using image value
175
+ image_display.append(gr.Image(image_bytes=image_value))
176
+
177
+ return image_display
178
+
179
+ # Display the uploaded images using gr.image
180
+ uploaded_images_display = display_uploaded_images(input_images)
181
 
182
+ # Show the uploaded images in the Gradio interface
183
+ gr.Interface(fn=inference, inputs=input_images, outputs=output_text, title="CLIP Interrogator 2.1", layout="vertical", examples=None).launch()