ArcAhmedEssam commited on
Commit
25d1299
·
1 Parent(s): 17e3aa9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -38
app.py CHANGED
@@ -34,7 +34,8 @@ sys.path.append('clip-interrogator')
34
 
35
  import gradio as gr
36
  from clip_interrogator import Config, Interrogator
37
-
 
38
  config = Config()
39
  config.device = 'cuda' if torch.cuda.is_available() else 'cpu'
40
  config.blip_offload = False if torch.cuda.is_available() else True
@@ -44,31 +45,20 @@ config.blip_num_beams = 64
44
 
45
  ci = Interrogator(config)
46
 
47
- def inference(image, mode, best_max_flavors):
48
- image = image.convert('RGB')
49
- if mode == 'best':
50
-
51
- prompt_result = ci.interrogate(image, max_flavors=int(best_max_flavors))
52
-
53
- print("mode best: " + prompt_result)
54
-
55
- return prompt_result, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
56
-
57
- elif mode == 'classic':
58
-
59
- prompt_result = ci.interrogate_classic(image)
60
-
61
- print("mode classic: " + prompt_result)
62
-
63
- return prompt_result, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
64
-
65
- else:
66
-
67
- prompt_result = ci.interrogate_fast(image)
68
-
69
- print("mode fast: " + prompt_result)
70
-
71
- return prompt_result, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
72
 
73
  title = """
74
  <div style="text-align: center; max-width: 500px; margin: 0 auto;">
@@ -109,7 +99,7 @@ article = """
109
  """
110
 
111
  css = '''
112
- #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
113
  a {text-decoration-line: underline; font-weight: 600;}
114
  .animate-spin {
115
  animation: spin 1s linear infinite;
@@ -144,27 +134,24 @@ with gr.Blocks(css=css) as block:
144
  with gr.Column(elem_id="col-container"):
145
  gr.HTML(title)
146
 
147
- input_image = gr.Image(type='pil', elem_id="input-img")
 
148
  with gr.Row():
149
  mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
150
  flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='best mode max flavors')
151
 
152
  submit_btn = gr.Button("Submit")
153
 
154
- output_text = gr.Textbox(label="Description Output", elem_id="output-txt")
155
-
 
 
 
156
  with gr.Group(elem_id="share-btn-container"):
157
- community_icon = gr.HTML(community_icon_html, visible=False)
158
  loading_icon = gr.HTML(loading_icon_html, visible=False)
159
- share_button = gr.Button("Share to community", elem_id="share-btn", visible=False)
160
 
161
- examples=[['27E894C4-9375-48A1-A95D-CB2425416B4B.png', "best",4], ['DB362F56-BA98-4CA1-A999-A25AA94B723B.png',"fast",4]]
162
- ex = gr.Examples(examples=examples, fn=inference, inputs=[input_image, mode_input, flavor_input], outputs=[output_text, share_button, community_icon, loading_icon], cache_examples=True, run_on_click=True)
163
- ex.dataset.headers = [""]
164
-
165
  gr.HTML(article)
 
166
 
167
- submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[output_text, share_button, community_icon, loading_icon], api_name="clipi2")
168
- share_button.click(None, [], [], _js=share_js)
169
 
170
  block.queue(max_size=32,concurrency_count=10).launch(show_api=False)
 
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((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;">
 
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;
 
134
  with gr.Column(elem_id="col-container"):
135
  gr.HTML(title)
136
 
137
+ input_image = gr.inputs.File(file_count="multiple", type='bytes')
138
+ # NUM_IMAGES = len(input_image)
139
  with gr.Row():
140
  mode_input = gr.Radio(['best', 'classic', 'fast'], label='Select mode', value='best')
141
  flavor_input = gr.Slider(minimum=2, maximum=24, step=2, value=4, label='best mode max flavors')
142
 
143
  submit_btn = gr.Button("Submit")
144
 
145
+ # rows, cols = NUM_IMAGES //3,
146
+ gallery = gr.Gallery(
147
+ label="Images", show_label=True, elem_id="gallery"
148
+ ).style( object_fit="contain", height="auto")
149
+
150
  with gr.Group(elem_id="share-btn-container"):
 
151
  loading_icon = gr.HTML(loading_icon_html, visible=False)
 
152
 
 
 
 
 
153
  gr.HTML(article)
154
+ submit_btn.click(fn=inference, inputs=[input_image,mode_input,flavor_input], outputs=[gallery], api_name="clipi2")
155
 
 
 
156
 
157
  block.queue(max_size=32,concurrency_count=10).launch(show_api=False)