jykoh commited on
Commit
363ce98
1 Parent(s): 777823b

UI changes

Browse files
Files changed (3) hide show
  1. app.py +28 -11
  2. fromage/models.py +1 -1
  3. share_btn.py +41 -0
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import tempfile
2
- from share_btn import community_icon_html, loading_icon_html, share_js
3
  import huggingface_hub
4
  import gradio as gr
5
  from fromage import utils
@@ -15,7 +15,7 @@ os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "False"
15
  css = """
16
  #share-btn-container {
17
  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;
18
- margin-top: 10px;
19
  margin-left: auto;
20
  flex: unset;
21
  }
@@ -33,6 +33,22 @@ css = """
33
  display: none !important;
34
  }
35
  #chatbot { min-height: 300px; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  """
37
 
38
  examples = [
@@ -133,7 +149,7 @@ def generate_for_prompt(input_text, state, ret_scale_factor, max_num_rets, num_w
133
  # Set input image to None.
134
  print('state', state, flush=True)
135
  print('updated state', [conversation, chat_history, None], flush=True)
136
- return [conversation, chat_history, None], conversation, gr.update(visible=True)
137
 
138
 
139
  with gr.Blocks(css=css) as demo:
@@ -161,11 +177,11 @@ with gr.Blocks(css=css) as demo:
161
  "Submit", interactive=True, variant="primary")
162
  clear_last_btn = gr.Button("Undo")
163
  clear_btn = gr.Button("Reset All")
 
 
 
164
  with gr.Row(visible=False) as share_group:
165
- with gr.Group(elem_id="share-btn-container"):
166
- community_icon = gr.HTML(community_icon_html)
167
- loading_icon = gr.HTML(loading_icon_html)
168
- share_button = gr.Button("Share to community", elem_id="share-btn")
169
 
170
 
171
  with gr.Column(scale=0.3, min_width=200):
@@ -183,17 +199,18 @@ with gr.Blocks(css=css) as demo:
183
  # ).style(grid=[2], height="auto")
184
 
185
  text_input.submit(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
186
- max_ret_images, gr_max_len, gr_temperature], [gr_state, chatbot, share_group])
187
  text_input.submit(lambda: "", None, text_input) # Reset chatbox.
188
  submit_btn.click(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
189
- max_ret_images, gr_max_len, gr_temperature], [gr_state, chatbot, share_group])
190
  submit_btn.click(lambda: "", None, text_input) # Reset chatbox.
191
 
192
  image_btn.upload(upload_image, [gr_state, image_btn], [gr_state, chatbot])
193
  clear_last_btn.click(reset_last, [gr_state], [gr_state, chatbot])
194
  clear_btn.click(reset, [], [gr_state, chatbot])
195
  share_button.click(None, [], [], _js=share_js)
 
196
 
197
 
198
- # demo.queue(concurrency_count=1, api_open=False, max_size=16)
199
- demo.launch(debug=True, server_name="127.0.0.1")
 
1
  import tempfile
2
+ from share_btn import community_icon_html, loading_icon_html, share_js, save_js
3
  import huggingface_hub
4
  import gradio as gr
5
  from fromage import utils
 
15
  css = """
16
  #share-btn-container {
17
  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;
18
+ margin-top: 3px;
19
  margin-left: auto;
20
  flex: unset;
21
  }
 
33
  display: none !important;
34
  }
35
  #chatbot { min-height: 300px; }
36
+ #save-btn {
37
+ background-image: linear-gradient(to right bottom, rgba(130,217,244, 0.9), rgba(158,231,214, 1.0));
38
+ }
39
+ #save-btn:hover {
40
+ background-image: linear-gradient(to right bottom, rgba(130,217,244, 0.9), rgba(158,231,214, 1.0));
41
+ }
42
+ #share-btn-2 {
43
+ background-image: linear-gradient(to right bottom, rgba(130,217,244, 0.9), rgba(158,231,214, 1.0));
44
+ }
45
+ #share-btn-2:hover {
46
+ background-image: linear-gradient(to right bottom, rgba(130,217,244, 0.9), rgba(158,231,214, 1.0));
47
+ }
48
+ .message .user {
49
+ }
50
+ .message .bot {
51
+ }
52
  """
53
 
54
  examples = [
 
149
  # Set input image to None.
150
  print('state', state, flush=True)
151
  print('updated state', [conversation, chat_history, None], flush=True)
152
+ return [conversation, chat_history, None], conversation, gr.update(visible=True), gr.update(visible=True)
153
 
154
 
155
  with gr.Blocks(css=css) as demo:
 
177
  "Submit", interactive=True, variant="primary")
178
  clear_last_btn = gr.Button("Undo")
179
  clear_btn = gr.Button("Reset All")
180
+ with gr.Row(visible=False) as save_group:
181
+ save_button = gr.Button("💾 Save Conversation as .png", elem_id="save-btn")
182
+
183
  with gr.Row(visible=False) as share_group:
184
+ share_button = gr.Button("🤗 Share to Community", elem_id="share-btn-2")
 
 
 
185
 
186
 
187
  with gr.Column(scale=0.3, min_width=200):
 
199
  # ).style(grid=[2], height="auto")
200
 
201
  text_input.submit(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
202
+ max_ret_images, gr_max_len, gr_temperature], [gr_state, chatbot, share_group, save_group])
203
  text_input.submit(lambda: "", None, text_input) # Reset chatbox.
204
  submit_btn.click(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
205
+ max_ret_images, gr_max_len, gr_temperature], [gr_state, chatbot, share_group, save_group])
206
  submit_btn.click(lambda: "", None, text_input) # Reset chatbox.
207
 
208
  image_btn.upload(upload_image, [gr_state, image_btn], [gr_state, chatbot])
209
  clear_last_btn.click(reset_last, [gr_state], [gr_state, chatbot])
210
  clear_btn.click(reset, [], [gr_state, chatbot])
211
  share_button.click(None, [], [], _js=share_js)
212
+ save_button.click(None, [], [], _js=save_js)
213
 
214
 
215
+ demo.queue(concurrency_count=1, api_open=False, max_size=16)
216
+ demo.launch(debug=True, server_name="0.0.0.0")
fromage/models.py CHANGED
@@ -635,7 +635,7 @@ def load_fromage(embeddings_dir: str, model_args_path: str, model_ckpt_path: str
635
  assert len(ret_token_idx) == 1, ret_token_idx
636
  model_kwargs['retrieval_token_idx'] = ret_token_idx[0]
637
 
638
- debug = True
639
  if debug:
640
  model_kwargs['opt_version'] = 'facebook/opt-125m'
641
  model_kwargs['visual_encoder'] = 'openai/clip-vit-base-patch32'
 
635
  assert len(ret_token_idx) == 1, ret_token_idx
636
  model_kwargs['retrieval_token_idx'] = ret_token_idx[0]
637
 
638
+ debug = False
639
  if debug:
640
  model_kwargs['opt_version'] = 'facebook/opt-125m'
641
  model_kwargs['visual_encoder'] = 'openai/clip-vit-base-patch32'
share_btn.py CHANGED
@@ -68,4 +68,45 @@ async () => {
68
  shareIconEl.style.removeProperty('display');
69
  loadingIconEl.style.display = 'none';
70
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  """
 
68
  shareIconEl.style.removeProperty('display');
69
  loadingIconEl.style.display = 'none';
70
  }
71
+ """
72
+
73
+ save_js = """
74
+ async () => {
75
+ const html2canvas = (await import('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.esm.js')).default;
76
+
77
+ function saveAs(uri, filename) {
78
+ var link = document.createElement('a');
79
+ if (typeof link.download === 'string') {
80
+ link.href = uri;
81
+ link.download = filename;
82
+
83
+ //Firefox requires the link to be in the body
84
+ document.body.appendChild(link);
85
+
86
+ //simulate click
87
+ link.click();
88
+
89
+ //remove the link when done
90
+ document.body.removeChild(link);
91
+ } else {
92
+ window.open(uri);
93
+ }
94
+ }
95
+
96
+ async function getImageFile(div) {
97
+ return new Promise((resolve, reject) =>
98
+ html2canvas(div)
99
+ .then((canvas) => {
100
+ const imageId = Date.now();
101
+ const fileName = "FROMAGe-" + imageId + ".jpg";
102
+ saveAs(canvas.toDataURL(), fileName);
103
+ })
104
+
105
+ )
106
+ }
107
+ const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
108
+ const chatbotEl = gradioEl.querySelector('#chatbot')
109
+ const imageFile = await getImageFile(chatbotEl);
110
+ console.log(imageFile);
111
+ }
112
  """