Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,12 @@
|
|
| 1 |
-
import os
|
| 2 |
import json
|
|
|
|
|
|
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
|
| 6 |
from pingpong import PingPong
|
| 7 |
from pingpong.pingpong import PPManager
|
|
@@ -9,6 +14,64 @@ from pingpong.pingpong import PromptFmt
|
|
| 9 |
from pingpong.pingpong import UIFmt
|
| 10 |
from pingpong.gradio import GradioChatUIFmt
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
class LLaMA2ChatPromptFmt(PromptFmt):
|
| 13 |
@classmethod
|
| 14 |
def ctx(cls, context):
|
|
@@ -54,7 +117,15 @@ class GradioLLaMA2ChatPPManager(LLaMA2ChatPPManager):
|
|
| 54 |
TOKEN = os.getenv('HF_TOKEN')
|
| 55 |
MODEL_ID = 'meta-llama/Llama-2-70b-chat-hf'
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
STYLES = """
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
.small-big {
|
| 59 |
font-size: 12pt !important;
|
| 60 |
}
|
|
@@ -110,13 +181,17 @@ STYLES = """
|
|
| 110 |
.no-label-chatbot {
|
| 111 |
border: none !important;
|
| 112 |
box-shadow: none !important;
|
| 113 |
-
height:
|
| 114 |
}
|
| 115 |
|
| 116 |
.no-label-chatbot > div > div:nth-child(1) {
|
| 117 |
display: none;
|
| 118 |
}
|
| 119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
.left-margin-30 {
|
| 121 |
padding-left: 30px !important;
|
| 122 |
}
|
|
@@ -138,6 +213,9 @@ STYLES = """
|
|
| 138 |
}
|
| 139 |
"""
|
| 140 |
|
|
|
|
|
|
|
|
|
|
| 141 |
def get_new_ppm(ping):
|
| 142 |
ppm = LLaMA2ChatPPManager()
|
| 143 |
ppm.ctx = """\
|
|
@@ -192,6 +270,44 @@ def gen_text(prompt, hf_model='meta-llama/Llama-2-70b-chat-hf', hf_token=None, p
|
|
| 192 |
|
| 193 |
return json.loads(r.content.decode("utf-8"))[0]['generated_text']
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
def select(editor, evt: gr.SelectData):
|
| 196 |
return [
|
| 197 |
evt.value,
|
|
@@ -199,9 +315,9 @@ def select(editor, evt: gr.SelectData):
|
|
| 199 |
evt.index[1]
|
| 200 |
]
|
| 201 |
|
| 202 |
-
def get_gen_txt(editor, prompt, only_gen_text=False):
|
| 203 |
if editor.strip() == '':
|
| 204 |
-
ppm = get_new_ppm('Write a short paragraph to start a short story for me')
|
| 205 |
else:
|
| 206 |
ppm = get_new_ppm(f"""{prompt}
|
| 207 |
--------------------------------
|
|
@@ -217,22 +333,9 @@ def get_gen_txt(editor, prompt, only_gen_text=False):
|
|
| 217 |
print(f"something went wrong - {e}")
|
| 218 |
return editor
|
| 219 |
|
| 220 |
-
def gen_txt(editor):
|
| 221 |
-
return [
|
| 222 |
-
get_gen_txt(editor, "Write the next paragraph based on the following stories so far."),
|
| 223 |
-
0,
|
| 224 |
-
gr.update(interactive=True),
|
| 225 |
-
gr.update(visible=False),
|
| 226 |
-
gr.update(visible=False),
|
| 227 |
-
gr.update(visible=False),
|
| 228 |
-
gr.update(interactive=True),
|
| 229 |
-
gr.update(interactive=True),
|
| 230 |
-
gr.update(interactive=True),
|
| 231 |
-
]
|
| 232 |
-
|
| 233 |
-
def gen_txt_with_prompt(editor, prompt):
|
| 234 |
return [
|
| 235 |
-
get_gen_txt(editor, prompt),
|
| 236 |
0,
|
| 237 |
gr.update(interactive=True),
|
| 238 |
gr.update(visible=False),
|
|
@@ -240,7 +343,6 @@ def gen_txt_with_prompt(editor, prompt):
|
|
| 240 |
gr.update(visible=False),
|
| 241 |
gr.update(interactive=True),
|
| 242 |
gr.update(interactive=True),
|
| 243 |
-
gr.update(interactive=True),
|
| 244 |
]
|
| 245 |
|
| 246 |
def chat_gen(editor, chat_txt, chatbot, ppm, regen=False):
|
|
@@ -310,9 +412,9 @@ def replace_sel(editor, replace_type, selected_text, sel_index_from, sel_index_t
|
|
| 310 |
0
|
| 311 |
]
|
| 312 |
|
| 313 |
-
def gen_alt(editor, num_enabled_alts, alt_btn1, alt_btn2, alt_btn3):
|
| 314 |
if num_enabled_alts < 3:
|
| 315 |
-
gen_txt = get_gen_txt(editor, "Write the next paragraph based on the following stories so far.", only_gen_text=True)
|
| 316 |
|
| 317 |
return [
|
| 318 |
min(num_enabled_alts+1, 3),
|
|
@@ -326,7 +428,6 @@ def gen_alt(editor, num_enabled_alts, alt_btn1, alt_btn2, alt_btn3):
|
|
| 326 |
" ",
|
| 327 |
gr.update(interactive=True),
|
| 328 |
gr.update(interactive=True),
|
| 329 |
-
gr.update(interactive=True),
|
| 330 |
]
|
| 331 |
|
| 332 |
def fill_with_gen(alt_txt, editor):
|
|
@@ -355,16 +456,32 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 355 |
"be changed over time, but [meta-llama/Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) is selected for now.",
|
| 356 |
elem_classes=['center', 'small-big'])
|
| 357 |
|
|
|
|
|
|
|
| 358 |
with gr.Row():
|
| 359 |
with gr.Column(scale=2):
|
| 360 |
-
editor = gr.Textbox(lines=
|
| 361 |
word_counter = gr.Markdown("0 words", elem_classes=['right'])
|
| 362 |
|
| 363 |
with gr.Column(scale=1):
|
| 364 |
with gr.Tab("Control"):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 365 |
with gr.Column(elem_classes=['group-border']):
|
| 366 |
with gr.Column():
|
| 367 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 368 |
|
| 369 |
with gr.Row():
|
| 370 |
gen_btn = gr.Button("generate text", elem_classes=['control-label-font', 'control-button'])
|
|
@@ -383,14 +500,6 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 383 |
gr.Markdown("↳", scale=1, elem_classes=['wrap'])
|
| 384 |
alt_btn3 = gr.Button("Alternative 3", elem_classes=['alt-button'], scale=8)
|
| 385 |
|
| 386 |
-
with gr.Row(elem_classes=['group-border']):
|
| 387 |
-
with gr.Column():
|
| 388 |
-
gr.Markdown("'Write the next paragraph based on the following stories so far.' is the default prompt when clicking `generate text`, and the text so far will always be attached to the end. By giving your own prompt, only the default prompt will be replaced.")
|
| 389 |
-
|
| 390 |
-
with gr.Column(elem_classes=['no-gap']):
|
| 391 |
-
gen_with_prompt_btn = gr.Button("generate text with custom prompt", elem_classes=['control-label-font', 'control-button'])
|
| 392 |
-
prompt = gr.Textbox(placeholder="enter prompt: ", elem_classes=['no-label'])
|
| 393 |
-
|
| 394 |
with gr.Column(elem_classes=['group-border']):
|
| 395 |
with gr.Row():
|
| 396 |
selected_text = gr.Markdown("Selected text will be displayed in this area", elem_classes=['highlighted-text'])
|
|
@@ -400,11 +509,6 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 400 |
replace_sel_btn = gr.Button("replace selection", elem_classes=['control-label-font', 'control-button'])
|
| 401 |
replace_type = gr.Dropdown(choices=['word', 'sentense', 'phrase', 'paragraph'], value='sentense', interactive=True, elem_classes=['no-label'])
|
| 402 |
|
| 403 |
-
# with gr.Row():
|
| 404 |
-
# with gr.Column(elem_classes=['no-gap']):
|
| 405 |
-
# rewrite_sel_btn = gr.Button("rewrite selection", elem_classes=['control-label-font', 'control-button'])
|
| 406 |
-
# rewrite_prompt = gr.Textbox(placeholder="Rewrite the text: ", elem_classes=['no-label'])
|
| 407 |
-
|
| 408 |
with gr.Tab("Chatting"):
|
| 409 |
chatbot = gr.Chatbot([], elem_classes=['no-label-chatbot'])
|
| 410 |
chat_txt = gr.Textbox(placeholder="enter question", elem_classes=['no-label'])
|
|
@@ -413,8 +517,33 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 413 |
clear_btn = gr.Button("clear", elem_classes=['control-label-font', 'control-button'])
|
| 414 |
regen_btn = gr.Button("regenerate", elem_classes=['control-label-font', 'control-button'])
|
| 415 |
|
| 416 |
-
|
| 417 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
editor.change(
|
| 419 |
fn=None,
|
| 420 |
inputs=[editor],
|
|
@@ -437,11 +566,11 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 437 |
gr.update(interactive=False),
|
| 438 |
),
|
| 439 |
inputs=None,
|
| 440 |
-
outputs=[gen_btn, gen_alt_btn,
|
| 441 |
).then(
|
| 442 |
fn=gen_txt,
|
| 443 |
-
inputs=[editor],
|
| 444 |
-
outputs=[editor, num_enabled_alts, gen_alt_btn, first_alt, second_alt, third_alt, gen_btn,
|
| 445 |
)
|
| 446 |
|
| 447 |
gen_alt_btn.click(
|
|
@@ -452,11 +581,11 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 452 |
gr.update(interactive=False),
|
| 453 |
),
|
| 454 |
inputs=None,
|
| 455 |
-
outputs=[gen_btn, gen_alt_btn,
|
| 456 |
).then(
|
| 457 |
fn=gen_alt,
|
| 458 |
-
inputs=[editor, num_enabled_alts, alt_btn1, alt_btn2, alt_btn3],
|
| 459 |
-
outputs=[num_enabled_alts, gen_alt_btn, first_alt, alt_btn1, second_alt, alt_btn2, third_alt, alt_btn3, progress_bar, gen_btn,
|
| 460 |
)
|
| 461 |
|
| 462 |
alt_btn1.click(
|
|
@@ -475,12 +604,6 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 475 |
outputs=[editor, num_enabled_alts, gen_alt_btn, first_alt, second_alt, third_alt]
|
| 476 |
)
|
| 477 |
|
| 478 |
-
gen_with_prompt_btn.click(
|
| 479 |
-
gen_txt_with_prompt,
|
| 480 |
-
inputs=[editor, prompt],
|
| 481 |
-
outputs=[editor, num_enabled_alts, gen_alt_btn, first_alt, second_alt, third_alt]
|
| 482 |
-
)
|
| 483 |
-
|
| 484 |
replace_sel_btn.click(
|
| 485 |
fn=replace_sel,
|
| 486 |
inputs=[editor, replace_type, selected_text, sel_index_from, sel_index_to],
|
|
@@ -499,5 +622,5 @@ with gr.Blocks(css=STYLES) as demo:
|
|
| 499 |
inputs=[editor, chat_txt, chatbot, chat_history],
|
| 500 |
outputs=[chat_txt, chatbot, chat_history]
|
| 501 |
)
|
| 502 |
-
|
| 503 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import json
|
| 2 |
+
import random
|
| 3 |
+
import string
|
| 4 |
import requests
|
| 5 |
+
import torch
|
| 6 |
+
|
| 7 |
import gradio as gr
|
| 8 |
+
from PIL import Image
|
| 9 |
+
from diffusers import StableDiffusionPipeline
|
| 10 |
|
| 11 |
from pingpong import PingPong
|
| 12 |
from pingpong.pingpong import PPManager
|
|
|
|
| 14 |
from pingpong.pingpong import UIFmt
|
| 15 |
from pingpong.gradio import GradioChatUIFmt
|
| 16 |
|
| 17 |
+
from fpdf import FPDF
|
| 18 |
+
|
| 19 |
+
class PDF(FPDF):
|
| 20 |
+
def header(self):
|
| 21 |
+
# Arial bold 15
|
| 22 |
+
self.set_font('Arial', 'B', 15)
|
| 23 |
+
# Calculate width of title and position
|
| 24 |
+
w = self.get_string_width(self.title) + 6
|
| 25 |
+
self.set_x((210 - w) / 2)
|
| 26 |
+
# Colors of frame, background and text
|
| 27 |
+
self.set_draw_color(255, 255, 255)
|
| 28 |
+
self.set_fill_color(255, 255, 255)
|
| 29 |
+
# self.set_text_color(220, 50, 50)
|
| 30 |
+
# Thickness of frame (1 mm)
|
| 31 |
+
self.set_line_width(1)
|
| 32 |
+
# Title
|
| 33 |
+
self.cell(w, 9, self.title, 1, 1, 'C', 1)
|
| 34 |
+
# Line break
|
| 35 |
+
self.ln(10)
|
| 36 |
+
|
| 37 |
+
if self.art is not None:
|
| 38 |
+
self.image(self.art, x=self.w/2.0-25, w=50)
|
| 39 |
+
self.ln(10)
|
| 40 |
+
|
| 41 |
+
def footer(self):
|
| 42 |
+
# Position at 1.5 cm from bottom
|
| 43 |
+
self.set_y(-15)
|
| 44 |
+
# Arial italic 8
|
| 45 |
+
self.set_font('Arial', 'I', 8)
|
| 46 |
+
# Text color in gray
|
| 47 |
+
self.set_text_color(128)
|
| 48 |
+
# Page number
|
| 49 |
+
self.cell(0, 10, 'Page ' + str(self.page_no()), 0, 0, 'C')
|
| 50 |
+
|
| 51 |
+
def chapter_title(self, num, label):
|
| 52 |
+
# Arial 12
|
| 53 |
+
self.set_font('Arial', '', 12)
|
| 54 |
+
# Background color
|
| 55 |
+
self.set_fill_color(200, 220, 255)
|
| 56 |
+
# Title
|
| 57 |
+
self.cell(0, 6, 'Chapter %d : %s' % (num, label), 0, 1, 'L', 1)
|
| 58 |
+
# Line break
|
| 59 |
+
self.ln(4)
|
| 60 |
+
|
| 61 |
+
def chapter_body(self, content):
|
| 62 |
+
# Times 12
|
| 63 |
+
self.set_font('Times', '', 12)
|
| 64 |
+
# Output justified text
|
| 65 |
+
self.multi_cell(0, 5, content)
|
| 66 |
+
# Line break
|
| 67 |
+
self.ln()
|
| 68 |
+
# Mention in italics
|
| 69 |
+
self.set_font('', 'I')
|
| 70 |
+
|
| 71 |
+
def print_chapter(self, content):
|
| 72 |
+
self.add_page()
|
| 73 |
+
self.chapter_body(content)
|
| 74 |
+
|
| 75 |
class LLaMA2ChatPromptFmt(PromptFmt):
|
| 76 |
@classmethod
|
| 77 |
def ctx(cls, context):
|
|
|
|
| 117 |
TOKEN = os.getenv('HF_TOKEN')
|
| 118 |
MODEL_ID = 'meta-llama/Llama-2-70b-chat-hf'
|
| 119 |
|
| 120 |
+
pipe = StableDiffusionPipeline.from_pretrained(
|
| 121 |
+
"nota-ai/bk-sdm-small", torch_dtype=torch.float16
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
STYLES = """
|
| 125 |
+
.left-panel {
|
| 126 |
+
min-width: min(290px, 100%) !important;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
.small-big {
|
| 130 |
font-size: 12pt !important;
|
| 131 |
}
|
|
|
|
| 181 |
.no-label-chatbot {
|
| 182 |
border: none !important;
|
| 183 |
box-shadow: none !important;
|
| 184 |
+
height: 520px !important;
|
| 185 |
}
|
| 186 |
|
| 187 |
.no-label-chatbot > div > div:nth-child(1) {
|
| 188 |
display: none;
|
| 189 |
}
|
| 190 |
|
| 191 |
+
.no-label-image > div:nth-child(2) {
|
| 192 |
+
display: none;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
.left-margin-30 {
|
| 196 |
padding-left: 30px !important;
|
| 197 |
}
|
|
|
|
| 213 |
}
|
| 214 |
"""
|
| 215 |
|
| 216 |
+
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
|
| 217 |
+
return ''.join(random.choice(chars) for _ in range(size))
|
| 218 |
+
|
| 219 |
def get_new_ppm(ping):
|
| 220 |
ppm = LLaMA2ChatPPManager()
|
| 221 |
ppm.ctx = """\
|
|
|
|
| 270 |
|
| 271 |
return json.loads(r.content.decode("utf-8"))[0]['generated_text']
|
| 272 |
|
| 273 |
+
def gen_art(editor, cover_art_image, gen_cover_art_prompt):
|
| 274 |
+
if gen_cover_art_prompt.strip() == "":
|
| 275 |
+
ppm = get_new_ppm(f"""describe the story below as a movie poster. give me the caption ONLY.
|
| 276 |
+
--------------------------------
|
| 277 |
+
{editor}""")
|
| 278 |
+
cover_art_prompt = gen_text(ppm.build_prompts(), hf_model=MODEL_ID, hf_token=TOKEN)
|
| 279 |
+
|
| 280 |
+
return [
|
| 281 |
+
cover_art_image,
|
| 282 |
+
cover_art_prompt
|
| 283 |
+
]
|
| 284 |
+
else:
|
| 285 |
+
global pipe
|
| 286 |
+
pipe = pipe.to("cuda")
|
| 287 |
+
|
| 288 |
+
return [
|
| 289 |
+
pipe(gen_cover_art_prompt).images[0],
|
| 290 |
+
gen_cover_art_prompt
|
| 291 |
+
]
|
| 292 |
+
|
| 293 |
+
def generate_pdf(title, editor, concept_art):
|
| 294 |
+
tmp_filename = id_generator()
|
| 295 |
+
|
| 296 |
+
if concept_art is not None:
|
| 297 |
+
im = Image.fromarray(concept_art)
|
| 298 |
+
im.save(f"{tmp_filename}.png")
|
| 299 |
+
|
| 300 |
+
pdf = PDF()
|
| 301 |
+
pdf.title = "Untitled" if title.strip() == "" else title
|
| 302 |
+
pdf.art = None if concept_art is None else f"{tmp_filename}.png"
|
| 303 |
+
pdf.print_chapter(editor)
|
| 304 |
+
pdf.output(f'{tmp_filename}.pdf', 'F')
|
| 305 |
+
|
| 306 |
+
return (
|
| 307 |
+
gr.update(value=f'{tmp_filename}.pdf', visible=True),
|
| 308 |
+
" "
|
| 309 |
+
)
|
| 310 |
+
|
| 311 |
def select(editor, evt: gr.SelectData):
|
| 312 |
return [
|
| 313 |
evt.value,
|
|
|
|
| 315 |
evt.index[1]
|
| 316 |
]
|
| 317 |
|
| 318 |
+
def get_gen_txt(title, editor, prompt, only_gen_text=False):
|
| 319 |
if editor.strip() == '':
|
| 320 |
+
ppm = get_new_ppm(f'Write a short paragraph to start a short story titled "{title}" for me')
|
| 321 |
else:
|
| 322 |
ppm = get_new_ppm(f"""{prompt}
|
| 323 |
--------------------------------
|
|
|
|
| 333 |
print(f"something went wrong - {e}")
|
| 334 |
return editor
|
| 335 |
|
| 336 |
+
def gen_txt(title, editor, prompt):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
return [
|
| 338 |
+
get_gen_txt(title, editor, "Write the next paragraph based on the following stories so far." if prompt.strip() == "" else prompt),
|
| 339 |
0,
|
| 340 |
gr.update(interactive=True),
|
| 341 |
gr.update(visible=False),
|
|
|
|
| 343 |
gr.update(visible=False),
|
| 344 |
gr.update(interactive=True),
|
| 345 |
gr.update(interactive=True),
|
|
|
|
| 346 |
]
|
| 347 |
|
| 348 |
def chat_gen(editor, chat_txt, chatbot, ppm, regen=False):
|
|
|
|
| 412 |
0
|
| 413 |
]
|
| 414 |
|
| 415 |
+
def gen_alt(title, editor, num_enabled_alts, alt_btn1, alt_btn2, alt_btn3):
|
| 416 |
if num_enabled_alts < 3:
|
| 417 |
+
gen_txt = get_gen_txt(title, editor, "Write the next paragraph based on the following stories so far.", only_gen_text=True)
|
| 418 |
|
| 419 |
return [
|
| 420 |
min(num_enabled_alts+1, 3),
|
|
|
|
| 428 |
" ",
|
| 429 |
gr.update(interactive=True),
|
| 430 |
gr.update(interactive=True),
|
|
|
|
| 431 |
]
|
| 432 |
|
| 433 |
def fill_with_gen(alt_txt, editor):
|
|
|
|
| 456 |
"be changed over time, but [meta-llama/Llama-2-70b-chat-hf](https://huggingface.co/meta-llama/Llama-2-70b-chat-hf) is selected for now.",
|
| 457 |
elem_classes=['center', 'small-big'])
|
| 458 |
|
| 459 |
+
progress_bar = gr.Textbox(elem_classes=['no-label'])
|
| 460 |
+
|
| 461 |
with gr.Row():
|
| 462 |
with gr.Column(scale=2):
|
| 463 |
+
editor = gr.Textbox(lines=32, max_lines=32, elem_classes=['no-label', 'small-big-textarea'])
|
| 464 |
word_counter = gr.Markdown("0 words", elem_classes=['right'])
|
| 465 |
|
| 466 |
with gr.Column(scale=1):
|
| 467 |
with gr.Tab("Control"):
|
| 468 |
+
with gr.Column(elem_classes=['group-border']):
|
| 469 |
+
gr.Markdown('### title')
|
| 470 |
+
title = gr.Textbox("pokemon training story", elem_classes=['no-label'])
|
| 471 |
+
|
| 472 |
with gr.Column(elem_classes=['group-border']):
|
| 473 |
with gr.Column():
|
| 474 |
+
gr.Markdown("For instant generation and concatenation, use `generate text` button. "
|
| 475 |
+
"Want to explore alternative choices? use `generate alternatives` button.")
|
| 476 |
+
|
| 477 |
+
with gr.Accordion("longer guideline", open=False):
|
| 478 |
+
gr.Markdown("`generate text` button generate continued text and attach it to the end. "
|
| 479 |
+
"on the other hand, `generate alternatives` button generate alternate texts "
|
| 480 |
+
"up to 3 and let you choose one of them. In both cases, **Write the next paragraph based on "
|
| 481 |
+
"the following stories so far.** is the default prompt. If you want to try your own designed "
|
| 482 |
+
"prompt, enter it in the textbox below.")
|
| 483 |
+
|
| 484 |
+
prompt = gr.Textbox(placeholder="design your own prompt", elem_classes=['no-label'])
|
| 485 |
|
| 486 |
with gr.Row():
|
| 487 |
gen_btn = gr.Button("generate text", elem_classes=['control-label-font', 'control-button'])
|
|
|
|
| 500 |
gr.Markdown("↳", scale=1, elem_classes=['wrap'])
|
| 501 |
alt_btn3 = gr.Button("Alternative 3", elem_classes=['alt-button'], scale=8)
|
| 502 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
with gr.Column(elem_classes=['group-border']):
|
| 504 |
with gr.Row():
|
| 505 |
selected_text = gr.Markdown("Selected text will be displayed in this area", elem_classes=['highlighted-text'])
|
|
|
|
| 509 |
replace_sel_btn = gr.Button("replace selection", elem_classes=['control-label-font', 'control-button'])
|
| 510 |
replace_type = gr.Dropdown(choices=['word', 'sentense', 'phrase', 'paragraph'], value='sentense', interactive=True, elem_classes=['no-label'])
|
| 511 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 512 |
with gr.Tab("Chatting"):
|
| 513 |
chatbot = gr.Chatbot([], elem_classes=['no-label-chatbot'])
|
| 514 |
chat_txt = gr.Textbox(placeholder="enter question", elem_classes=['no-label'])
|
|
|
|
| 517 |
clear_btn = gr.Button("clear", elem_classes=['control-label-font', 'control-button'])
|
| 518 |
regen_btn = gr.Button("regenerate", elem_classes=['control-label-font', 'control-button'])
|
| 519 |
|
| 520 |
+
with gr.Tab("Exporting"):
|
| 521 |
+
cover_art = gr.Image(interactive=False, elem_classes=['no-label-image'])
|
| 522 |
+
gen_cover_art_prompt = gr.Textbox(lines=5, max_lines=5, elem_classes=['no-label'])
|
| 523 |
+
# toggle between "generate prompt for cover art" and "generate cover art"
|
| 524 |
+
gen_cover_art_btn = gr.Button("generate prompt for cover art", elem_classes=['control-label-font', 'control-button'])
|
| 525 |
+
gen_pdf_btn = gr.Button("export as PDF", elem_classes=['control-label-font', 'control-button'])
|
| 526 |
+
pdf_file = gr.File(visible=False)
|
| 527 |
+
|
| 528 |
+
gen_pdf_btn.click(
|
| 529 |
+
lambda t, e, c: generate_pdf(t, e, c),
|
| 530 |
+
inputs=[title, editor, cover_art],
|
| 531 |
+
outputs=[pdf_file, progress_bar]
|
| 532 |
+
)
|
| 533 |
+
|
| 534 |
+
gen_cover_art_btn.click(
|
| 535 |
+
gen_art,
|
| 536 |
+
inputs=[editor, cover_art, gen_cover_art_prompt],
|
| 537 |
+
outputs=[cover_art, gen_cover_art_prompt]
|
| 538 |
+
)
|
| 539 |
+
|
| 540 |
+
gen_cover_art_prompt.change(
|
| 541 |
+
fn=None,
|
| 542 |
+
inputs=[gen_cover_art_prompt],
|
| 543 |
+
outputs=[gen_cover_art_btn],
|
| 544 |
+
_js="(t) => t.trim() == '' ? 'generate prompt for cover art' : 'generate cover art'"
|
| 545 |
+
)
|
| 546 |
+
|
| 547 |
editor.change(
|
| 548 |
fn=None,
|
| 549 |
inputs=[editor],
|
|
|
|
| 566 |
gr.update(interactive=False),
|
| 567 |
),
|
| 568 |
inputs=None,
|
| 569 |
+
outputs=[gen_btn, gen_alt_btn, replace_sel_btn]
|
| 570 |
).then(
|
| 571 |
fn=gen_txt,
|
| 572 |
+
inputs=[title, editor, prompt],
|
| 573 |
+
outputs=[editor, num_enabled_alts, gen_alt_btn, first_alt, second_alt, third_alt, gen_btn, replace_sel_btn]
|
| 574 |
)
|
| 575 |
|
| 576 |
gen_alt_btn.click(
|
|
|
|
| 581 |
gr.update(interactive=False),
|
| 582 |
),
|
| 583 |
inputs=None,
|
| 584 |
+
outputs=[gen_btn, gen_alt_btn, replace_sel_btn]
|
| 585 |
).then(
|
| 586 |
fn=gen_alt,
|
| 587 |
+
inputs=[title, editor, num_enabled_alts, alt_btn1, alt_btn2, alt_btn3],
|
| 588 |
+
outputs=[num_enabled_alts, gen_alt_btn, first_alt, alt_btn1, second_alt, alt_btn2, third_alt, alt_btn3, progress_bar, gen_btn, replace_sel_btn],
|
| 589 |
)
|
| 590 |
|
| 591 |
alt_btn1.click(
|
|
|
|
| 604 |
outputs=[editor, num_enabled_alts, gen_alt_btn, first_alt, second_alt, third_alt]
|
| 605 |
)
|
| 606 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 607 |
replace_sel_btn.click(
|
| 608 |
fn=replace_sel,
|
| 609 |
inputs=[editor, replace_type, selected_text, sel_index_from, sel_index_to],
|
|
|
|
| 622 |
inputs=[editor, chat_txt, chatbot, chat_history],
|
| 623 |
outputs=[chat_txt, chatbot, chat_history]
|
| 624 |
)
|
| 625 |
+
|
| 626 |
demo.launch()
|