rodolfoocampo commited on
Commit
3a159e9
1 Parent(s): c095115

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def generateStory(theme1, theme2):
2
+ openai.api_key = {API_KEY}
3
+ prompt_text = "Write a children's story about \"{}\" and \"{}\".".format(theme1,theme2)
4
+ response = openai.Completion.create(
5
+ engine="text-davinci-003",
6
+ prompt=prompt_text,
7
+ temperature=0.7,
8
+ max_tokens=600,
9
+ top_p=1,
10
+ frequency_penalty=0,
11
+ presence_penalty=0
12
+ )
13
+ story = response["choices"][0]["text"]
14
+
15
+ content_to_classify = "Your content here"
16
+
17
+ response = openai.Completion.create(
18
+ model="content-filter-alpha",
19
+ prompt = "<|endoftext|>"+story+"\n--\nLabel:",
20
+ temperature=0,
21
+ max_tokens=1,
22
+ top_p=0,
23
+ logprobs=10
24
+ )
25
+
26
+ output_label = response["choices"][0]["text"]
27
+
28
+ # This is the probability at which we evaluate that a "2" is likely real
29
+ # vs. should be discarded as a false positive
30
+ toxic_threshold = -0.355
31
+
32
+ if output_label == "2":
33
+ story='Please generate again'
34
+
35
+ if story.startswith('\n\n'):
36
+ story = story[2:]
37
+ return story
38
+
39
+
40
+ def illustratedStory(story):
41
+
42
+ if story != 'Please generate again':
43
+
44
+ illustration_response = openai.Completion.create(
45
+ model="text-davinci-002",
46
+ prompt="Write a visual description of an illustration that could accompany the following story: '{}'".format(story),
47
+ temperature=0.7,
48
+ max_tokens=256,
49
+ top_p=1,
50
+ frequency_penalty=0,
51
+ presence_penalty=0
52
+ )
53
+
54
+ image_prompt = illustration_response["choices"][0]["text"]
55
+ print(image_prompt)
56
+ response = openai.Image.create(
57
+
58
+ prompt="A cartoon of: " + image_prompt,
59
+ n=1,
60
+ size="1024x1024"
61
+ )
62
+ image_url = response['data'][0]['url']
63
+
64
+ else:
65
+ image = np.zeros([100,100,3],dtype=np.uint8)
66
+ image.fill(255) # or img[:] = 255
67
+
68
+ return image_url
69
+
70
+ def continueStory(inputStory):
71
+ openai.api_key = "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"
72
+ prompt_text = inputStory
73
+ response = openai.Completion.create(
74
+ engine="text-davinci-002",
75
+ prompt=prompt_text,
76
+ temperature=0.7,
77
+ max_tokens=250,
78
+ top_p=1,
79
+ frequency_penalty=0,
80
+ presence_penalty=0
81
+ )
82
+ story = response["choices"][0]["text"]
83
+
84
+ content_to_classify = "Your content here"
85
+
86
+ response = openai.Completion.create(
87
+ model="content-filter-alpha",
88
+ prompt = "<|endoftext|>"+story+"\n--\nLabel:",
89
+ temperature=0,
90
+ max_tokens=1,
91
+ top_p=0,
92
+ logprobs=10
93
+ )
94
+
95
+ output_label = response["choices"][0]["text"]
96
+
97
+ # This is the probability at which we evaluate that a "2" is likely real
98
+ # vs. should be discarded as a false positive
99
+ toxic_threshold = -0.355
100
+
101
+ if output_label == "2":
102
+ story='Please generate again'
103
+
104
+ if story.startswith('\n\n'):
105
+ story = story[2:]
106
+ return inputStory + story
107
+
108
+ def downloadStory(story):
109
+ with open("output.txt", "a") as f:
110
+ print("Hello stackoverflow!", file=f)
111
+ print("I have a question.", file=f)
112
+
113
+ '''
114
+ demo = gr.Interface(
115
+ fn=themes,
116
+
117
+
118
+ inputs=["text", "text"],
119
+ outputs=["text", "image"],
120
+ )
121
+
122
+ demo.launch()
123
+ '''
124
+
125
+
126
+
127
+ with gr.Blocks(css='''
128
+ .h1 {
129
+
130
+ font-family: HK Grotesk;
131
+ font-style: normal;
132
+ font-weight: bold;
133
+ font-size: 100px;
134
+ line-height: 105%;
135
+ margin: 0;
136
+ }
137
+
138
+ ''') as demo:
139
+ title = gr.HTML(
140
+ """
141
+ <div style="text-align: center; margin: 0;">
142
+ <div style="
143
+ display: inline-flex;
144
+ align-items: center;
145
+ gap: 0.8rem;
146
+ font-size: 1.75rem;
147
+ ">
148
+
149
+ <h1 style="font-weight: 900; margin-bottom: 7px;">
150
+ Infinite Stories
151
+ </h1>
152
+ </div>
153
+ <p style="margin-bottom: 10px; font-size: 94%;">
154
+
155
+ </p>
156
+ <br>
157
+ <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>
158
+
159
+ </div>
160
+ """)
161
+ with gr.Row():
162
+ theme1 = gr.Textbox(label='Theme 1', elem_id = 'theme')
163
+ theme2 = gr.Textbox(label='Theme 2', elem_id = 'theme')
164
+
165
+ b1 = gr.Button("Generate starting paragraph", elem_id="generate-btn")
166
+
167
+ story_output = gr.Textbox(label='Story (pro tip: you can edit this! Shift + enter for a new line)')
168
+
169
+ with gr.Row():
170
+
171
+ b3 = gr.Button("Continue Story", elem_id="continue-btn")
172
+ b2 = gr.Button("Illustrate Story", elem_id="illustrated-btn")
173
+
174
+
175
+ with gr.Row():
176
+ illustration = gr.Image(label='Illustration')
177
+
178
+
179
+ 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>')
180
+ 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>')
181
+
182
+
183
+ b1.click(generateStory, inputs=[theme1,theme2], outputs=[story_output])
184
+ b2.click(illustratedStory, inputs=[story_output], outputs=[illustration])
185
+
186
+ b3.click(continueStory, inputs=[story_output], outputs=[story_output])
187
+
188
+
189
+ demo.launch(debug=True, share=True)