Matthew Hollings commited on
Commit
4b7e826
1 Parent(s): b463816

Add the main app.py

Browse files
Files changed (1) hide show
  1. app.py +100 -0
app.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Set up the generatove model transformer pipeline
5
+ generator = pipeline("text-generation", model="matthh/")
6
+
7
+ # A sequence of lines both those typed in and the line so far
8
+ # when save is clicked the txt file is downloaded
9
+ source_lines = []
10
+ generated_lines = []
11
+
12
+
13
+ def twolists(list1, list2):
14
+ newlist = []
15
+ a1 = len(list1)
16
+ a2 = len(list2)
17
+
18
+ for i in range(max(a1, a2)):
19
+ if i < a1:
20
+ newlist.append(list1[i])
21
+ if i < a2:
22
+ newlist.append(list2[i])
23
+
24
+ return newlist
25
+
26
+
27
+ def build_poem(source_lines, generated_lines):
28
+ # This function structures the space already, set boundarys to the possible.
29
+ # return a list with the two lists interspersed
30
+ return twolists(source_lines, generated_lines)
31
+
32
+
33
+ def add_to_lines(new_line):
34
+ source_lines.append(new_line)
35
+ # NOTE: pass the whole array of lines to the generator
36
+ # There is a compounding of the effect, a spiral of interaction.
37
+ poem = build_poem(source_lines, generated_lines)
38
+
39
+ # pass the last two lines of the poem
40
+ prompt = "\n".join(poem[-2:])
41
+ result = generator(prompt, max_length=50, num_return_sequences=3)
42
+ response = result[0]["generated_text"]
43
+
44
+ # Get only the new generated text
45
+ response = response.replace(prompt, "")
46
+
47
+ generated_lines.append(response)
48
+
49
+ lines = []
50
+ for line in build_poem(source_lines, generated_lines):
51
+ if (len(lines) % 2) == 1:
52
+ line = f"<p style='text-align: right;'>{line}</p>"
53
+ else:
54
+ line = f"<p>{line}</p>"
55
+ lines.append(line)
56
+
57
+ html = "".join(lines)
58
+
59
+ return html
60
+
61
+
62
+ def reset_all(new_line):
63
+ global source_lines
64
+ global generated_lines
65
+ source_lines = []
66
+ generated_lines = []
67
+
68
+ return None
69
+
70
+
71
+ # demo = gr.Interface(
72
+ # fn=add_to_lines,
73
+ # inputs=gr.Textbox(
74
+ # lines=1,
75
+ # value="The drought had lasted now for ten million years, and the reign of the terrible lizards had long since ended.",
76
+ # ),
77
+ # outputs="html",
78
+ # allow_flagging="never",
79
+ # )
80
+
81
+ demo = gr.Blocks()
82
+
83
+ with demo:
84
+ with gr.Row():
85
+ with gr.Column():
86
+ input = gr.Textbox(
87
+ lines=1,
88
+ value="The drought had lasted now for ten million years, and the reign of the terrible lizards had long since ended.",
89
+ )
90
+ with gr.Row():
91
+ add_line_button = gr.Button("Add line", variant="primary")
92
+ clear_all_button = gr.Button("Reset all")
93
+ with gr.Column():
94
+ output = gr.HTML()
95
+
96
+ add_line_button.click(fn=add_to_lines, inputs=input, outputs=output)
97
+ clear_all_button.click(fn=reset_all, inputs=input, outputs=output)
98
+
99
+ if __name__ == "__main__":
100
+ demo.launch()