rodolfoocampo commited on
Commit
d40162f
1 Parent(s): 94d49fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -131
app.py CHANGED
@@ -1,95 +1,53 @@
1
  import numpy as np
 
2
  import gradio as gr
3
  import openai
4
- import os
5
-
6
- openai.api_key = os.environ['OPENAI_KEY']
7
-
8
- def generateStory(theme1, theme2):
9
-
10
- prompt_text = "Write a short children's story of less than 500 words, which will be read by a parent to their 4-year-old. Make the language witty and funny, and make the story have an interesting twist. The story has to creatively include the following themes: \"{}\" and \"{}\".".format(theme1,theme2)
11
- response = openai.Completion.create(
12
- engine="text-davinci-003",
13
- prompt=prompt_text,
14
- temperature=0.7,
15
- max_tokens=600,
16
- top_p=1,
17
- frequency_penalty=0,
18
- presence_penalty=0
19
- )
20
- story = response["choices"][0]["text"]
21
-
22
- content_to_classify = "Your content here"
23
-
24
- response = openai.Completion.create(
25
- model="content-filter-alpha",
26
- prompt = "<|endoftext|>"+story+"\n--\nLabel:",
27
- temperature=0,
28
- max_tokens=1,
29
- top_p=0,
30
- logprobs=10
31
- )
32
-
33
- output_label = response["choices"][0]["text"]
34
-
35
- # This is the probability at which we evaluate that a "2" is likely real
36
- # vs. should be discarded as a false positive
37
- toxic_threshold = -0.355
38
-
39
- if output_label == "2":
40
- story='Please generate again'
41
-
42
- if story.startswith('\n\n'):
43
- story = story[2:]
44
- return story
45
 
46
 
47
- def illustratedStory(story, theme1, theme2):
48
 
49
- if story != 'Please generate again':
50
- #image_prompt = illustration_response["choices"][0]["text"]
51
- image_prompt="A watercolour painting for a children's book of a {} and {}, artstation, award winning, stunning, trending on Artstation, volumetric lighting, ray tracing, 8k, masterpiece, fully adorable".format(theme1,theme2)
52
- print(image_prompt)
53
- response = openai.Image.create(
54
-
55
- prompt= image_prompt,
56
- n=1,
57
- size="1024x1024"
58
- )
59
- image_url = response['data'][0]['url']
 
 
60
 
61
- else:
62
- image = np.zeros([100,100,3],dtype=np.uint8)
63
- image.fill(255) # or img[:] = 255
64
 
65
- return image_url
66
-
67
- def continueStory(inputStory):
68
- prompt_text = inputStory
69
  response = openai.Completion.create(
70
- engine="text-davinci-002",
71
- prompt=prompt_text,
72
- temperature=0.7,
73
- max_tokens=250,
74
- top_p=1,
75
- frequency_penalty=0,
76
- presence_penalty=0
77
  )
78
  story = response["choices"][0]["text"]
79
 
80
  content_to_classify = "Your content here"
81
 
82
  response = openai.Completion.create(
83
- model="content-filter-alpha",
84
- prompt = "<|endoftext|>"+story+"\n--\nLabel:",
85
- temperature=0,
86
- max_tokens=1,
87
- top_p=0,
88
- logprobs=10
89
- )
90
-
91
  output_label = response["choices"][0]["text"]
92
 
 
93
  # This is the probability at which we evaluate that a "2" is likely real
94
  # vs. should be discarded as a false positive
95
  toxic_threshold = -0.355
@@ -99,12 +57,7 @@ def continueStory(inputStory):
99
 
100
  if story.startswith('\n\n'):
101
  story = story[2:]
102
- return inputStory + story
103
-
104
- def downloadStory(story):
105
- with open("output.txt", "a") as f:
106
- print("Hello stackoverflow!", file=f)
107
- print("I have a question.", file=f)
108
 
109
  '''
110
  demo = gr.Interface(
@@ -121,62 +74,47 @@ demo.launch()
121
 
122
 
123
  with gr.Blocks(css='''
124
- .h1 {
125
-
126
- font-family: HK Grotesk;
127
- font-style: normal;
128
- font-weight: bold;
129
- font-size: 100px;
130
- line-height: 105%;
131
- margin: 0;
132
- }
133
-
134
- ''') as demo:
135
- title = gr.HTML(
136
- """
137
- <div style="text-align: center; margin: 0;">
138
- <div style="
139
- display: inline-flex;
140
- align-items: center;
141
- gap: 0.8rem;
142
- font-size: 1.75rem;
143
- ">
144
-
145
  <h1 style="font-weight: 900; margin-bottom: 7px;">
146
- Infinite Stories
147
  </h1>
148
- </div>
149
- <p style="margin-bottom: 10px; font-size: 94%;">
150
-
151
- </p>
152
- <br>
153
- <p style="font-size: 70%;>Generate the beginning of a story by writing two themes, then edit, add to it, extend it and illustrate it! </p>
154
-
155
  </div>
156
- """)
157
- with gr.Row():
158
- theme1 = gr.Textbox(label='Theme 1', elem_id = 'theme')
159
- theme2 = gr.Textbox(label='Theme 2', elem_id = 'theme')
 
 
160
 
161
- b1 = gr.Button("Create!", elem_id="generate-btn")
 
 
162
 
163
- story_output = gr.Textbox(label='')
 
164
 
165
- with gr.Row():
166
- b2 = gr.Button("Illustrate Story", elem_id="illustrated-btn")
167
-
168
 
169
- with gr.Row():
170
- illustration = gr.Image(label='Illustration')
171
-
172
-
173
- gr.HTML('<div style="text-align: center; max-width: 650px; margin: 0 auto;"><p style="margin-bottom: 10px; font-size: 94%;">Compute credits are expensive. Please help me keep this experiment running by buying me a coffee <a href="https://www.buymeacoffee.com/jrodolfoocG"> <u><b>here</u></b> :) </a></p></div><br>')
174
- gr.HTML('<div style="text-align: center; max-width: 650px; margin: 0 auto;"><p style="margin-bottom: 10px; font-size: 70%;">Built with GPT-3, Stable Diffusion, the Diffusers library and Gradio, by <a href="https://research.rodolfoocampo.com"><u><b>Rodolfo Ocampo</u></b></a></p></div>')
175
-
176
-
177
- b1.click(generateStory, inputs=[theme1,theme2], outputs=[story_output])
178
- b2.click(illustratedStory, inputs=[story_output, theme1, theme2], outputs=[illustration])
179
 
 
180
 
181
-
182
- demo.launch(debug=True)
 
 
1
  import numpy as np
2
+ import os
3
  import gradio as gr
4
  import openai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
 
 
7
 
8
+ def generateStory(theme1, theme2, teaching, read_time):
9
+ if read_time > 0:
10
+ word_counts = read_time*200
11
+ else:
12
+ read_time=1
13
+ word_counts = read_time*200
14
+ prompt_text = ""
15
+ if teaching == "No teaching":
16
+ prompt_text = "Write a long children's story that combines the following two topics: {} and {}. Your story should be appropriate, understandable and fun for children aged around 10 years old. Use descriptive language, dialogue and intricate details and vivid imagery to bring your the story life.".format(theme1, theme2)
17
+ elif teaching == "Open teaching":
18
+ prompt_text = "Write a long children's story that combines the following two topics: {} and {}. Your story should be appropriate, understandable and fun for children aged around 10 years old and should include a positive message or moral. Use descriptive language, dialogue and intricate details and vivid imagery to bring your the story life. ".format(theme1, theme2)
19
+ else:
20
+ prompt_text = "Write a long children's story that combines the following two topics: {} and {}. Your story should be appropriate, understandable and fun for children aged around 10 years old and should teach children about {}. Use descriptive language, dialogue and intricate details and vivid imagery to bring your the story life. ".format(theme1, theme2, teaching)
21
 
22
+ prompt_text += f" The story should be approximately {word_counts} words long, corresponding to approximately a {read_time} minute read."
23
+ prompt_text += "The story should have a tone similar to this: 'Once there was an evil wizard who made a mirror with his dark magic. If anything good or beautiful was put in front of the mirror, the reflection that showed back was only rotten and gray. The wizard laughed. He wanted to show his evil mirror to the whole world! He took it and flew up high into the sky.'"
24
+ openai.api_key = os.environ['OPENAI_KEY']
25
 
 
 
 
 
26
  response = openai.Completion.create(
27
+ engine="text-davinci-003",
28
+ prompt=prompt_text,
29
+ temperature=0.7,
30
+ max_tokens=2300,
31
+ top_p=1,
32
+ frequency_penalty=0,
33
+ presence_penalty=0
34
  )
35
  story = response["choices"][0]["text"]
36
 
37
  content_to_classify = "Your content here"
38
 
39
  response = openai.Completion.create(
40
+ model="content-filter-alpha",
41
+ prompt="{}\n--\nLabel:".format(story),
42
+ temperature=0,
43
+ max_tokens=1,
44
+ top_p=0,
45
+ logprobs=10
46
+ )
47
+
48
  output_label = response["choices"][0]["text"]
49
 
50
+
51
  # This is the probability at which we evaluate that a "2" is likely real
52
  # vs. should be discarded as a false positive
53
  toxic_threshold = -0.355
 
57
 
58
  if story.startswith('\n\n'):
59
  story = story[2:]
60
+ return story
 
 
 
 
 
61
 
62
  '''
63
  demo = gr.Interface(
 
74
 
75
 
76
  with gr.Blocks(css='''
77
+ .h1 {
78
+ font-family: HK Grotesk;
79
+ font-style: normal;
80
+ font-weight: bold;
81
+ font-size: 100px;
82
+ line-height: 105%;
83
+ margin: 0;
84
+ }
85
+ ''') as demo:
86
+ title = gr.HTML('''
87
+ <div style="text-align: center; margin: 0;">
88
+ <div style="
89
+ display: inline-flex;
90
+ align-items: center;
91
+ gap: 0.8rem;
92
+ font-size: 1.75rem;
93
+ ">
 
 
 
 
94
  <h1 style="font-weight: 900; margin-bottom: 7px;">
95
+ Infinite Stories
96
  </h1>
 
 
 
 
 
 
 
97
  </div>
98
+ <p style="margin-bottom: 10px; font-size: 94%;">
99
+ </p>
100
+ <br>
101
+
102
+ </div>
103
+ ''')
104
 
105
+ with gr.Row():
106
+ theme1 = gr.Textbox(label='Seed 1', elem_id='theme')
107
+ theme2 = gr.Textbox(label='Seed 2', elem_id='theme')
108
 
109
+ theme_options = ["No teaching", "Open teaching", "Gratitude", "The value of friendship", "Caring for the planet", "Believing in yourself", "Being brave", "Choosing to do the right thing", "Accepting people different than us", "Being compassionate"]
110
+ theme_dropdown = gr.Dropdown(choices=theme_options, label="Moral")
111
 
112
+ slider = gr.Slider(minimum=0, maximum=10, step=5, label="Approximate reading time", default=5)
 
 
113
 
114
+ b1 = gr.Button("Create!", elem_id="generate-btn")
 
 
 
 
 
 
 
 
 
115
 
116
+ story_output = gr.Textbox(label='Story')
117
 
118
+ b1.click(generateStory, inputs=[theme1, theme2, theme_dropdown, slider], outputs=[story_output])
119
+
120
+ demo.launch(debug=True, share=True)