wendys-llc Pharma commited on
Commit
f0ed665
·
0 Parent(s):

Duplicate from pharma/CLIP-Interrogator

Browse files

Co-authored-by: pharmapsychotic <[email protected]>

Files changed (8) hide show
  1. .gitattributes +31 -0
  2. .gitignore +2 -0
  3. README.md +14 -0
  4. app.py +215 -0
  5. example01.jpg +0 -0
  6. example02.jpg +0 -0
  7. requirements.txt +13 -0
  8. share_btn.py +80 -0
.gitattributes ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ftz filter=lfs diff=lfs merge=lfs -text
6
+ *.gz filter=lfs diff=lfs merge=lfs -text
7
+ *.h5 filter=lfs diff=lfs merge=lfs -text
8
+ *.joblib filter=lfs diff=lfs merge=lfs -text
9
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
10
+ *.model filter=lfs diff=lfs merge=lfs -text
11
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
12
+ *.npy filter=lfs diff=lfs merge=lfs -text
13
+ *.npz filter=lfs diff=lfs merge=lfs -text
14
+ *.onnx filter=lfs diff=lfs merge=lfs -text
15
+ *.ot filter=lfs diff=lfs merge=lfs -text
16
+ *.parquet filter=lfs diff=lfs merge=lfs -text
17
+ *.pickle filter=lfs diff=lfs merge=lfs -text
18
+ *.pkl filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pt filter=lfs diff=lfs merge=lfs -text
21
+ *.pth filter=lfs diff=lfs merge=lfs -text
22
+ *.rar filter=lfs diff=lfs merge=lfs -text
23
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
24
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
25
+ *.tflite filter=lfs diff=lfs merge=lfs -text
26
+ *.tgz filter=lfs diff=lfs merge=lfs -text
27
+ *.wasm filter=lfs diff=lfs merge=lfs -text
28
+ *.xz filter=lfs diff=lfs merge=lfs -text
29
+ *.zip filter=lfs diff=lfs merge=lfs -text
30
+ *.zst filter=lfs diff=lfs merge=lfs -text
31
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ cache/
2
+ venv/
README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: CLIP Interrogator
3
+ emoji: 🕵️‍♂️
4
+ colorFrom: yellow
5
+ colorTo: gray
6
+ sdk: gradio
7
+ sdk_version: 3.11.0
8
+ app_file: app.py
9
+ pinned: true
10
+ license: mit
11
+ duplicated_from: pharma/CLIP-Interrogator
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ import gradio as gr
3
+ from clip_interrogator import Config, Interrogator
4
+ from share_btn import community_icon_html, loading_icon_html, share_js
5
+
6
+ MODELS = ['ViT-L (best for Stable Diffusion 1.*)', 'ViT-H (best for Stable Diffusion 2.*)']
7
+
8
+ # load BLIP and ViT-L https://huggingface.co/openai/clip-vit-large-patch14
9
+ config = Config(clip_model_name="ViT-L-14/openai")
10
+ ci_vitl = Interrogator(config)
11
+ ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")
12
+
13
+ # load ViT-H https://huggingface.co/laion/CLIP-ViT-H-14-laion2B-s32B-b79K
14
+ config.blip_model = ci_vitl.blip_model
15
+ config.clip_model_name = "ViT-H-14/laion2b_s32b_b79k"
16
+ ci_vith = Interrogator(config)
17
+ ci_vith.clip_model = ci_vith.clip_model.to("cpu")
18
+
19
+
20
+ def image_analysis(image, clip_model_name):
21
+ # move selected model to GPU and other model to CPU
22
+ if clip_model_name == MODELS[0]:
23
+ ci_vith.clip_model = ci_vith.clip_model.to("cpu")
24
+ ci_vitl.clip_model = ci_vitl.clip_model.to(ci_vitl.device)
25
+ ci = ci_vitl
26
+ else:
27
+ ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")
28
+ ci_vith.clip_model = ci_vith.clip_model.to(ci_vith.device)
29
+ ci = ci_vith
30
+
31
+ image = image.convert('RGB')
32
+ image_features = ci.image_to_features(image)
33
+
34
+ top_mediums = ci.mediums.rank(image_features, 5)
35
+ top_artists = ci.artists.rank(image_features, 5)
36
+ top_movements = ci.movements.rank(image_features, 5)
37
+ top_trendings = ci.trendings.rank(image_features, 5)
38
+ top_flavors = ci.flavors.rank(image_features, 5)
39
+
40
+ medium_ranks = {medium: sim for medium, sim in zip(top_mediums, ci.similarities(image_features, top_mediums))}
41
+ artist_ranks = {artist: sim for artist, sim in zip(top_artists, ci.similarities(image_features, top_artists))}
42
+ movement_ranks = {movement: sim for movement, sim in zip(top_movements, ci.similarities(image_features, top_movements))}
43
+ trending_ranks = {trending: sim for trending, sim in zip(top_trendings, ci.similarities(image_features, top_trendings))}
44
+ flavor_ranks = {flavor: sim for flavor, sim in zip(top_flavors, ci.similarities(image_features, top_flavors))}
45
+
46
+ return medium_ranks, artist_ranks, movement_ranks, trending_ranks, flavor_ranks
47
+
48
+
49
+ def image_to_prompt(image, clip_model_name, mode):
50
+ # move selected model to GPU and other model to CPU
51
+ if clip_model_name == MODELS[0]:
52
+ ci_vith.clip_model = ci_vith.clip_model.to("cpu")
53
+ ci_vitl.clip_model = ci_vitl.clip_model.to(ci_vitl.device)
54
+ ci = ci_vitl
55
+ else:
56
+ ci_vitl.clip_model = ci_vitl.clip_model.to("cpu")
57
+ ci_vith.clip_model = ci_vith.clip_model.to(ci_vith.device)
58
+ ci = ci_vith
59
+
60
+ ci.config.blip_num_beams = 64
61
+ ci.config.chunk_size = 2048
62
+ ci.config.flavor_intermediate_count = 2048 if clip_model_name == MODELS[0] else 1024
63
+
64
+ image = image.convert('RGB')
65
+ if mode == 'best':
66
+ prompt = ci.interrogate(image)
67
+ elif mode == 'classic':
68
+ prompt = ci.interrogate_classic(image)
69
+ elif mode == 'fast':
70
+ prompt = ci.interrogate_fast(image)
71
+ elif mode == 'negative':
72
+ prompt = ci.interrogate_negative(image)
73
+
74
+ return prompt, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
75
+
76
+
77
+ TITLE = """
78
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
79
+ <div
80
+ style="
81
+ display: inline-flex;
82
+ align-items: center;
83
+ gap: 0.8rem;
84
+ font-size: 1.75rem;
85
+ "
86
+ >
87
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
88
+ CLIP Interrogator
89
+ </h1>
90
+ </div>
91
+ <p style="margin-bottom: 10px; font-size: 94%">
92
+ Want to figure out what a good prompt might be to create new images like an existing one?<br>The CLIP Interrogator is here to get you answers!
93
+ </p>
94
+ <p>You can skip the queue by duplicating this space and upgrading to gpu in settings: <a style='display:inline-block' href='https://huggingface.co/spaces/pharma/CLIP-Interrogator?duplicate=true'><img src='https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14' alt='Duplicate Space'></a></p>
95
+ </div>
96
+ """
97
+
98
+ ARTICLE = """
99
+ <div style="text-align: center; max-width: 650px; margin: 0 auto;">
100
+ <p>
101
+ Example art by <a href="https://pixabay.com/illustrations/watercolour-painting-art-effect-4799014/">Layers</a>
102
+ and <a href="https://pixabay.com/illustrations/animal-painting-cat-feline-pet-7154059/">Lin Tong</a>
103
+ from pixabay.com
104
+ </p>
105
+
106
+ <p>
107
+ Server busy? You can also run on <a href="https://colab.research.google.com/github/pharmapsychotic/clip-interrogator/blob/main/clip_interrogator.ipynb">Google Colab</a>
108
+ </p>
109
+
110
+ <p>
111
+ Has this been helpful to you? Follow me on twitter
112
+ <a href="https://twitter.com/pharmapsychotic">@pharmapsychotic</a><br>
113
+ and check out more tools at my
114
+ <a href="https://pharmapsychotic.com/tools.html">Ai generative art tools list</a>
115
+ </p>
116
+ </div>
117
+ """
118
+
119
+ CSS = """
120
+ #col-container {margin-left: auto; margin-right: auto;}
121
+ a {text-decoration-line: underline; font-weight: 600;}
122
+ .animate-spin {
123
+ animation: spin 1s linear infinite;
124
+ }
125
+ @keyframes spin {
126
+ from { transform: rotate(0deg); }
127
+ to { transform: rotate(360deg); }
128
+ }
129
+ #share-btn-container {
130
+ 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;
131
+ }
132
+ #share-btn {
133
+ 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;
134
+ }
135
+ #share-btn * {
136
+ all: unset;
137
+ }
138
+ #share-btn-container div:nth-child(-n+2){
139
+ width: auto !important;
140
+ min-height: 0px !important;
141
+ }
142
+ #share-btn-container .wrap {
143
+ display: none !important;
144
+ }
145
+ """
146
+
147
+ def analyze_tab():
148
+ with gr.Column():
149
+ with gr.Row():
150
+ image = gr.Image(type='pil', label="Image")
151
+ model = gr.Dropdown(MODELS, value=MODELS[0], label='CLIP Model')
152
+ with gr.Row():
153
+ medium = gr.Label(label="Medium", num_top_classes=5)
154
+ artist = gr.Label(label="Artist", num_top_classes=5)
155
+ movement = gr.Label(label="Movement", num_top_classes=5)
156
+ trending = gr.Label(label="Trending", num_top_classes=5)
157
+ flavor = gr.Label(label="Flavor", num_top_classes=5)
158
+
159
+ button = gr.Button("Analyze", api_name="image-analysis")
160
+ button.click(image_analysis, inputs=[image, model], outputs=[medium, artist, movement, trending, flavor])
161
+
162
+ examples=[['example01.jpg', MODELS[0]], ['example02.jpg', MODELS[0]]]
163
+ ex = gr.Examples(
164
+ examples=examples,
165
+ fn=image_analysis,
166
+ inputs=[input_image, input_model],
167
+ outputs=[medium, artist, movement, trending, flavor],
168
+ cache_examples=True,
169
+ run_on_click=True
170
+ )
171
+ ex.dataset.headers = [""]
172
+
173
+
174
+ with gr.Blocks(css=CSS) as block:
175
+ with gr.Column(elem_id="col-container"):
176
+ gr.HTML(TITLE)
177
+
178
+ with gr.Tab("Prompt"):
179
+ with gr.Row():
180
+ input_image = gr.Image(type='pil', elem_id="input-img")
181
+ with gr.Column():
182
+ input_model = gr.Dropdown(MODELS, value=MODELS[0], label='CLIP Model')
183
+ input_mode = gr.Radio(['best', 'fast', 'classic', 'negative'], value='best', label='Mode')
184
+ submit_btn = gr.Button("Submit", api_name="image-to-prompt")
185
+ output_text = gr.Textbox(label="Output", elem_id="output-txt")
186
+
187
+ with gr.Group(elem_id="share-btn-container"):
188
+ community_icon = gr.HTML(community_icon_html, visible=False)
189
+ loading_icon = gr.HTML(loading_icon_html, visible=False)
190
+ share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
191
+
192
+ examples=[['example01.jpg', MODELS[0], 'best'], ['example02.jpg', MODELS[0], 'best']]
193
+ ex = gr.Examples(
194
+ examples=examples,
195
+ fn=image_to_prompt,
196
+ inputs=[input_image, input_model, input_mode],
197
+ outputs=[output_text, share_button, community_icon, loading_icon],
198
+ cache_examples=True,
199
+ run_on_click=True
200
+ )
201
+ ex.dataset.headers = [""]
202
+
203
+ with gr.Tab("Analyze"):
204
+ analyze_tab()
205
+
206
+ gr.HTML(ARTICLE)
207
+
208
+ submit_btn.click(
209
+ fn=image_to_prompt,
210
+ inputs=[input_image, input_model, input_mode],
211
+ outputs=[output_text, share_button, community_icon, loading_icon]
212
+ )
213
+ share_button.click(None, [], [], _js=share_js)
214
+
215
+ block.queue(max_size=64).launch(show_api=False)
example01.jpg ADDED
example02.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cu117
2
+ torch
3
+ torchvision
4
+
5
+ fairscale
6
+ ftfy
7
+ huggingface-hub
8
+ Pillow
9
+ timm
10
+ transformers==4.15.0
11
+ open_clip_torch
12
+ requests
13
+ clip-interrogator==0.5.0
share_btn.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ community_icon_html = """<svg id="share-btn-share-icon" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32">
2
+ <path d="M20.6081 3C21.7684 3 22.8053 3.49196 23.5284 4.38415C23.9756 4.93678 24.4428 5.82749 24.4808 7.16133C24.9674 7.01707 25.4353 6.93643 25.8725 6.93643C26.9833 6.93643 27.9865 7.37587 28.696 8.17411C29.6075 9.19872 30.0124 10.4579 29.8361 11.7177C29.7523 12.3177 29.5581 12.8555 29.2678 13.3534C29.8798 13.8646 30.3306 14.5763 30.5485 15.4322C30.719 16.1032 30.8939 17.5006 29.9808 18.9403C30.0389 19.0342 30.0934 19.1319 30.1442 19.2318C30.6932 20.3074 30.7283 21.5229 30.2439 22.6548C29.5093 24.3704 27.6841 25.7219 24.1397 27.1727C21.9347 28.0753 19.9174 28.6523 19.8994 28.6575C16.9842 29.4379 14.3477 29.8345 12.0653 29.8345C7.87017 29.8345 4.8668 28.508 3.13831 25.8921C0.356375 21.6797 0.754104 17.8269 4.35369 14.1131C6.34591 12.058 7.67023 9.02782 7.94613 8.36275C8.50224 6.39343 9.97271 4.20438 12.4172 4.20438H12.4179C12.6236 4.20438 12.8314 4.2214 13.0364 4.25468C14.107 4.42854 15.0428 5.06476 15.7115 6.02205C16.4331 5.09583 17.134 4.359 17.7682 3.94323C18.7242 3.31737 19.6794 3 20.6081 3ZM20.6081 5.95917C20.2427 5.95917 19.7963 6.1197 19.3039 6.44225C17.7754 7.44319 14.8258 12.6772 13.7458 14.7131C13.3839 15.3952 12.7655 15.6837 12.2086 15.6837C11.1036 15.6837 10.2408 14.5497 12.1076 13.1085C14.9146 10.9402 13.9299 7.39584 12.5898 7.1776C12.5311 7.16799 12.4731 7.16355 12.4172 7.16355C11.1989 7.16355 10.6615 9.33114 10.6615 9.33114C10.6615 9.33114 9.0863 13.4148 6.38031 16.206C3.67434 18.998 3.5346 21.2388 5.50675 24.2246C6.85185 26.2606 9.42666 26.8753 12.0653 26.8753C14.8021 26.8753 17.6077 26.2139 19.1799 25.793C19.2574 25.7723 28.8193 22.984 27.6081 20.6107C27.4046 20.212 27.0693 20.0522 26.6471 20.0522C24.9416 20.0522 21.8393 22.6726 20.5057 22.6726C20.2076 22.6726 19.9976 22.5416 19.9116 22.222C19.3433 20.1173 28.552 19.2325 27.7758 16.1839C27.639 15.6445 27.2677 15.4256 26.746 15.4263C24.4923 15.4263 19.4358 19.5181 18.3759 19.5181C18.2949 19.5181 18.2368 19.4937 18.2053 19.4419C17.6743 18.557 17.9653 17.9394 21.7082 15.6009C25.4511 13.2617 28.0783 11.8545 26.5841 10.1752C26.4121 9.98141 26.1684 9.8956 25.8725 9.8956C23.6001 9.89634 18.2311 14.9403 18.2311 14.9403C18.2311 14.9403 16.7821 16.496 15.9057 16.496C15.7043 16.496 15.533 16.4139 15.4169 16.2112C14.7956 15.1296 21.1879 10.1286 21.5484 8.06535C21.7928 6.66715 21.3771 5.95917 20.6081 5.95917Z" fill="#FF9D00"></path>
3
+ <path d="M5.50686 24.2246C3.53472 21.2387 3.67446 18.9979 6.38043 16.206C9.08641 13.4147 10.6615 9.33111 10.6615 9.33111C10.6615 9.33111 11.2499 6.95933 12.59 7.17757C13.93 7.39581 14.9139 10.9401 12.1069 13.1084C9.29997 15.276 12.6659 16.7489 13.7459 14.713C14.8258 12.6772 17.7747 7.44316 19.304 6.44221C20.8326 5.44128 21.9089 6.00204 21.5484 8.06532C21.188 10.1286 14.795 15.1295 15.4171 16.2118C16.0391 17.2934 18.2312 14.9402 18.2312 14.9402C18.2312 14.9402 25.0907 8.49588 26.5842 10.1752C28.0776 11.8545 25.4512 13.2616 21.7082 15.6008C17.9646 17.9393 17.6744 18.557 18.2054 19.4418C18.7372 20.3266 26.9998 13.1351 27.7759 16.1838C28.5513 19.2324 19.3434 20.1173 19.9117 22.2219C20.48 24.3274 26.3979 18.2382 27.6082 20.6107C28.8193 22.9839 19.2574 25.7722 19.18 25.7929C16.0914 26.62 8.24723 28.3726 5.50686 24.2246Z" fill="#FFD21E"></path>
4
+ </svg>"""
5
+
6
+ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" class="animate-spin"
7
+ style="color: #ffffff;
8
+ "
9
+ xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" fill="none" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 24 24"><circle style="opacity: 0.25;" cx="12" cy="12" r="10" stroke="white" stroke-width="4"></circle><path style="opacity: 0.75;" fill="white" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path></svg>"""
10
+
11
+ share_js = """async () => {
12
+ async function uploadFile(file){
13
+ const UPLOAD_URL = 'https://huggingface.co/uploads';
14
+ const response = await fetch(UPLOAD_URL, {
15
+ method: 'POST',
16
+ headers: {
17
+ 'Content-Type': file.type,
18
+ 'X-Requested-With': 'XMLHttpRequest',
19
+ },
20
+ body: file, /// <- File inherits from Blob
21
+ });
22
+ const url = await response.text();
23
+ return url;
24
+ }
25
+
26
+ async function getInputImgFile(imgEl){
27
+ const res = await fetch(imgEl.src);
28
+ const blob = await res.blob();
29
+ const imgId = Date.now() % 200;
30
+ const isPng = imgEl.src.startsWith(`data:image/png`);
31
+ if(isPng){
32
+ const fileName = `sd-perception-${{imgId}}.png`;
33
+ return new File([blob], fileName, { type: 'image/png' });
34
+ }else{
35
+ const fileName = `sd-perception-${{imgId}}.jpg`;
36
+ return new File([blob], fileName, { type: 'image/jpeg' });
37
+ }
38
+ }
39
+
40
+ const gradioEl = document.querySelector('body > gradio-app');
41
+ // const gradioEl = document.querySelector("gradio-app").shadowRoot;
42
+ const inputImgEl = gradioEl.querySelector('#input-img img');
43
+ const outputTxt = gradioEl.querySelector('#output-txt textarea').value;
44
+ let titleTxt = outputTxt;
45
+ if(titleTxt.length > 100){
46
+ titleTxt = titleTxt.slice(0, 100) + ' ...';
47
+ }
48
+ const shareBtnEl = gradioEl.querySelector('#share-btn');
49
+ const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
50
+ const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
51
+
52
+ if(!outputTxt){
53
+ return;
54
+ };
55
+
56
+ shareBtnEl.style.pointerEvents = 'none';
57
+ shareIconEl.style.display = 'none';
58
+ loadingIconEl.style.removeProperty('display');
59
+
60
+ const inputFile = await getInputImgFile(inputImgEl);
61
+ const urlInputImg = await uploadFile(inputFile);
62
+
63
+ const descriptionMd = `#### Input img:
64
+ <img src='${urlInputImg}' style='max-height: 350px;'>
65
+
66
+ #### Caption:
67
+ ${outputTxt}`;
68
+
69
+ const params = new URLSearchParams({
70
+ title: titleTxt,
71
+ description: descriptionMd,
72
+ });
73
+
74
+ const paramsStr = params.toString();
75
+ window.open(`https://huggingface.co/spaces/pharma/CLIP-Interrogator/discussions/new?${paramsStr}`, '_blank');
76
+
77
+ shareBtnEl.style.removeProperty('pointer-events');
78
+ shareIconEl.style.removeProperty('display');
79
+ loadingIconEl.style.display = 'none';
80
+ }"""