InfiniteStories / app.py
rodolfoocampo's picture
Update app.py
e6630ec
raw
history blame
4.57 kB
import numpy as np
import os
import gradio as gr
import openai
def generateStory(theme1, theme2, teaching, read_time):
if read_time > 0:
word_counts = read_time*200
else:
read_time=1
word_counts = read_time*200
prompt_text = ""
if teaching == "No teaching":
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)
elif teaching == "Open teaching":
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)
else:
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)
prompt_text += f" The story should be approximately {word_counts} words long, corresponding to approximately a {read_time} minute read."
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.'"
openai.api_key = os.environ['OPENAI_KEY']
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt_text,
temperature=0.7,
max_tokens=2300,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
story = response["choices"][0]["text"]
content_to_classify = "Your content here"
response = openai.Completion.create(
model="content-filter-alpha",
prompt="{}\n--\nLabel:".format(story),
temperature=0,
max_tokens=1,
top_p=0,
logprobs=10
)
output_label = response["choices"][0]["text"]
# This is the probability at which we evaluate that a "2" is likely real
# vs. should be discarded as a false positive
toxic_threshold = -0.355
if output_label == "2":
story='Please generate again'
if story.startswith('\n\n'):
story = story[2:]
return story
'''
demo = gr.Interface(
fn=themes,
inputs=["text", "text"],
outputs=["text", "image"],
)
demo.launch()
'''
with gr.Blocks(css='''
.h1 {
font-family: HK Grotesk;
font-style: normal;
font-weight: bold;
font-size: 100px;
line-height: 105%;
margin: 0;
}
''') as demo:
title = gr.HTML('''
<div style="text-align: center; margin: 0;">
<div style="
display: inline-flex;
align-items: center;
gap: 0.8rem;
font-size: 1.75rem;
">
<h1 style="font-weight: 900; margin-bottom: 7px;">
Infinite Stories
</h1>
</div>
<p style="margin-bottom: 10px; font-size: 94%;">
</p>
<br>
</div>
''')
with gr.Row():
theme1 = gr.Textbox(label='Seed 1', elem_id='theme')
theme2 = gr.Textbox(label='Seed 2', elem_id='theme')
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"]
theme_dropdown = gr.Dropdown(choices=theme_options, label="Moral")
slider = gr.Slider(minimum=0, maximum=10, step=5, label="Approximate reading time", default=5)
b1 = gr.Button("Create!", elem_id="generate-btn")
story_output = gr.Textbox(label='Story')
b1.click(generateStory, inputs=[theme1, theme2, theme_dropdown, slider], outputs=[story_output])
demo.launch(debug=True, share=False)