rodolfoocampo commited on
Commit
61f86b3
1 Parent(s): 1ecf223

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -39
app.py CHANGED
@@ -4,31 +4,12 @@ import gradio as gr
4
  import openai
5
 
6
 
7
-
8
- def generateStory(character):
9
-
10
- openai.api_key = os.environ['OPENAI_KEY']
11
-
12
- messageHistory=[
13
- {"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."},
14
- {"role": "user", "content": f"Please write the first paragraph of a story about {character}. The story needs to be set in a magic island."}
15
- ]
16
- response = openai.ChatCompletion.create(
17
- model="gpt-3.5-turbo",
18
- messages = messageHistory
19
- )
20
-
21
- story = response["choices"][0]["message"]["content"]
22
- messageHistory.append(response["choices"][0]["message"])
23
-
24
- return story, messageHistory
25
-
26
-
27
  def continueStory(blank, messageHistory):
28
 
29
  openai.api_key = os.environ['OPENAI_KEY']
30
 
31
- messageHistory.append({"role": "user", "content": blank + "\n\nGenerate the next paragraph with a blank in the last line for me to fill."})
 
32
 
33
  response = openai.ChatCompletion.create(
34
  model="gpt-3.5-turbo",
@@ -41,9 +22,12 @@ def continueStory(blank, messageHistory):
41
  return story, messageHistory
42
 
43
 
 
 
44
  with gr.Blocks(css='''
45
  .gradio-container {
46
  display: flex;
 
47
  flex-direction: column;
48
  justify-content: center;
49
  align-items: center;
@@ -98,43 +82,114 @@ with gr.Blocks(css='''
98
  background-color: #0c7cd5;
99
  }
100
 
101
- .gradio-textbox {
102
- font-size: 1.25rem;
103
- padding: 0.5rem;
104
- border: 2px solid #ccc;
105
- border-radius: 5px;
106
- width: 100%;
107
- height: 200px;
108
- resize: none;
109
- }
110
 
111
  ''') as demo:
112
  title = gr.HTML('''
113
  <div style="text-align: center;">
114
  <h1 style="color: white; font-size: 3rem; font-weight: 700; margin-bottom: 1rem; color: white;">Infinite Stories</h1>
115
- <p style="color: white; font-size: 1.25rem; margin-bottom: 2rem; color: white;">Enter a character name and let the AI generate a story for you!</p>
116
  </div>
117
  ''')
118
  with gr.Row():
119
- character = gr.Textbox(label='Character', elem_id = 'theme')
120
 
121
  messageHistory = gr.State()
122
 
123
- b1 = gr.Button("Generate!", elem_id="generate-btn")
124
 
125
  story_output = gr.Textbox(label='Story')
126
 
127
  blank = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank')
128
 
129
- b3 = gr.Button("Continue Story", elem_id="continue-btn")
130
-
131
-
132
-
 
 
 
 
133
 
134
- b1.click(generateStory, inputs=[character], outputs=[story_output, messageHistory])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
 
 
136
 
137
- b3.click(continueStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  demo.launch(debug=True, share=False)
 
4
  import openai
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def continueStory(blank, messageHistory):
8
 
9
  openai.api_key = os.environ['OPENAI_KEY']
10
 
11
+ messageHistory.append({"role": "user", "content": blank + "\n\nGenerate the next paragraph with a blank in the last line for me to fill. There should only be one and only one blank in the paragraph. The blank should be at the end of the last line of the paragraph. It is very importat that there is only one blank and that it is at the end!"})
12
+
13
 
14
  response = openai.ChatCompletion.create(
15
  model="gpt-3.5-turbo",
 
22
  return story, messageHistory
23
 
24
 
25
+
26
+
27
  with gr.Blocks(css='''
28
  .gradio-container {
29
  display: flex;
30
+ width: 50%;
31
  flex-direction: column;
32
  justify-content: center;
33
  align-items: center;
 
82
  background-color: #0c7cd5;
83
  }
84
 
85
+
 
 
 
 
 
 
 
 
86
 
87
  ''') as demo:
88
  title = gr.HTML('''
89
  <div style="text-align: center;">
90
  <h1 style="color: white; font-size: 3rem; font-weight: 700; margin-bottom: 1rem; color: white;">Infinite Stories</h1>
91
+ <p style="color: white; font-size: 1.25rem; margin-bottom: 2rem; color: white;">Let's see where your imagination can take you!</p>
92
  </div>
93
  ''')
94
  with gr.Row():
95
+ character = gr.Textbox(label='Who will be the main character?', elem_id = 'theme', placeholder="i.e. Diego, an 11-year-old pirate")
96
 
97
  messageHistory = gr.State()
98
 
99
+ beginBtn = gr.Button("Start!", elem_id="generate-btn")
100
 
101
  story_output = gr.Textbox(label='Story')
102
 
103
  blank = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank')
104
 
105
+ with gr.Column(visible=True) as continueStoryCol:
106
+ continueBtn = gr.Button("Continue Story", elem_id="continue-btn")
107
+
108
+ with gr.Column(visible=False) as newStoryCol:
109
+ newStoryBtn = gr.Button("Create New Story", elem_id="new-story-btn")
110
+
111
+ with gr.Column(visible=True) as finishCol:
112
+ finishBtn = gr.Button("Finish Story", elem_id="finish-btn")
113
 
114
+ provideFeedbackBtn = gr.Button("Provide Feedback", elem_id="feedback-btn")
115
+
116
+ with gr.Column(visible=False) as feedbackForm:
117
+ gr.Radio(["1", "2", "3", "4", "5"], label="How would you rate the story")
118
+ feedback_text = gr.Textbox(label='Any other comments?')
119
+ submitFeedbackBtn = gr.Button("Submit Feedback", elem_id="submit-feedback-btn")
120
+
121
+ with gr.Column(visible=False) as thankyou:
122
+
123
+ feedback_text = gr.HTML("<h2>Thanks for your feeback!</h2>")
124
+
125
+ with gr.Column(visible=False) as mesHistoryCol:
126
+
127
+ messageHistoryTextBox = gr.Textbox(label='How will you fill the blank?', elem_id = 'blank', value=messageHistory)
128
+
129
+
130
+ # the submit feedback btn needs to: send the feedback,
131
+ # hide the elements and provide a message saying thanks for your feedback
132
 
133
+ def show_feedback_text():
134
+ return {feedbackForm: gr.update(visible=True)}
135
 
136
+ def submitFeedback():
137
+ return {thankyou: gr.update(visible=True), feedbackForm: gr.update(visible=False)}
138
+
139
+ def generateStory(character):
140
+
141
+ openai.api_key = os.environ['OPENAI_KEY']
142
+
143
+ messageHistory=[
144
+ {"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."},
145
+ {"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!"}
146
+ ]
147
+ response = openai.ChatCompletion.create(
148
+ model="gpt-3.5-turbo",
149
+ messages = messageHistory
150
+ )
151
+
152
+ story = response["choices"][0]["message"]["content"]
153
+ messageHistory.append(response["choices"][0]["message"])
154
 
155
+ return story, messageHistory
156
+
157
+ def finishStory(blank, messageHistory):
158
+
159
+ openai.api_key = os.environ['OPENAI_KEY']
160
+
161
+ 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."})
162
+
163
+
164
+ response = openai.ChatCompletion.create(
165
+ model="gpt-3.5-turbo",
166
+ messages = messageHistory
167
+ )
168
+
169
+ story = response["choices"][0]["message"]["content"]
170
+ messageHistory.append(response["choices"][0]["message"])
171
+ print(messageHistory)
172
+ return story, messageHistory
173
+
174
+ def createNewStory():
175
+ return {character: gr.update(value=""), story_output: gr.update(value="")}
176
+
177
+ def hideContinueBtn():
178
+ return {continueStoryCol: gr.update(visible=False), newStoryCol: gr.update(visible=True), finishCol: gr.update(visible=False)}
179
+
180
+
181
+ beginBtn.click(generateStory, inputs=[character], outputs=[story_output, messageHistory])
182
 
183
+ continueBtn.click(continueStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
184
+
185
+ finishBtn.click(finishStory, inputs=[blank, messageHistory], outputs=[story_output, messageHistory])
186
+ finishBtn.click(hideContinueBtn, [], [continueStoryCol, newStoryCol, finishCol])
187
+
188
+ provideFeedbackBtn.click(show_feedback_text, [], [feedbackForm])
189
+
190
+ submitFeedbackBtn.click(submitFeedback, [], [thankyou, feedbackForm])
191
+
192
+ newStoryBtn.click(createNewStory, [], [character, story_output])
193
+
194
+
195
  demo.launch(debug=True, share=False)