-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allowing stateful flows in Gradio #2570
Labels
enhancement
New feature or request
needs designing
The proposed feature needs to be discussed and designed before being implemented
Milestone
Comments
abidlabs
added
enhancement
New feature or request
needs designing
The proposed feature needs to be discussed and designed before being implemented
labels
Oct 31, 2022
Another try @pngwn import gradio as gr
import random
answer_list = ["good", "happy", "twelve"]
MAX_GUESSES = 10
class Demo(gr.Blocks):
def init(self):
self.win = False
self.secret_word = random.choice(answer_list)
self.letters_guessed = []
self.guesses_left = MAX_GUESSES
def state(self):
self.visible_word = ""
self.guesses_left = MAX_GUESSES - len(self.letters_guessed)
self.win = True
for letter in self.secret_word:
if letter in self.letters_guessed:
self.visible_word += letter
else:
self.win = False
self.visible_word += "_"
def ui(self):
if self.win:
gr.Markdown("# You Won")
gr.Button("Reset").click(self.init)
elif self.guesses_left == 0:
gr.Markdown("# You Lose")
gr.Button("Reset").click(self.init)
else:
gr.Number(self.guesses_left, label="Guesses Left")
with gr.Row():
gr.Textbox(self.current_letter, label="Letter")
gr.Button("Guess").click(self.add_letter)
with gr.Row():
gr.Textbox(self.visible_word, label="Visible Word")
gr.Textbox(self.letters_guessed, label="Guessed So Far")
def add_letter(self):
self.letters_guessed.append(self.current_letter) |
Another attempt! This is backwards compatible, where the import gradio as gr
import random
word_list = ["good", "happy", "twelve"]
MAX_GUESSES = 10
with gr.Blocks() as demo:
secret_word = gr.Variable()
letters_guessed = gr.Variable([])
@demo.load([secret_word, letters_guessed])
def reset():
return {
secret_word: random.choice(word_list),
letters_guessed: []
}
gr.Markdown("# Hangman")
gr.Markdown("Guess the secret word!")
@demo.bind({secret_word, letters_guessed})
def hangman(data):
for letter in data[secret_word]:
if letter not in letters_guessed:
win_condition = True
break
else:
win_condition = False
lose_condition = len(letters_guessed) == MAX_GUESSES
if win_condition:
gr.Markdown("# You Won")
gr.Button("Reset").click(reset)
elif lose_condition:
gr.Markdown("# You Won")
gr.Button("Reset").click(reset)
else:
with gr.Row():
letter = gr.Textbox(label="Enter Letter")
submit = gr.Button("Go")
@submit.click(letters_guessed, {letter, letters_guessed})
def add_letter(data):
return {
letters_guessed: data[letters_guessed] + [data[letter]]
} |
1 task
1 task
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
enhancement
New feature or request
needs designing
The proposed feature needs to be discussed and designed before being implemented
An example of stateful flow API for gradio that is very compatible with our current API.
The text was updated successfully, but these errors were encountered: