Spaces:
Running
Running
Commit
·
5863d22
1
Parent(s):
d3c2ede
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,27 +12,8 @@ def getTextTrans(text, source='zh', target='en'):
|
|
| 12 |
except Exception as e:
|
| 13 |
return text
|
| 14 |
|
| 15 |
-
extend_prompt_pipe = pipeline('text-generation', model='yizhangliu/prompt-extend', max_length=77)
|
| 16 |
|
| 17 |
-
def extend_prompt(prompt):
|
| 18 |
-
prompt_en = getTextTrans(prompt, source='zh', target='en')
|
| 19 |
-
extend_prompt_en = extend_prompt_pipe(prompt_en+',', num_return_sequences=1)[0]["generated_text"]
|
| 20 |
-
if (prompt != prompt_en):
|
| 21 |
-
logger.info(f"extend_prompt__1__")
|
| 22 |
-
extend_prompt_zh = getTextTrans(extend_prompt_en, source='en', target='zh')
|
| 23 |
-
extend_prompt_out = f'{extend_prompt_zh} 【{extend_prompt_en}】'
|
| 24 |
-
else:
|
| 25 |
-
logger.info(f"extend_prompt__2__")
|
| 26 |
-
extend_prompt_out = extend_prompt_en
|
| 27 |
-
|
| 28 |
-
return prompt_en, extend_prompt_en, extend_prompt_out
|
| 29 |
-
|
| 30 |
-
def change_prompt_radio(choice):
|
| 31 |
-
if choice == "Original prompt":
|
| 32 |
-
return gr.update(visible=False)
|
| 33 |
-
else:
|
| 34 |
-
return gr.update(visible=True)
|
| 35 |
-
|
| 36 |
examples = [
|
| 37 |
['elon musk as thor'],
|
| 38 |
["giant dragon flying in the sky"],
|
|
@@ -125,10 +106,8 @@ start_work = """async() => {
|
|
| 125 |
tabitems[i].childNodes[0].children[0].style.display='none';
|
| 126 |
tabitems[i].childNodes[0].children[1].style.display='none';
|
| 127 |
tabitems[i].childNodes[0].children[2].children[0].style.display='none';
|
| 128 |
-
tabitems[i].childNodes[0].children[3].style.display='none';
|
| 129 |
-
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
}
|
| 133 |
|
| 134 |
tab_demo = window['gradioEl'].querySelectorAll('#tab_demo')[0];
|
|
@@ -136,24 +115,15 @@ start_work = """async() => {
|
|
| 136 |
tab_demo.setAttribute('style', 'height: 100%;');
|
| 137 |
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
| 138 |
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
| 139 |
-
window['gradioEl'].
|
| 140 |
-
window['gradioEl'].
|
| 141 |
-
|
| 142 |
page1.style.display = "none";
|
| 143 |
page2.style.display = "block";
|
| 144 |
window['prevPrompt'] = '';
|
| 145 |
window['doCheckPrompt'] = 0;
|
| 146 |
window['checkPrompt'] = function checkPrompt() {
|
| 147 |
try {
|
| 148 |
-
|
| 149 |
-
text0 = texts[0];
|
| 150 |
-
text1 = texts[1];
|
| 151 |
-
text2 = texts[2];
|
| 152 |
-
if (window['gradioEl'].querySelectorAll('.gr-radio')[0].checked) {
|
| 153 |
-
text_value = text1.value;
|
| 154 |
-
} else {
|
| 155 |
-
text_value = text2.value;
|
| 156 |
-
}
|
| 157 |
progress_bar = window['gradioEl'].querySelectorAll('.progress-bar');
|
| 158 |
if (window['doCheckPrompt'] === 0 && window['prevPrompt'] !== text_value && progress_bar.length == 0) {
|
| 159 |
console.log('_____new prompt___[' + text_value + ']_');
|
|
@@ -189,6 +159,26 @@ start_work = """async() => {
|
|
| 189 |
return false;
|
| 190 |
}"""
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
with gr.Blocks(title='Text-to-Image') as demo:
|
| 193 |
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
| 194 |
with gr.Box():
|
|
@@ -199,24 +189,25 @@ with gr.Blocks(title='Text-to-Image') as demo:
|
|
| 199 |
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
| 200 |
with gr.Row(elem_id="prompt_row0"):
|
| 201 |
with gr.Column(id="input_col1"):
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
|
|
|
|
|
|
|
|
|
| 206 |
with gr.Column(id="input_col2"):
|
| 207 |
-
prompt_input1 = gr.Textbox(lines=2, label="Extend prompt", visible=
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
)
|
| 214 |
with gr.Row(elem_id='tab_demo', visible=True).style(height=200):
|
| 215 |
tab_demo = gr.TabbedInterface(tab_actions, tab_titles)
|
| 216 |
with gr.Row():
|
| 217 |
gr.HTML(f"<p>{thanks_info}</p>")
|
| 218 |
|
| 219 |
-
#
|
| 220 |
-
submit_btn.click(fn=extend_prompt, inputs=[prompt_input0], outputs=[prompt_input0_en, prompt_input1, prompt_input2])
|
| 221 |
|
| 222 |
demo.launch()
|
|
|
|
| 12 |
except Exception as e:
|
| 13 |
return text
|
| 14 |
|
| 15 |
+
extend_prompt_pipe = pipeline('text-generation', model='yizhangliu/prompt-extend', max_length=77, pad_token_id=0)
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
examples = [
|
| 18 |
['elon musk as thor'],
|
| 19 |
["giant dragon flying in the sky"],
|
|
|
|
| 106 |
tabitems[i].childNodes[0].children[0].style.display='none';
|
| 107 |
tabitems[i].childNodes[0].children[1].style.display='none';
|
| 108 |
tabitems[i].childNodes[0].children[2].children[0].style.display='none';
|
| 109 |
+
tabitems[i].childNodes[0].children[3].style.display='none';
|
| 110 |
+
}
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
|
| 113 |
tab_demo = window['gradioEl'].querySelectorAll('#tab_demo')[0];
|
|
|
|
| 115 |
tab_demo.setAttribute('style', 'height: 100%;');
|
| 116 |
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
| 117 |
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
| 118 |
+
window['gradioEl'].querySelector('#input_col1_row2').children[0].setAttribute('style', 'min-width:0px;width:50%;');
|
| 119 |
+
window['gradioEl'].querySelector('#input_col1_row2').children[1].setAttribute('style', 'min-width:0px;width:50%;');
|
|
|
|
| 120 |
page1.style.display = "none";
|
| 121 |
page2.style.display = "block";
|
| 122 |
window['prevPrompt'] = '';
|
| 123 |
window['doCheckPrompt'] = 0;
|
| 124 |
window['checkPrompt'] = function checkPrompt() {
|
| 125 |
try {
|
| 126 |
+
text_value = window['gradioEl'].querySelectorAll('#prompt_work')[0].querySelectorAll('textarea')[0].value
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
progress_bar = window['gradioEl'].querySelectorAll('.progress-bar');
|
| 128 |
if (window['doCheckPrompt'] === 0 && window['prevPrompt'] !== text_value && progress_bar.length == 0) {
|
| 129 |
console.log('_____new prompt___[' + text_value + ']_');
|
|
|
|
| 159 |
return false;
|
| 160 |
}"""
|
| 161 |
|
| 162 |
+
def prompt_extend(prompt):
|
| 163 |
+
prompt_en = getTextTrans(prompt, source='zh', target='en')
|
| 164 |
+
extend_prompt_en = extend_prompt_pipe(prompt_en+',', num_return_sequences=1)[0]["generated_text"]
|
| 165 |
+
if (prompt != prompt_en):
|
| 166 |
+
logger.info(f"extend_prompt__1__")
|
| 167 |
+
extend_prompt_out = getTextTrans(extend_prompt_en, source='en', target='zh')
|
| 168 |
+
else:
|
| 169 |
+
logger.info(f"extend_prompt__2__")
|
| 170 |
+
extend_prompt_out = extend_prompt_en
|
| 171 |
+
|
| 172 |
+
return extend_prompt_out
|
| 173 |
+
|
| 174 |
+
def prompt_draw(prompt):
|
| 175 |
+
prompt_en = getTextTrans(prompt, source='zh', target='en')
|
| 176 |
+
if (prompt != prompt_en):
|
| 177 |
+
logger.info(f"draw_prompt______1__")
|
| 178 |
+
else:
|
| 179 |
+
logger.info(f"draw_prompt______2__")
|
| 180 |
+
return prompt_en
|
| 181 |
+
|
| 182 |
with gr.Blocks(title='Text-to-Image') as demo:
|
| 183 |
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
| 184 |
with gr.Box():
|
|
|
|
| 189 |
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
| 190 |
with gr.Row(elem_id="prompt_row0"):
|
| 191 |
with gr.Column(id="input_col1"):
|
| 192 |
+
with gr.Row(elem_id="input_col1_row1"):
|
| 193 |
+
prompt_input0 = gr.Textbox(lines=2, label="Original prompt", visible=True)
|
| 194 |
+
with gr.Row(elem_id="input_col1_row2"):
|
| 195 |
+
with gr.Column(elem_id="input_col1_row2_col1"):
|
| 196 |
+
draw_btn_0 = gr.Button(value = "Generate(original)", elem_id="draw-btn-0")
|
| 197 |
+
with gr.Column(elem_id="input_col1_row2_col2"):
|
| 198 |
+
extend_btn = gr.Button(value = "Extend prompt",elem_id="extend-btn")
|
| 199 |
with gr.Column(id="input_col2"):
|
| 200 |
+
prompt_input1 = gr.Textbox(lines=2, label="Extend prompt", visible=True)
|
| 201 |
+
draw_btn_1 = gr.Button(value = "Generate(extend)", elem_id="draw-btn-1")
|
| 202 |
+
prompt_work = gr.Textbox(lines=1, label="prompt_work", elem_id="prompt_work", visible=False)
|
| 203 |
+
extend_btn.click(fn=prompt_extend, inputs=[prompt_input0], outputs=[prompt_input1])
|
| 204 |
+
draw_btn_0.click(fn=prompt_draw, inputs=[prompt_input0], outputs=[prompt_work])
|
| 205 |
+
draw_btn_1.click(fn=prompt_draw, inputs=[prompt_input1], outputs=[prompt_work])
|
|
|
|
| 206 |
with gr.Row(elem_id='tab_demo', visible=True).style(height=200):
|
| 207 |
tab_demo = gr.TabbedInterface(tab_actions, tab_titles)
|
| 208 |
with gr.Row():
|
| 209 |
gr.HTML(f"<p>{thanks_info}</p>")
|
| 210 |
|
| 211 |
+
# submit_btn.click(fn=extend_prompt, inputs=[prompt_input0], outputs=[prompt_input0_en, prompt_input1, prompt_input2])
|
|
|
|
| 212 |
|
| 213 |
demo.launch()
|