Ashrafb commited on
Commit
99b150f
·
1 Parent(s): e8c3351

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -34
app.py CHANGED
@@ -1,32 +1,8 @@
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')
@@ -36,6 +12,7 @@ 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
@@ -57,8 +34,10 @@ def inference(input_images, mode, best_max_flavors):
57
  else:
58
  prompt_result = ci.interrogate_fast(image)
59
  prompt_results.append((image, prompt_result)) # Use dictionary to set image labels
60
- return prompt_results
61
-
 
 
62
 
63
  title = """
64
  <div style="text-align: center; max-width: 500px; margin: 0 auto;">
@@ -162,16 +141,13 @@ with gr.Blocks(css=css) as block:
162
 
163
  submit_btn = gr.Button("Submit")
164
 
165
- # rows, cols = NUM_IMAGES //3,
166
- gallery = gr.Gallery(
167
- label="Outputs", show_label=True, elem_id="gallery", object_fit="contain", height="auto"
168
- )
169
 
170
  with gr.Group(elem_id="share-btn-container"):
171
  loading_icon = gr.HTML(loading_icon_html, visible=False)
172
 
173
  gr.HTML(article)
174
- submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[gallery], api_name="clipi2")
175
-
176
 
177
- 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
  import os, subprocess
3
  import torch
4
 
5
+ # (The setup function and cache download section has been commented out)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  import sys
8
  sys.path.append('src/blip')
 
12
  from clip_interrogator import Config, Interrogator
13
  import io
14
  from PIL import Image
15
+
16
  config = Config()
17
  config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
18
  config.blip_offload = False if torch.cuda.is_available() else True
 
34
  else:
35
  prompt_result = ci.interrogate_fast(image)
36
  prompt_results.append((image, prompt_result)) # Use dictionary to set image labels
37
+
38
+ # Convert prompt_results to text format
39
+ text_results = [f"Image {i+1}: {result[1]}" for i, result in enumerate(prompt_results)]
40
+ return "\n".join(text_results)
41
 
42
  title = """
43
  <div style="text-align: center; max-width: 500px; margin: 0 auto;">
 
141
 
142
  submit_btn = gr.Button("Submit")
143
 
144
+ # Change from Gallery to Textbox for displaying results
145
+ result_textbox = gr.Textbox(label="Outputs", type="str", readonly=True, elem_id="output-textbox")
 
 
146
 
147
  with gr.Group(elem_id="share-btn-container"):
148
  loading_icon = gr.HTML(loading_icon_html, visible=False)
149
 
150
  gr.HTML(article)
151
+ submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[result_textbox], api_name="clipi2")
 
152
 
153
+ block.queue(max_size=32,concurrency_count=10).launch(show_api=False)