{"cells": [{"cell_type": "markdown", "id": "302934307671667531413257853548643485645", "metadata": {}, "source": ["# Gradio Demo: hangman"]}, {"cell_type": "code", "execution_count": null, "id": "272996653310673477252411125948039410165", "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": "288918539441861185822528903084949547379", "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "secret_word = \"gradio\"\n", "\n", "with gr.Blocks() as demo:\n", " used_letters_var = gr.State([])\n", " with gr.Row() as row:\n", " with gr.Column():\n", " input_letter = gr.Textbox(label=\"Enter letter\")\n", " btn = gr.Button(\"Guess Letter\")\n", " with gr.Column():\n", " hangman = gr.Textbox(\n", " label=\"Hangman\",\n", " value=\"_\"*len(secret_word)\n", " )\n", " used_letters_box = gr.Textbox(label=\"Used Letters\")\n", "\n", " def guess_letter(letter, used_letters):\n", " used_letters.append(letter)\n", " answer = \"\".join([\n", " (letter if letter in used_letters else \"_\")\n", " for letter in secret_word\n", " ])\n", " return {\n", " used_letters_var: used_letters,\n", " used_letters_box: \", \".join(used_letters),\n", " hangman: answer\n", " }\n", " btn.click(\n", " guess_letter,\n", " [input_letter, used_letters_var],\n", " [used_letters_var, used_letters_box, hangman]\n", " )\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5}