Spaces:
Runtime error
Runtime error
File size: 3,026 Bytes
398c837 |
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 |
import gradio as gr
import clip_chat
def logit2sentence(logit, slider_value):
sentence = ""
if logit < slider_value / 2.5:
sentence = "Nope. Not at all."
elif slider_value / 2.5 < logit < slider_value / 1.56:
sentence = "Not really..."
elif slider_value / 1.56 < logit < slider_value / 1.36:
sentence = "Close but not there."
elif slider_value / 1.36 < logit < slider_value / 1.14:
sentence = "That's quite close."
elif slider_value / 1.14 < logit < slider_value:
sentence = "Almost guessed."
elif logit >= slider_value:
sentence = "YES!!"
return sentence
def give_up():
image = clip_chat.image_org
return image, None, "You lost... (Press \"Reset\" to play again)"
def update_difficulty(x):
if not has_started:
clip_chat.goal = x
return clip_chat.goal
return clip_chat.goal
has_started = False
best_guess = None
def respond(message, chat_history, label_value, image_value):
global has_started, best_guess
if not has_started:
has_started = True
logits, is_better = clip_chat.answer(message)
bot_message = logit2sentence(logits, clip_chat.goal)
if is_better == 3:
best_guess = {f"Best Guess: \"{message}\"": float(logits) / clip_chat.goal}
if float(logits) >= clip_chat.goal:
print("WIN")
bot_message = "YES!"
best_guess = "YOU WIN! (Press \"Reset\" to play again)"
image_value = clip_chat.image_org
else:
if is_better == -1:
bot_message += ""
elif is_better == 0:
bot_message += "You did worse than the last one."
elif is_better == 1 or is_better == 3:
bot_message += "You did better than the last one."
label_value = best_guess
chat_history.append((message, bot_message))
return "", chat_history, label_value, image_value
def reset_everything():
global has_started, best_guess
clip_chat.reset_everything()
has_started = False
best_guess = None
return clip_chat.goal, None, "This is a \"Guess the Image\" game. I'm thinking of a picture and you have to guess using the chat above.", None
with gr.Blocks() as demo:
chatbot = gr.Chatbot()
msg = gr.Textbox()
label = gr.Label("This is a \"Guess the Image\" game. I'm thinking of a picture and you have to guess using the chat above.")
image_output = gr.outputs.Image(type="pil")
show_image_button = gr.Button("Give Up...")
slider = gr.inputs.Slider(minimum=18, maximum=25, default=21, label="Difficulty (18 - Easy, 25 - Expert)")
reset_button = gr.Button("Reset")
msg.submit(respond, [msg, chatbot], [msg, chatbot, label, image_output])
slider.release(update_difficulty, inputs=[slider], outputs=[slider])
show_image_button.click(give_up, outputs=[image_output, chatbot, label], queue=False)
reset_button.click(reset_everything, outputs=[slider, image_output, label, chatbot], queue=False)
if __name__ == "__main__":
demo.launch()
|