Spaces:
Runtime error
Runtime error
rodolfoocampo
commited on
Commit
•
d5c8a18
1
Parent(s):
a1fd363
Update app.py
Browse files
app.py
CHANGED
@@ -147,18 +147,39 @@ with gr.Blocks(css='''
|
|
147 |
|
148 |
def generateStory(character):
|
149 |
|
150 |
-
openai.api_key = openai.api_key = os.environ['OPENAI_KEY']
|
151 |
-
|
152 |
messageHistory=[
|
153 |
-
|
154 |
-
|
155 |
-
|
|
|
|
|
|
|
|
|
156 |
response = openai.ChatCompletion.create(
|
157 |
model="gpt-3.5-turbo",
|
158 |
messages = messageHistory
|
159 |
)
|
160 |
|
161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
messageHistory.append(response["choices"][0]["message"])
|
163 |
|
164 |
return story, messageHistory
|
|
|
147 |
|
148 |
def generateStory(character):
|
149 |
|
|
|
|
|
150 |
messageHistory=[
|
151 |
+
{"role": "system", "content": "You are a helpful assistant for storytelling-driven games that writes in the style of Doctor Seuss and Julia Donaldson."},
|
152 |
+
{"role": "system", "name":"example_user", "content": "We are going to engage in a collaborative storytelling game. You are going to generate a paragraph but you are going to leave a blank in the last word, that I will have to complete in order to generate the next paragraph. We will do this for every paragraph. Please generate the first paragraph. The story is about a {}. Write in the style of Julia Donaldson and Doctor Seuss. Remember, just write one paragraph! After this one paragraph is generated, I will tell you the word that I want to use to fill the blank, and then you can generate the next. 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. I tell you how I want to fill the blank. 3. You generate the next paragraph based on my response to continue the story, this next paragraph also has a blank somewhere in the last line of the paragraph. 4. I tell you how to fill the blank. And so on."},
|
153 |
+
{"role": "system", "name": "example_assistant", "content": "In a land of green hills and blue sky,\n Lived a boy named Jack, oh my oh my!\n He loved to explore and roam,\n From the woods to the sea foam.\n He'd climb up trees and run down hills,\n Watch the water flow and the flowers spill.\n But his favorite place, as you can see,\n Was the forest where he felt so free.\n One day, Jack set out to _____."},
|
154 |
+
{"role": "system", "name":"example_user", "content": "{A cave} Write the next paragraph and leave a blank at the end for me to fill!"},
|
155 |
+
{"role": "system", "name": "example_assistant", "content": "Jack had heard about a cave,\n Where treasure was hidden like in a rave.\n He packed some food and a flashlight,\n And set off, with all his might.\n The forest was thick and the way was rough,\n But Jack kept going, feeling tough.\n He crossed a stream and climbed a hill,\n Until he saw the cave, so still.\n The entrance was dark, but Jack was brave,\n He took a deep breath and entered the cave.\n Deep inside, he saw something ____."},
|
156 |
+
{"role": "user", "content": f"We are going to engage in a collaborative storytelling game. You are going to generate a paragraph but you are going to leave a blank in the last word, that I will have to complete in order to generate the next paragraph. We will do this for every paragraph. Please generate the first paragraph. The story is about {character}. Write in the style of Julia Donaldson and Doctor Seuss. Remember, just write one paragraph! After this one paragraph is generated, I will tell you the word that I want to use to fill the blank, and then you can generate the next. 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. I tell you how I want to fill the blank. 3. You generate the next paragraph based on my response to continue the story, this next paragraph also has a blank somewhere in the last line of the paragraph. 4. I tell you how to fill the blank. And so on."}
|
157 |
+
]
|
158 |
response = openai.ChatCompletion.create(
|
159 |
model="gpt-3.5-turbo",
|
160 |
messages = messageHistory
|
161 |
)
|
162 |
|
163 |
+
# check response and generate until you get desired behavior
|
164 |
+
s = response["choices"][0]["message"]["content"]
|
165 |
+
if (s.endswith("_.") or s.endswith("_")) and ("_" not in s[:-2].rstrip("_")):
|
166 |
+
story = s
|
167 |
+
else:
|
168 |
+
response = openai.ChatCompletion.create(
|
169 |
+
model="gpt-3.5-turbo",
|
170 |
+
messages = messageHistory
|
171 |
+
)
|
172 |
+
s = response["choices"][0]["message"]["content"]
|
173 |
+
if (s.endswith("_.") or s.endswith("_")) and ("_" not in s[:-2].rstrip("_")):
|
174 |
+
story = s
|
175 |
+
else:
|
176 |
+
response = openai.ChatCompletion.create(
|
177 |
+
model="gpt-3.5-turbo",
|
178 |
+
messages = messageHistory
|
179 |
+
)
|
180 |
+
story = response["choices"][0]["message"]["content"]
|
181 |
+
|
182 |
+
|
183 |
messageHistory.append(response["choices"][0]["message"])
|
184 |
|
185 |
return story, messageHistory
|