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 do_homeros DEFAULT_STORY_LEN = 3 #default : after how many chunks we force the story to end USE_DEFAULT_SETTINGS = "Storyteller decides" #default : are we using default story config or asking the user USE_TEXT_INPUT = False # option to use text input instead of voice ''' update settings ''' def save_settings(how_many_chunks, go_with_the_defaults, save_params_btn, go_btn, u_input, story_chunk, settings, story): #save settings settings["default_settings"] = go_with_the_defaults == USE_DEFAULT_SETTINGS settings["max_len"] = how_many_chunks + 1 pprint(settings) #update ui return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), settings, story ''' gradio interface stuff ''' 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": "" }) settings = gr.State(value={ "max_len" : DEFAULT_STORY_LEN, "default_settings": USE_DEFAULT_SETTINGS, "use_text_input": USE_TEXT_INPUT }) 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. ''') with gr.Row(): how_many_chunks = gr.Slider( minimum = 1, value = DEFAULT_STORY_LEN, step = 1, maximum = 10, label = "How long would you like your story to be?", interactive = True ) with gr.Row(): go_with_defaults = gr.Radio( label = "Should the storyteller ask you about the details of the world, hero, style and plot or would you like to let the storyteller decide?", value = USE_DEFAULT_SETTINGS, choices = [ USE_DEFAULT_SETTINGS, "I want full control" ], interactive = True ) with gr.Row(): if settings.value["use_text_input"]: text_input = gr.Textbox( label="you say", visible=False ) else: u_input = gr.Audio( label="you say. NB! After each recording you will need to clear existing recoring to record again", source="microphone", type="filepath", visible=False ) with gr.Row(): story_chunk = gr.Audio( label="storyteller says", interactive=False, autoplay=True, visible=False ) with gr.Row(): go_btn = gr.Button( value="Tell the story!", visible=False ) go_btn.click( do_homeros, inputs=[u_input, story, settings], outputs=[story_chunk, story, settings] ) with gr.Row(): save_params_btn = gr.Button("Save Settings") save_params_btn.click( save_settings, inputs=[how_many_chunks, go_with_defaults, save_params_btn, go_btn, story_chunk, u_input, settings, story], outputs=[how_many_chunks, go_with_defaults, save_params_btn, go_btn, story_chunk, u_input, settings, story] ) demo.queue( concurrency_count=5 ) demo.launch( server_name="0.0.0.0", ssl_verify=False, show_api=False )