rodolfoocampo commited on
Commit
c555193
1 Parent(s): 6f00058

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -94
app.py CHANGED
@@ -81,6 +81,10 @@ with gr.Blocks(css='''
81
  background-color: #0c7cd5;
82
  }
83
 
 
 
 
 
84
 
85
 
86
  ''') as demo:
@@ -90,105 +94,109 @@ with gr.Blocks(css='''
90
  <p style="color: white; font-size: 1.25rem; margin-bottom: 2rem; color: white;">Let's see where your imagination can take you!</p>
91
  </div>
92
  ''')
93
- with gr.Row(elem_id="row"):
94
- character = gr.Textbox(label='Who will be the main character?', elem_id = 'theme', placeholder="i.e. Diego, an 11-year-old pirate")
95
-
96
- messageHistory = gr.State()
97
-
98
- beginBtn = gr.Button("Start!", elem_id="generate-btn")
99
-
100
- story_output = gr.Textbox(label='Story')
101
-
102
- blank = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank')
103
-
104
- with gr.Column(visible=True) as continueStoryCol:
105
- continueBtn = gr.Button("Continue Story", elem_id="continue-btn")
106
-
107
- with gr.Column(visible=False) as newStoryCol:
108
- newStoryBtn = gr.Button("Create New Story", elem_id="new-story-btn")
109
 
110
- with gr.Column(visible=True) as finishCol:
111
- finishBtn = gr.Button("Finish Story", elem_id="finish-btn")
 
 
112
 
113
- provideFeedbackBtn = gr.Button("Provide Feedback", elem_id="feedback-btn")
114
-
115
- with gr.Column(visible=False) as feedbackForm:
116
- gr.Radio(["1", "2", "3", "4", "5"], label="How would you rate the story")
117
- feedback_text = gr.Textbox(label='Any other comments?')
118
- submitFeedbackBtn = gr.Button("Submit Feedback", elem_id="submit-feedback-btn")
119
-
120
- with gr.Column(visible=False) as thankyou:
121
-
122
- feedback_text = gr.HTML("<h2>Thanks for your feeback!</h2>")
123
-
124
- with gr.Column(visible=False) as mesHistoryCol:
125
-
126
- messageHistoryTextBox = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank', value=messageHistory)
127
-
128
-
129
- # the submit feedback btn needs to: send the feedback,
130
- # hide the elements and provide a message saying thanks for your feedback
131
 
132
- def show_feedback_text():
133
- return {feedbackForm: gr.update(visible=True)}
134
-
135
- def submitFeedback():
136
- return {thankyou: gr.update(visible=True), feedbackForm: gr.update(visible=False)}
137
-
138
- def generateStory(character):
139
-
140
- openai.api_key = os.environ['OPENAI_KEY']
141
-
142
- messageHistory=[
143
- {"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."},
144
- {"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!"}
145
- ]
146
- response = openai.ChatCompletion.create(
147
- model="gpt-3.5-turbo",
148
- messages = messageHistory
149
- )
150
-
151
- story = response["choices"][0]["message"]["content"]
152
- messageHistory.append(response["choices"][0]["message"])
153
 
154
- return story, messageHistory
155
-
156
- def finishStory(blank, messageHistory):
157
-
158
- openai.api_key = os.environ['OPENAI_KEY']
159
-
160
- 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."})
161
-
162
-
163
- response = openai.ChatCompletion.create(
164
- model="gpt-3.5-turbo",
165
- messages = messageHistory
166
- )
167
-
168
- story = response["choices"][0]["message"]["content"]
169
- messageHistory.append(response["choices"][0]["message"])
170
- print(messageHistory)
171
- return story, messageHistory
172
-
173
- def createNewStory():
174
- return {character: gr.update(value=""), story_output: gr.update(value="")}
175
-
176
- def hideContinueBtn():
177
- return {continueStoryCol: gr.update(visible=False), newStoryCol: gr.update(visible=True), finishCol: gr.update(visible=False)}
178
-
179
-
180
- beginBtn.click(generateStory, inputs=[character], outputs=[story_output, messageHistory])
181
 
182
- continueBtn.click(continueStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
183
-
184
- finishBtn.click(finishStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
185
- finishBtn.click(hideContinueBtn, [], [continueStoryCol, newStoryCol, finishCol])
186
-
187
- provideFeedbackBtn.click(show_feedback_text, [], [feedbackForm])
188
-
189
- submitFeedbackBtn.click(submitFeedback, [], [thankyou, feedbackForm])
190
-
191
- newStoryBtn.click(createNewStory, [], [character, story_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
 
194
  demo.launch(debug=True, share=False)
 
81
  background-color: #0c7cd5;
82
  }
83
 
84
+ #main_col {
85
+ margin: 0 auto;
86
+ }
87
+
88
 
89
 
90
  ''') as demo:
 
94
  <p style="color: white; font-size: 1.25rem; margin-bottom: 2rem; color: white;">Let's see where your imagination can take you!</p>
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")
102
 
103
+
104
+ messageHistory = gr.State()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
+ beginBtn = gr.Button("Start!", elem_id="generate-btn")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
 
108
+ story_output = gr.Textbox(label='Story')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
+ blank = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank')
111
+
112
+ with gr.Column(visible=True) as continueStoryCol:
113
+ continueBtn = gr.Button("Continue Story", elem_id="continue-btn")
114
+
115
+ with gr.Column(visible=False) as newStoryCol:
116
+ newStoryBtn = gr.Button("Create New Story", elem_id="new-story-btn")
117
+
118
+ with gr.Column(visible=True) as finishCol:
119
+ finishBtn = gr.Button("Finish Story", elem_id="finish-btn")
120
+
121
+ provideFeedbackBtn = gr.Button("Provide Feedback", elem_id="feedback-btn")
122
+
123
+ with gr.Column(visible=False) as feedbackForm:
124
+ gr.Radio(["1", "2", "3", "4", "5"], label="How would you rate the story")
125
+ feedback_text = gr.Textbox(label='Any other comments?')
126
+ submitFeedbackBtn = gr.Button("Submit Feedback", elem_id="submit-feedback-btn")
127
+
128
+ with gr.Column(visible=False) as thankyou:
129
+
130
+ feedback_text = gr.HTML("<h2>Thanks for your feeback!</h2>")
131
+
132
+ with gr.Column(visible=False) as mesHistoryCol:
133
+
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)