Spaces:
Runtime error
Runtime error
rodolfoocampo
commited on
Commit
•
1ecf223
1
Parent(s):
e6630ec
Update app.py
Browse files
app.py
CHANGED
@@ -5,116 +5,136 @@ import openai
|
|
5 |
|
6 |
|
7 |
|
8 |
-
def generateStory(
|
9 |
-
|
10 |
-
word_counts = read_time*200
|
11 |
-
else:
|
12 |
-
read_time=1
|
13 |
-
word_counts = read_time*200
|
14 |
-
prompt_text = ""
|
15 |
-
if teaching == "No teaching":
|
16 |
-
prompt_text = "Write a long children's story that combines the following two topics: {} and {}. Your story should be appropriate, understandable and fun for children aged around 10 years old. Use descriptive language, dialogue and intricate details and vivid imagery to bring your the story life.".format(theme1, theme2)
|
17 |
-
elif teaching == "Open teaching":
|
18 |
-
prompt_text = "Write a long children's story that combines the following two topics: {} and {}. Your story should be appropriate, understandable and fun for children aged around 10 years old and should include a positive message or moral. Use descriptive language, dialogue and intricate details and vivid imagery to bring your the story life. ".format(theme1, theme2)
|
19 |
-
else:
|
20 |
-
prompt_text = "Write a long children's story that combines the following two topics: {} and {}. Your story should be appropriate, understandable and fun for children aged around 10 years old and should teach children about {}. Use descriptive language, dialogue and intricate details and vivid imagery to bring your the story life. ".format(theme1, theme2, teaching)
|
21 |
-
|
22 |
-
prompt_text += f" The story should be approximately {word_counts} words long, corresponding to approximately a {read_time} minute read."
|
23 |
-
prompt_text += "The story should have a tone similar to this: 'Once there was an evil wizard who made a mirror with his dark magic. If anything good or beautiful was put in front of the mirror, the reflection that showed back was only rotten and gray. The wizard laughed. He wanted to show his evil mirror to the whole world! He took it and flew up high into the sky.'"
|
24 |
-
|
25 |
openai.api_key = os.environ['OPENAI_KEY']
|
26 |
-
response = openai.Completion.create(
|
27 |
-
engine="text-davinci-003",
|
28 |
-
prompt=prompt_text,
|
29 |
-
temperature=0.7,
|
30 |
-
max_tokens=2300,
|
31 |
-
top_p=1,
|
32 |
-
frequency_penalty=0,
|
33 |
-
presence_penalty=0
|
34 |
-
)
|
35 |
-
story = response["choices"][0]["text"]
|
36 |
-
|
37 |
-
content_to_classify = "Your content here"
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
)
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
output_label = response["choices"][0]["text"]
|
49 |
-
|
50 |
-
|
51 |
-
# This is the probability at which we evaluate that a "2" is likely real
|
52 |
-
# vs. should be discarded as a false positive
|
53 |
-
toxic_threshold = -0.355
|
54 |
|
55 |
-
|
56 |
-
story='Please generate again'
|
57 |
|
58 |
-
|
59 |
-
story = story[2:]
|
60 |
-
return story
|
61 |
|
62 |
-
|
63 |
-
demo = gr.Interface(
|
64 |
-
fn=themes,
|
65 |
|
|
|
|
|
|
|
|
|
66 |
|
67 |
-
|
68 |
-
|
69 |
-
)
|
70 |
-
|
71 |
-
demo.launch()
|
72 |
-
'''
|
73 |
-
|
74 |
|
75 |
|
76 |
with gr.Blocks(css='''
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
''') as demo:
|
86 |
title = gr.HTML('''
|
87 |
-
<div style="text-align: center;
|
88 |
-
<
|
89 |
-
|
90 |
-
align-items: center;
|
91 |
-
gap: 0.8rem;
|
92 |
-
font-size: 1.75rem;
|
93 |
-
">
|
94 |
-
<h1 style="font-weight: 900; margin-bottom: 7px;">
|
95 |
-
Infinite Stories
|
96 |
-
</h1>
|
97 |
-
</div>
|
98 |
-
<p style="margin-bottom: 10px; font-size: 94%;">
|
99 |
-
</p>
|
100 |
-
<br>
|
101 |
-
|
102 |
</div>
|
103 |
''')
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
theme_options = ["No teaching", "Open teaching", "Gratitude", "The value of friendship", "Caring for the planet", "Believing in yourself", "Being brave", "Choosing to do the right thing", "Accepting people different than us", "Being compassionate"]
|
110 |
-
theme_dropdown = gr.Dropdown(choices=theme_options, label="Moral")
|
111 |
-
|
112 |
-
slider = gr.Slider(minimum=0, maximum=10, step=5, label="Approximate reading time", default=5)
|
113 |
|
114 |
-
b1 = gr.Button("
|
115 |
|
116 |
story_output = gr.Textbox(label='Story')
|
117 |
|
118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
|
|
|
|
|
|
120 |
demo.launch(debug=True, share=False)
|
|
|
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",
|
35 |
+
messages = messageHistory
|
36 |
+
)
|
37 |
|
38 |
+
story = response["choices"][0]["message"]["content"]
|
39 |
+
messageHistory.append(response["choices"][0]["message"])
|
40 |
+
print(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;
|
50 |
+
background-image: url('file=https://cdn.discordapp.com/attachments/941582479117127680/1080730421425344542/rodotcom_colorful_abstract_illustration_for_the_background_of_c_f1b331d7-6493-4a33-9063-345d31a66ddb.png');
|
51 |
+
padding: 1rem;
|
52 |
+
}
|
53 |
+
|
54 |
+
h1 {
|
55 |
+
font-size: 3rem;
|
56 |
+
font-weight: 700;
|
57 |
+
margin-bottom: 1rem;
|
58 |
+
color: white;
|
59 |
+
}
|
60 |
+
|
61 |
+
p {
|
62 |
+
font-size: 1.25rem;
|
63 |
+
margin-bottom: 2rem;
|
64 |
+
color: white;
|
65 |
+
}
|
66 |
+
|
67 |
+
.gradio-row {
|
68 |
+
width: 100%;
|
69 |
+
margin-bottom: 1.5rem;
|
70 |
+
}
|
71 |
+
|
72 |
+
label {
|
73 |
+
font-size: 1.25rem;
|
74 |
+
font-weight: 500;
|
75 |
+
margin-bottom: 0.5rem;
|
76 |
+
}
|
77 |
+
|
78 |
+
input[type="text"], textarea {
|
79 |
+
font-size: 1.25rem;
|
80 |
+
padding: 0.5rem;
|
81 |
+
border: 2px solid #ccc;
|
82 |
+
border-radius: 5px;
|
83 |
+
width: 100%;
|
84 |
+
}
|
85 |
+
|
86 |
+
.gradio-button {
|
87 |
+
background-color: #2196f3;
|
88 |
+
color: #fff;
|
89 |
+
font-size: 1.25rem;
|
90 |
+
font-weight: 500;
|
91 |
+
padding: 0.75rem 1.5rem;
|
92 |
+
border-radius: 5px;
|
93 |
+
cursor: pointer;
|
94 |
+
transition: all 0.2s ease-in-out;
|
95 |
+
}
|
96 |
+
|
97 |
+
.gradio-button:hover {
|
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)
|