Spaces:
Build error
Build error
share button fixes (#2)
Browse files- share button fixes (921db717ea62a850767e67adb0289b86a17f9a11)
Co-authored-by: Radamés Ajna <radames@users.noreply.huggingface.co>
- app.py +49 -34
- share_btn.py +24 -30
app.py
CHANGED
@@ -1,16 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "False"
|
3 |
-
import numpy as np
|
4 |
-
import torch
|
5 |
-
from PIL import Image
|
6 |
-
import matplotlib.pyplot as plt
|
7 |
-
|
8 |
-
from fromage import models
|
9 |
-
from fromage import utils
|
10 |
-
import gradio as gr
|
11 |
-
import huggingface_hub
|
12 |
-
from share_btn import community_icon_html, loading_icon_html, share_js
|
13 |
-
import tempfile
|
14 |
|
15 |
|
16 |
css = """
|
@@ -18,6 +17,7 @@ css = """
|
|
18 |
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;
|
19 |
margin-top: 10px;
|
20 |
margin-left: auto;
|
|
|
21 |
}
|
22 |
#share-btn {
|
23 |
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;right:0;
|
@@ -35,8 +35,10 @@ css = """
|
|
35 |
"""
|
36 |
|
37 |
# Download model from HF Hub.
|
38 |
-
ckpt_path = huggingface_hub.hf_hub_download(
|
39 |
-
|
|
|
|
|
40 |
model = models.load_fromage('./', args_path, ckpt_path)
|
41 |
|
42 |
|
@@ -44,7 +46,8 @@ def upload_image(state, image_input):
|
|
44 |
conversation = state[0]
|
45 |
chat_history = state[1]
|
46 |
conversation += [(f"![](/file={image_input.name})", "")]
|
47 |
-
input_image = Image.open(image_input.name).resize(
|
|
|
48 |
return [conversation, chat_history, input_image], conversation
|
49 |
|
50 |
|
@@ -86,10 +89,11 @@ def generate_for_prompt(input_text, state, ret_scale_factor, max_nm_rets, num_wo
|
|
86 |
if temperature != 0.0:
|
87 |
top_p = 0.95
|
88 |
|
89 |
-
print('Running model.generate_for_images_and_texts with',
|
90 |
-
|
91 |
-
|
92 |
-
|
|
|
93 |
print('model_outputs', model_outputs, flush=True)
|
94 |
|
95 |
im_names = []
|
@@ -108,13 +112,15 @@ def generate_for_prompt(input_text, state, ret_scale_factor, max_nm_rets, num_wo
|
|
108 |
response += f'<img src="/file={filename}">'
|
109 |
|
110 |
# TODO(jykoh): Persist image inputs.
|
111 |
-
chat_history = model_inputs +
|
112 |
-
|
|
|
|
|
113 |
|
114 |
# Set input image to None.
|
115 |
print('state', state, flush=True)
|
116 |
print('updated state', [conversation, chat_history, None], flush=True)
|
117 |
-
return [conversation, chat_history, None], conversation
|
118 |
|
119 |
|
120 |
with gr.Blocks(css=css) as demo:
|
@@ -128,33 +134,42 @@ with gr.Blocks(css=css) as demo:
|
|
128 |
|
129 |
chatbot = gr.Chatbot(elem_id="chatbot")
|
130 |
gr_state = gr.State([[], [], None]) # chat_history, input_image
|
131 |
-
with gr.
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
135 |
|
136 |
with gr.Row():
|
137 |
with gr.Column(scale=0.3, min_width=100):
|
138 |
-
ret_scale_factor = gr.Slider(minimum=0.0, maximum=3.0, value=1.0, step=0.1, interactive=True,
|
139 |
-
|
140 |
-
|
141 |
-
|
|
|
|
|
|
|
|
|
142 |
|
143 |
with gr.Column(scale=0.7, min_width=400):
|
144 |
image_btn = gr.UploadButton("🖼️ Image Input", file_types=["image"])
|
145 |
-
text_input = gr.Textbox(
|
|
|
146 |
|
147 |
with gr.Row():
|
148 |
with gr.Column(scale=0.33):
|
149 |
-
submit_btn = gr.Button(
|
|
|
150 |
with gr.Column(scale=0.33):
|
151 |
clear_last_btn = gr.Button("Clear Last Round")
|
152 |
with gr.Column(scale=0.33):
|
153 |
clear_btn = gr.Button("Clear All")
|
154 |
|
155 |
-
text_input.submit(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
|
|
|
156 |
text_input.submit(lambda: "", None, text_input) # Reset chatbox.
|
157 |
-
submit_btn.click(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
|
|
|
158 |
submit_btn.click(lambda: "", None, text_input) # Reset chatbox.
|
159 |
|
160 |
image_btn.upload(upload_image, [gr_state, image_btn], [gr_state, chatbot])
|
@@ -164,4 +179,4 @@ with gr.Blocks(css=css) as demo:
|
|
164 |
|
165 |
|
166 |
demo.queue(concurrency_count=1, api_open=False, max_size=16)
|
167 |
-
demo.launch(debug=True, server_name="0.0.0.0")
|
|
|
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
|
6 |
+
from fromage import models
|
7 |
+
import matplotlib.pyplot as plt
|
8 |
+
from PIL import Image
|
9 |
+
import torch
|
10 |
+
import numpy as np
|
11 |
import os
|
12 |
os.environ["HF_HUB_ENABLE_HF_TRANSFER"] = "False"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
css = """
|
|
|
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 |
}
|
22 |
#share-btn {
|
23 |
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;right:0;
|
|
|
35 |
"""
|
36 |
|
37 |
# Download model from HF Hub.
|
38 |
+
ckpt_path = huggingface_hub.hf_hub_download(
|
39 |
+
repo_id='jykoh/fromage', filename='pretrained_ckpt.pth.tar')
|
40 |
+
args_path = huggingface_hub.hf_hub_download(
|
41 |
+
repo_id='jykoh/fromage', filename='model_args.json')
|
42 |
model = models.load_fromage('./', args_path, ckpt_path)
|
43 |
|
44 |
|
|
|
46 |
conversation = state[0]
|
47 |
chat_history = state[1]
|
48 |
conversation += [(f"![](/file={image_input.name})", "")]
|
49 |
+
input_image = Image.open(image_input.name).resize(
|
50 |
+
(224, 224)).convert('RGB')
|
51 |
return [conversation, chat_history, input_image], conversation
|
52 |
|
53 |
|
|
|
89 |
if temperature != 0.0:
|
90 |
top_p = 0.95
|
91 |
|
92 |
+
print('Running model.generate_for_images_and_texts with',
|
93 |
+
model_inputs, flush=True)
|
94 |
+
model_outputs = model.generate_for_images_and_texts(model_inputs,
|
95 |
+
num_words=max(num_words, 1), ret_scale_factor=ret_scale_factor, top_p=top_p,
|
96 |
+
temperature=temperature, max_num_rets=max_nm_rets)
|
97 |
print('model_outputs', model_outputs, flush=True)
|
98 |
|
99 |
im_names = []
|
|
|
112 |
response += f'<img src="/file={filename}">'
|
113 |
|
114 |
# TODO(jykoh): Persist image inputs.
|
115 |
+
chat_history = model_inputs + \
|
116 |
+
[' '.join([s for s in model_outputs if type(s) == str]) + '\n']
|
117 |
+
# Remove [RET] from outputs.
|
118 |
+
conversation.append((input_text, response.replace('[RET]', '')))
|
119 |
|
120 |
# Set input image to None.
|
121 |
print('state', state, flush=True)
|
122 |
print('updated state', [conversation, chat_history, None], flush=True)
|
123 |
+
return [conversation, chat_history, None], conversation, gr.update(visible=True)
|
124 |
|
125 |
|
126 |
with gr.Blocks(css=css) as demo:
|
|
|
134 |
|
135 |
chatbot = gr.Chatbot(elem_id="chatbot")
|
136 |
gr_state = gr.State([[], [], None]) # chat_history, input_image
|
137 |
+
with gr.Row(visible=False) as share_group:
|
138 |
+
with gr.Group(elem_id="share-btn-container"):
|
139 |
+
community_icon = gr.HTML(community_icon_html)
|
140 |
+
loading_icon = gr.HTML(loading_icon_html)
|
141 |
+
share_button = gr.Button("Share to community", elem_id="share-btn")
|
142 |
|
143 |
with gr.Row():
|
144 |
with gr.Column(scale=0.3, min_width=100):
|
145 |
+
ret_scale_factor = gr.Slider(minimum=0.0, maximum=3.0, value=1.0, step=0.1, interactive=True,
|
146 |
+
label="Multiplier for returning images (higher means more frequent)")
|
147 |
+
max_ret_images = gr.Number(
|
148 |
+
minimum=0, maximum=3, value=1, precision=1, interactive=True, label="Max images to return")
|
149 |
+
gr_max_len = gr.Slider(minimum=1, maximum=64, value=32,
|
150 |
+
step=1, interactive=True, label="Max # of words returned")
|
151 |
+
gr_temperature = gr.Slider(
|
152 |
+
minimum=0.0, maximum=1.0, value=0.0, interactive=True, label="Temperature")
|
153 |
|
154 |
with gr.Column(scale=0.7, min_width=400):
|
155 |
image_btn = gr.UploadButton("🖼️ Image Input", file_types=["image"])
|
156 |
+
text_input = gr.Textbox(
|
157 |
+
label="Chat Input", lines=1, placeholder="Upload an image above [optional]. Then enter a text prompt, and press enter!")
|
158 |
|
159 |
with gr.Row():
|
160 |
with gr.Column(scale=0.33):
|
161 |
+
submit_btn = gr.Button(
|
162 |
+
"Submit", interactive=True, variant="primary")
|
163 |
with gr.Column(scale=0.33):
|
164 |
clear_last_btn = gr.Button("Clear Last Round")
|
165 |
with gr.Column(scale=0.33):
|
166 |
clear_btn = gr.Button("Clear All")
|
167 |
|
168 |
+
text_input.submit(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
|
169 |
+
max_ret_images, gr_max_len, gr_temperature], [gr_state, chatbot, share_group])
|
170 |
text_input.submit(lambda: "", None, text_input) # Reset chatbox.
|
171 |
+
submit_btn.click(generate_for_prompt, [text_input, gr_state, ret_scale_factor,
|
172 |
+
max_ret_images, gr_max_len, gr_temperature], [gr_state, chatbot, share_group])
|
173 |
submit_btn.click(lambda: "", None, text_input) # Reset chatbox.
|
174 |
|
175 |
image_btn.upload(upload_image, [gr_state, image_btn], [gr_state, chatbot])
|
|
|
179 |
|
180 |
|
181 |
demo.queue(concurrency_count=1, api_open=False, max_size=16)
|
182 |
+
demo.launch(debug=True, server_name="0.0.0.0")
|
share_btn.py
CHANGED
@@ -12,7 +12,9 @@ loading_icon_html = """<svg id="share-btn-loading-icon" style="display:none;" cl
|
|
12 |
|
13 |
share_js = """
|
14 |
async () => {
|
15 |
-
|
|
|
|
|
16 |
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
17 |
const response = await fetch(UPLOAD_URL, {
|
18 |
method: 'POST',
|
@@ -25,53 +27,45 @@ async () => {
|
|
25 |
const url = await response.text();
|
26 |
return url;
|
27 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
function downloadURI(uri, name) {
|
31 |
-
var link = document.createElement("a");
|
32 |
-
link.download = name;
|
33 |
-
link.href = uri;
|
34 |
-
link.click();
|
35 |
-
}
|
36 |
-
|
37 |
-
//Your modified code.
|
38 |
-
function printToFile(div) {
|
39 |
-
html2canvas(div, {
|
40 |
-
onrendered: function (canvas) {
|
41 |
-
var myImage = canvas.toDataURL("image/png");
|
42 |
-
downloadURI("data:" + myImage, "fromage_chat.png");
|
43 |
-
}
|
44 |
-
});
|
45 |
}
|
46 |
|
47 |
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
|
48 |
const chatbotEl = gradioEl.querySelector('#chatbot')
|
49 |
-
|
50 |
-
const
|
51 |
-
|
52 |
-
|
53 |
-
const urlChatbotImage = "placeholder";
|
54 |
-
|
55 |
-
let titleTxt = `FROMAGe: ${inputPromptEl}`;
|
56 |
|
57 |
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
58 |
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
59 |
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
60 |
shareBtnEl.style.pointerEvents = 'none';
|
61 |
shareIconEl.style.display = 'none';
|
62 |
-
loadingIconEl.style.removeProperty('display');
|
63 |
const descriptionMd = `
|
64 |
-
## ${inputPromptEl}
|
65 |
|
66 |
-
${urlChatbotImage}
|
67 |
`;
|
68 |
const params = new URLSearchParams({
|
69 |
title: titleTxt,
|
70 |
description: descriptionMd,
|
71 |
});
|
72 |
-
|
73 |
-
|
74 |
shareBtnEl.style.removeProperty('pointer-events');
|
75 |
shareIconEl.style.removeProperty('display');
|
76 |
loadingIconEl.style.display = 'none';
|
77 |
-
}
|
|
|
|
12 |
|
13 |
share_js = """
|
14 |
async () => {
|
15 |
+
const html2canvas = (await import('https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.esm.js')).default;
|
16 |
+
async function uploadFile(file) {
|
17 |
+
console.log(file.type)
|
18 |
const UPLOAD_URL = 'https://huggingface.co/uploads';
|
19 |
const response = await fetch(UPLOAD_URL, {
|
20 |
method: 'POST',
|
|
|
27 |
const url = await response.text();
|
28 |
return url;
|
29 |
}
|
30 |
+
async function getImageFile(div) {
|
31 |
+
return new Promise((resolve, reject) =>
|
32 |
+
html2canvas(div)
|
33 |
+
.then((canvas) => {
|
34 |
+
const imageBlob = canvas.toBlob((blob) => {
|
35 |
+
const imageId = Date.now();
|
36 |
+
const fileName = "FROMAGe-" + imageId + ".jpg";
|
37 |
+
resolve(new File([blob], fileName, { type: 'image/jpeg' }));
|
38 |
+
}, 'image/jpeg', 0.95);
|
39 |
+
})
|
40 |
|
41 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
}
|
43 |
|
44 |
const gradioEl = document.querySelector("gradio-app").shadowRoot || document.querySelector('body > gradio-app');
|
45 |
const chatbotEl = gradioEl.querySelector('#chatbot')
|
46 |
+
const imageFile = await getImageFile(chatbotEl);
|
47 |
+
const urlChatbotImage = await uploadFile(imageFile);
|
48 |
+
console.log(urlChatbotImage);
|
49 |
+
let titleTxt = `FROMAGe`;
|
|
|
|
|
|
|
50 |
|
51 |
const shareBtnEl = gradioEl.querySelector('#share-btn');
|
52 |
const shareIconEl = gradioEl.querySelector('#share-btn-share-icon');
|
53 |
const loadingIconEl = gradioEl.querySelector('#share-btn-loading-icon');
|
54 |
shareBtnEl.style.pointerEvents = 'none';
|
55 |
shareIconEl.style.display = 'none';
|
56 |
+
loadingIconEl.style.removeProperty('display');
|
57 |
const descriptionMd = `
|
|
|
58 |
|
59 |
+
<img src='${urlChatbotImage}'>
|
60 |
`;
|
61 |
const params = new URLSearchParams({
|
62 |
title: titleTxt,
|
63 |
description: descriptionMd,
|
64 |
});
|
65 |
+
const paramsStr = params.toString();
|
66 |
+
window.open(`https://huggingface.co/spaces/jykoh/fromage/discussions/new?${paramsStr}`, '_blank');
|
67 |
shareBtnEl.style.removeProperty('pointer-events');
|
68 |
shareIconEl.style.removeProperty('display');
|
69 |
loadingIconEl.style.display = 'none';
|
70 |
+
}
|
71 |
+
"""
|