File size: 5,742 Bytes
ec2a0f3
 
 
 
 
 
a7e740d
ec2a0f3
1f8e954
ec2a0f3
49a7070
 
a7e740d
 
 
ec2a0f3
 
49a7070
 
 
 
 
 
 
 
 
 
 
 
ec2a0f3
a7e740d
ec2a0f3
a7e740d
 
 
 
 
 
 
 
 
 
5447886
a7e740d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49a7070
a7e740d
 
 
1f8e954
5447886
1f8e954
a7e740d
 
5447886
a7e740d
5447886
49a7070
a7e740d
 
1f8e954
a7e740d
 
022433c
 
a7e740d
 
 
ec2a0f3
a7e740d
ec2a0f3
 
 
 
 
 
 
 
 
 
 
 
 
 
3eaf131
ec2a0f3
 
 
 
 
 
 
 
 
 
 
49a7070
ec2a0f3
 
 
 
 
 
 
 
 
3eaf131
 
 
ec2a0f3
3eaf131
49a7070
 
 
3eaf131
ec2a0f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
    )