Spaces:
Sleeping
Sleeping
import gradio as gr | |
from pprint import pprint | |
import json | |
from loguru import logger | |
from dotenv import load_dotenv | |
load_dotenv() | |
import helpers | |
from homeros import init_story, start_story, continue_story, finish_story, define_metadata, evaluate_story | |
DEFAULT_PARAMS = True | |
''' | |
Here we manage the flow and state of the story | |
''' | |
def do_homeros(user_input, story): | |
# if default params is true - skip the asking, including magic word and just start the story | |
if DEFAULT_PARAMS and len(story["chunks"]) == 0: | |
story = init_story(story) | |
story = define_metadata("J.R.R. Tolkien's Middle Earth", "world", story) | |
story = define_metadata("I don't know. Please choose something unusual.", "hero", story) | |
story = define_metadata("I don't know. Please choose something unusual.", "plot", story) | |
story = define_metadata("Happy", "ending", story) | |
story = define_metadata("epic", "style", story) | |
story["status"] = "ongoing" | |
story = start_story(story) | |
next_message = story["chunks"][-1]["audio_url"] | |
return next_message, story | |
# story hasn't started | |
if story["status"] == "not_started": | |
logger.debug("status: initiating a new story") | |
next_message = helpers.get_fixed_msg("welcome") | |
story["status"] = "checking_magic_word" | |
# we are checking the magic word or it is wrong and we need to ask for it again | |
elif story["status"] == "checking_magic_word" or story["status"] == "wrong_magic_word": | |
logger.debug("status: checking magic word") | |
magic_word_correct = helpers.check_magic_word(user_input) | |
if magic_word_correct: | |
story["status"] = "defining_metadata_world" | |
story = init_story(story) | |
next_message = helpers.get_fixed_msg("ask_world") | |
else: | |
story["status"] = "wrong_magic_word" | |
next_message = helpers.get_fixed_msg("wrong_magic_word") | |
# defining the world | |
elif story["status"] == "defining_metadata_world": | |
logger.debug("status: magic word is wrong") | |
story = define_metadata(user_input, "world", story) | |
story["status"] = "defining_metadata_hero" | |
next_message = helpers.get_fixed_msg("ask_hero") | |
# defining the hero | |
elif story["status"] == "defining_metadata_hero": | |
logger.debug("status: defining the hero") | |
story = define_metadata(user_input, "hero", story) | |
story["status"] = "defining_metadata_plot" | |
next_message = helpers.get_fixed_msg("ask_plot") | |
# defining the plot | |
elif story["status"] == "defining_metadata_plot": | |
logger.debug("status: defining the plot") | |
story = define_metadata(user_input, "plot", story) | |
story["status"] = "defining_metadata_ending" | |
next_message = helpers.get_fixed_msg("ask_ending") | |
# defining the ending | |
elif story["status"] == "defining_metadata_ending": | |
logger.debug("status: defining the ending") | |
story = define_metadata(user_input, "ending", story) | |
story["status"] = "defining_metadata_style" | |
next_message = helpers.get_fixed_msg("ask_style") | |
# defining the style and starting the story with the first chunk | |
elif story["status"] == "defining_metadata_style": | |
logger.debug("status: defining the style") | |
story = define_metadata(user_input, "style", story) | |
story["status"] = "ongoing" | |
story = start_story(story) | |
next_message = story["chunks"][-1]["audio_url"] | |
# we are in the middle of the story - evaluate if time to end, or continue | |
elif story["status"] == "ongoing": | |
if evaluate_story(story)["is_time_to_end"]: | |
logger.debug("status: activating story finish") | |
story = finish_story(user_input, story) | |
story["status"] = "finished" | |
else: | |
story = continue_story(user_input, story) | |
story["status"] = "ongoing" | |
next_message = story["chunks"][-1]["audio_url"] | |
# story has ended, but the user still inputting. tell them it's over | |
elif story["status"] == "finished": | |
next_message = helpers.get_fixed_msg("no_more_story") | |
story["status"] = "finished" | |
else: | |
raise Exception("strange story status") | |
logger.error(f"we have a story status {story['status']} we didn't catch...") | |
logger.debug(story) | |
return next_message, story | |
demo = gr.Blocks() | |
with demo: | |
story = gr.State(value = { | |
"uuid" : "", | |
"status" : "not_started", | |
"world": "", | |
"hero": "", | |
"plot": "", | |
"ending": "", | |
"style": "", | |
"voice": "dylan", | |
"storyteller": "general", | |
"chunks" : [], | |
"messages": [], | |
"full_story_audio_ur": "", | |
"full_story_text": "" | |
}) | |
with gr.Row(): | |
gr.Markdown(''' | |
# HOMEROS | |
This demo is exploring the future of interactive storytelling. | |
It puts the user in charge and blurs the boundary between the reader and the author. | |
Hit "Tell me!" to get started. | |
When Homeros asks you something - hit record, answer with your voice and then hit "Tell me!" again. | |
''') | |
with gr.Row(): | |
text_input = gr.Textbox( | |
label="you say" | |
) | |
with gr.Row(): | |
story_chunk = gr.Audio( | |
label="storyteller says", | |
autoplay=True | |
) | |
with gr.Row(): | |
go_btn = gr.Button( | |
"Tell me!", | |
) | |
go_btn.click( | |
do_homeros, | |
inputs=[text_input, story], | |
outputs=[story_chunk, story] | |
) | |
demo.queue( | |
concurrency_count=5 | |
) | |
demo.launch( | |
server_name="0.0.0.0", | |
ssl_verify=False, | |
show_api=False | |
) | |