File size: 2,110 Bytes
87050c9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77427ef
87050c9
 
 
 
77427ef
87050c9
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from h2o_wave import main, app, Q, ui, copy_expando
from transformers import pipeline

async def init(q: Q):
    if not q.client.app_initialized:
        q.app.model = pipeline("text-generation")
        q.client.app_initialized = True

    q.page.drop()

    q.page["title"] = ui.header_card(
        box="1 1 8 1",
        title="Text Generation",
        subtitle="Generate text using Huggingface pipelines",
        icon="AddNotes",
        icon_color="Blue",
    )

async def get_inputs(q: Q):
    q.page['main'] = ui.form_card(box="1 2 8 5", items=[
        ui.text_xl('Enter your text input for generation:'),
        ui.textbox(name="input_text",
                   label='',
                   value=q.app.input_text,
                   multiline=True),
        ui.separator(),
        ui.slider(name="num_words_to_generate",
                  label="Maximum number of words to generate (including input text)",
                  min=5,
                  max=50,
                  step=1,
                  value=q.app.num_words_to_generate if q.app.num_words_to_generate else 12,
                  ),
        ui.separator(),
        ui.buttons([ui.button(name="generate_text", label='Generate', primary=True),
                    ])
    ])

async def show_results(q: Q):
    q.page['main'] = ui.form_card(box="1 2 4 5", items=[
        ui.text_xl("Input Text:"),
        ui.separator(),
        ui.text(q.app.input_text),
        ui.separator(),
        ui.buttons([ui.button(name="get_inputs", label='Try Again!', primary=True),
                    ])
    ])

    result = q.app.model(q.app.input_text, max_length=q.app.num_words_to_generate, do_sample=False)[0]
    q.app.generated_text = result["generated_text"]
    q.page['visualization'] = ui.form_card(box="5 2 4 5", items=[
        ui.text_xl("Generated Text:"),
        ui.separator(''),
        ui.text(q.app.generated_text)
    ])

@app("/")
async def serve(q: Q):
    await init(q)
    if q.args.generate_text:
        copy_expando(q.args, q.app)
        await show_results(q)
    else:
        await get_inputs(q)
    await q.page.save()