rodolfoocampo commited on
Commit
14d2ae1
1 Parent(s): c555193

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -63
app.py CHANGED
@@ -95,7 +95,58 @@ with gr.Blocks(css='''
95
  </div>
96
  ''')
97
 
98
- with gr.Column(elem_id="main_col") as main_col:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
 
101
  character = gr.Textbox(label='Who will be the main character?', elem_id = 'theme', placeholder="i.e. Diego, an 11-year-old pirate")
@@ -134,69 +185,18 @@ with gr.Blocks(css='''
134
  messageHistoryTextBox = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank', value=messageHistory)
135
 
136
 
137
- # the submit feedback btn needs to: send the feedback,
138
- # hide the elements and provide a message saying thanks for your feedback
139
-
140
- def show_feedback_text():
141
- return {feedbackForm: gr.update(visible=True)}
142
-
143
- def submitFeedback():
144
- return {thankyou: gr.update(visible=True), feedbackForm: gr.update(visible=False)}
145
-
146
- def generateStory(character):
147
-
148
- openai.api_key = os.environ['OPENAI_KEY']
149
-
150
- messageHistory=[
151
- {"role": "system", "content": "You are co-writing assistant that will help me generate an interactive storytelling game. You are going to generate one paragraph at a time but you are going to leave a blank somewhere in the last line of the paragraph. The user will have to fill this blank in order to generate the next paragraph. You have to write in the style of Julia Donaldson and Doctor Seuss. Make the story funny and exciting. For more context, the game works as follows. 1. You generate one, and only one paragraph, with a blank somewhere in the last line of the paragraph. 2. The user tells you how they want to fill the blank. 3. You generate the next paragraph based on the user’s response to continue the story. This next paragraph also has a blank somewhere in the last line of the paragraph. 4. The user tells you how they want to fill the blank. And so on. Please make sure that when you write a paragraph, there is only one blank and it is at the end of the last line."},
152
- {"role": "user", "content": f"Please write the first paragraph of a story about {character}. The story needs to be set in a magic island. Make sure you only write one paragraph, that is not too long. Also, make sure is has a blank in the last line of the last paragraph for me to fill. There should only be one blank in the paragraph. The blank should be at the end of the last line of the paragraph. It is very important that there is only one blank and that it is at the end, and that you only generate one paragraph!"}
153
- ]
154
- response = openai.ChatCompletion.create(
155
- model="gpt-3.5-turbo",
156
- messages = messageHistory
157
- )
158
-
159
- story = response["choices"][0]["message"]["content"]
160
- messageHistory.append(response["choices"][0]["message"])
161
-
162
- return story, messageHistory
163
-
164
- def finishStory(blank, messageHistory):
165
-
166
- openai.api_key = os.environ['OPENAI_KEY']
167
-
168
- messageHistory.append({"role": "user", "content": blank + "\n\nNow bring the story to a close. Write the necessary paragraphs to make it have a happy or funny ending. No need to leave a blank anymore."})
169
-
170
-
171
- response = openai.ChatCompletion.create(
172
- model="gpt-3.5-turbo",
173
- messages = messageHistory
174
- )
175
-
176
- story = response["choices"][0]["message"]["content"]
177
- messageHistory.append(response["choices"][0]["message"])
178
- print(messageHistory)
179
- return story, messageHistory
180
-
181
- def createNewStory():
182
- return {character: gr.update(value=""), story_output: gr.update(value="")}
183
-
184
- def hideContinueBtn():
185
- return {continueStoryCol: gr.update(visible=False), newStoryCol: gr.update(visible=True), finishCol: gr.update(visible=False)}
186
 
187
-
188
- beginBtn.click(generateStory, inputs=[character], outputs=[story_output, messageHistory])
189
-
190
- continueBtn.click(continueStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
191
-
192
- finishBtn.click(finishStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
193
- finishBtn.click(hideContinueBtn, [], [continueStoryCol, newStoryCol, finishCol])
194
-
195
- provideFeedbackBtn.click(show_feedback_text, [], [feedbackForm])
196
-
197
- submitFeedbackBtn.click(submitFeedback, [], [thankyou, feedbackForm])
198
-
199
- newStoryBtn.click(createNewStory, [], [character, story_output])
200
 
201
 
202
  demo.launch(debug=True, share=False)
 
95
  </div>
96
  ''')
97
 
98
+ # the submit feedback btn needs to: send the feedback,
99
+ # hide the elements and provide a message saying thanks for your feedback
100
+
101
+ def show_feedback_text():
102
+ return {feedbackForm: gr.update(visible=True)}
103
+
104
+ def submitFeedback():
105
+ return {thankyou: gr.update(visible=True), feedbackForm: gr.update(visible=False)}
106
+
107
+ def generateStory(character):
108
+
109
+ openai.api_key = os.environ['OPENAI_KEY']
110
+
111
+ messageHistory=[
112
+ {"role": "system", "content": "You are co-writing assistant that will help me generate an interactive storytelling game. You are going to generate one paragraph at a time but you are going to leave a blank somewhere in the last line of the paragraph. The user will have to fill this blank in order to generate the next paragraph. You have to write in the style of Julia Donaldson and Doctor Seuss. Make the story funny and exciting. For more context, the game works as follows. 1. You generate one, and only one paragraph, with a blank somewhere in the last line of the paragraph. 2. The user tells you how they want to fill the blank. 3. You generate the next paragraph based on the user’s response to continue the story. This next paragraph also has a blank somewhere in the last line of the paragraph. 4. The user tells you how they want to fill the blank. And so on. Please make sure that when you write a paragraph, there is only one blank and it is at the end of the last line."},
113
+ {"role": "user", "content": f"Please write the first paragraph of a story about {character}. The story needs to be set in a magic island. Make sure you only write one paragraph, that is not too long. Also, make sure is has a blank in the last line of the last paragraph for me to fill. There should only be one blank in the paragraph. The blank should be at the end of the last line of the paragraph. It is very important that there is only one blank and that it is at the end, and that you only generate one paragraph!"}
114
+ ]
115
+ response = openai.ChatCompletion.create(
116
+ model="gpt-3.5-turbo",
117
+ messages = messageHistory
118
+ )
119
+
120
+ story = response["choices"][0]["message"]["content"]
121
+ messageHistory.append(response["choices"][0]["message"])
122
+
123
+ return story, messageHistory
124
+
125
+ def finishStory(blank, messageHistory):
126
+
127
+ openai.api_key = os.environ['OPENAI_KEY']
128
+
129
+ messageHistory.append({"role": "user", "content": blank + "\n\nNow bring the story to a close. Write the necessary paragraphs to make it have a happy or funny ending. No need to leave a blank anymore."})
130
+
131
+
132
+ response = openai.ChatCompletion.create(
133
+ model="gpt-3.5-turbo",
134
+ messages = messageHistory
135
+ )
136
+
137
+ story = response["choices"][0]["message"]["content"]
138
+ messageHistory.append(response["choices"][0]["message"])
139
+ print(messageHistory)
140
+ return story, messageHistory
141
+
142
+ def createNewStory():
143
+ return {character: gr.update(value=""), story_output: gr.update(value="")}
144
+
145
+ def hideContinueBtn():
146
+ return {continueStoryCol: gr.update(visible=False), newStoryCol: gr.update(visible=True), finishCol: gr.update(visible=False)}
147
+
148
+
149
+ with gr.Column(elem_id="main_col"):
150
 
151
 
152
  character = gr.Textbox(label='Who will be the main character?', elem_id = 'theme', placeholder="i.e. Diego, an 11-year-old pirate")
 
185
  messageHistoryTextBox = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank', value=messageHistory)
186
 
187
 
188
+ beginBtn.click(generateStory, inputs=[character], outputs=[story_output, messageHistory])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
 
190
+ continueBtn.click(continueStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
191
+
192
+ finishBtn.click(finishStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
193
+ finishBtn.click(hideContinueBtn, [], [continueStoryCol, newStoryCol, finishCol])
194
+
195
+ provideFeedbackBtn.click(show_feedback_text, [], [feedbackForm])
196
+
197
+ submitFeedbackBtn.click(submitFeedback, [], [thankyou, feedbackForm])
198
+
199
+ newStoryBtn.click(createNewStory, [], [character, story_output])
 
 
 
200
 
201
 
202
  demo.launch(debug=True, share=False)