import time import os import gradio as gr import word_scoring def evaluate_new_word(new_word_str: str, word_history: list, score: int): new_word = word_scoring.create_word(new_word_str) new_word_history = word_history.copy() new_word_history.append(new_word) if not len(word_history) > 0: return [0, new_word_history, new_word.word, 0] last_words = word_history[-2:] word_score = word_scoring.score_word(last_words, new_word) return [score + word_score, new_word_history, new_word.word, word_score] def generic_updates(): new_start_time = time.time() return ['', new_start_time, 15] def check_list(word, word_history): was_said = word in [w.word for w in word_history] return gr.update(visible=was_said) with gr.Blocks() as demo: word_history = gr.State([]) time_start = gr.State(time.time()) with gr.Group(): with gr.Row(): score = gr.Number(label='Score', interactive=False) timer = gr.Number(value=15, label='Timer', interactive=False) with gr.Row(): last_word = gr.Textbox(interactive=False, label='Last word') last_word_score = gr.Number(interactive=False, label='Last word score') with gr.Row(variant='compact'): word_input = ( gr.Textbox(label='New word', placeholder='Write your word here').style(container=False) ) already_said = gr.Markdown('That word was already said!', visible=False) new_word_params = { 'fn': evaluate_new_word, 'inputs': [word_input, word_history, score], 'outputs': [score, word_history, last_word, last_word_score] } last_word_event = last_word.change(lambda t: 15 - round(time.time() - t), inputs=[time_start], outputs=[timer], every=1) word_input.submit(generic_updates, inputs=[], outputs=[word_input, time_start, timer], cancels=[last_word_event]) word_input.submit(**new_word_params) word_input.submit(check_list, inputs=[word_input, word_history], outputs=[already_said]) demo.queue(concurrency_count=4, api_open=False).launch(server_port=None, max_threads=8, show_api=False, share=False, auth=('Daniel', os.environ.get('PASSWORD')))