Matthew Hollings commited on
Commit
6cfc29d
1 Parent(s): 58f30bc

Use only one input line.

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. README.md +19 -3
  3. app.py +9 -72
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ __pycache__
README.md CHANGED
@@ -1,13 +1,29 @@
1
  ---
2
  title: Joyous Poetry Generator
3
- emoji: 🦀
4
  colorFrom: yellow
5
- colorTo: purple
6
  sdk: gradio
7
  sdk_version: 3.28.0
8
  app_file: app.py
9
- pinned: false
10
  license: apache-2.0
11
  ---
12
 
 
 
 
 
 
 
 
 
 
 
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
1
  ---
2
  title: Joyous Poetry Generator
3
+ emoji: 🌻
4
  colorFrom: yellow
5
+ colorTo: red
6
  sdk: gradio
7
  sdk_version: 3.28.0
8
  app_file: app.py
9
+ pinned: true
10
  license: apache-2.0
11
  ---
12
 
13
+ # Joyous Poetry Generator
14
+
15
+ Some lines to start with:
16
+
17
+ Music to hear, why hear'st thou music sadly?
18
+ Sweets with sweets war not, joy delights in joy.
19
+
20
+
21
+
22
+
23
+
24
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
25
+
26
+
27
+ ## TODO
28
+ - using the original dataset find some poems which include the word "joy"
29
+ - make the suggsted prompts selectable on the page
app.py CHANGED
@@ -2,82 +2,19 @@ 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/gpt2-poetry-model")
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:
@@ -85,15 +22,15 @@ with demo:
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__":
 
2
  from transformers import pipeline
3
 
4
  # Set up the generatove model transformer pipeline
5
+ generator = pipeline("text-generation", model="matthh/gpt2-rlhf-joyous-poetry")
6
 
 
 
 
 
7
 
8
+ def generate(prompt):
9
+ result = generator(prompt, max_length=50, num_return_sequences=1)
10
+ print(result)
11
+ return result[0]["generated_text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
 
14
  def reset_all(new_line):
 
 
 
 
 
15
  return None
16
 
17
 
 
 
 
 
 
 
 
 
 
 
18
  demo = gr.Blocks()
19
 
20
  with demo:
 
22
  with gr.Column():
23
  input = gr.Textbox(
24
  lines=1,
25
+ value="Music to hear, why hear'st thou music sadly?\nSweets with sweets war not, joy delights in joy.",
26
  )
27
  with gr.Row():
28
+ generate_button = gr.Button("Generate", variant="primary")
29
+ clear_all_button = gr.Button("Reset")
30
  with gr.Column():
31
  output = gr.HTML()
32
 
33
+ generate_button.click(fn=generate, inputs=input, outputs=output)
34
  clear_all_button.click(fn=reset_all, inputs=input, outputs=output)
35
 
36
  if __name__ == "__main__":