File size: 2,048 Bytes
30973d8
6b9687e
30973d8
 
 
 
6b9687e
 
 
30973d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b9687e
 
30973d8
ec6ed9c
30973d8
 
6b9687e
 
 
30973d8
ec6ed9c
30973d8
 
6b9687e
ec6ed9c
 
 
30973d8
6b9687e
ec6ed9c
 
6b9687e
30973d8
 
ec6ed9c
 
 
 
 
 
 
 
 
30973d8
 
 
 
 
 
 
 
 
 
 
 
 
6b9687e
 
 
 
30973d8
 
 
 
 
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import re
import gradio as gr
from transformers import pipeline

# Set up the generatove model transformer pipeline
generator = pipeline("text-generation", model="gpt2")

# A sequence of lines both those typed in and the line so far
# when save is clicked the txt file is downloaded
source_lines = []
generated_lines = []


def twolists(list1, list2):
    newlist = []
    a1 = len(list1)
    a2 = len(list2)

    for i in range(max(a1, a2)):
        if i < a1:
            newlist.append(list1[i])
        if i < a2:
            newlist.append(list2[i])

    return newlist


def build_poem(source_lines, generated_lines):
    # This function structures the space already, set boundarys to the possible.
    # return a list with the two lists interspersed
    return twolists(source_lines, generated_lines)


def add_to_lines(new_line):
    source_lines.append(new_line)
    # NOTE: pass the whole array of lines to the generator
    # There is a compounding of the effect, a spiral of interaction.
    poem = build_poem(source_lines, generated_lines)

    # pass the last two lines of the poem
    prompt = "\n".join(poem[-2:])
    result = generator(prompt, max_length=50, num_return_sequences=3)
    response = result[0]["generated_text"]

    # Get only the new generated text
    response = response.replace(prompt, "")

    generated_lines.append(response)

    lines = []
    for line in build_poem(source_lines, generated_lines):
        if (len(lines) % 2) == 1:
            line = f"<p style='text-align: right;'>{line}</p>"
        else:
            line = f"<p>{line}</p>"
        lines.append(line)

    html = "".join(lines)

    return html


demo = gr.Interface(
    fn=add_to_lines,
    inputs=gr.Textbox(
        lines=1,
        value="The drought had lasted now for ten million years, and the reign of the terrible lizards had long since ended.",
    ),
    outputs="html",
    allow_flagging="never",
)


if __name__ == "__main__":
    demo.launch()


def downloadtext():
    # somehow print the values from the list
    pass