Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
@@ -1,91 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import clip_chat
|
3 |
-
|
4 |
-
|
5 |
-
def logit2sentence(logit, slider_value):
|
6 |
-
sentence = ""
|
7 |
-
if logit < slider_value / 2.5:
|
8 |
-
sentence = "Nope. Not at all."
|
9 |
-
elif slider_value / 2.5 < logit < slider_value / 1.56:
|
10 |
-
sentence = "Not really..."
|
11 |
-
elif slider_value / 1.56 < logit < slider_value / 1.36:
|
12 |
-
sentence = "Close but not there."
|
13 |
-
elif slider_value / 1.36 < logit < slider_value / 1.14:
|
14 |
-
sentence = "That's quite close."
|
15 |
-
elif slider_value / 1.14 < logit < slider_value:
|
16 |
-
sentence = "Almost guessed."
|
17 |
-
elif logit >= slider_value:
|
18 |
-
sentence = "YES!!"
|
19 |
-
return sentence
|
20 |
-
|
21 |
-
|
22 |
-
def give_up():
|
23 |
-
image = clip_chat.image_org
|
24 |
-
return image, None, "You lost... (Press \"Reset\" to play again)"
|
25 |
-
|
26 |
-
|
27 |
-
def update_difficulty(x):
|
28 |
-
if not has_started:
|
29 |
-
clip_chat.goal = x
|
30 |
-
return clip_chat.goal
|
31 |
-
return clip_chat.goal
|
32 |
-
|
33 |
-
|
34 |
-
has_started = False
|
35 |
-
best_guess = None
|
36 |
-
|
37 |
-
|
38 |
-
def respond(message, chat_history, label_value, image_value):
|
39 |
-
global has_started, best_guess
|
40 |
-
|
41 |
-
if not has_started:
|
42 |
-
has_started = True
|
43 |
-
|
44 |
-
logits, is_better = clip_chat.answer(message)
|
45 |
-
bot_message = logit2sentence(logits, clip_chat.goal)
|
46 |
-
|
47 |
-
if is_better == 3:
|
48 |
-
best_guess = {f"Best Guess: \"{message}\"": float(logits) / clip_chat.goal}
|
49 |
-
if float(logits) >= clip_chat.goal:
|
50 |
-
print("WIN")
|
51 |
-
bot_message = "YES!"
|
52 |
-
best_guess = "YOU WIN! (Press \"Reset\" to play again)"
|
53 |
-
image_value = clip_chat.image_org
|
54 |
-
else:
|
55 |
-
if is_better == -1:
|
56 |
-
bot_message += ""
|
57 |
-
elif is_better == 0:
|
58 |
-
bot_message += "You did worse than the last one."
|
59 |
-
elif is_better == 1 or is_better == 3:
|
60 |
-
bot_message += "You did better than the last one."
|
61 |
-
|
62 |
-
label_value = best_guess
|
63 |
-
|
64 |
-
chat_history.append((message, bot_message))
|
65 |
-
return "", chat_history, label_value, image_value
|
66 |
-
|
67 |
-
|
68 |
-
def reset_everything():
|
69 |
-
global has_started, best_guess
|
70 |
-
clip_chat.reset_everything()
|
71 |
-
has_started = False
|
72 |
-
best_guess = None
|
73 |
-
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
|
74 |
-
|
75 |
-
|
76 |
-
with gr.Blocks() as demo:
|
77 |
-
chatbot = gr.Chatbot()
|
78 |
-
msg = gr.Textbox()
|
79 |
-
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.")
|
80 |
-
image_output = gr.outputs.Image(type="pil")
|
81 |
-
show_image_button = gr.Button("Give Up...")
|
82 |
-
slider = gr.inputs.Slider(minimum=18, maximum=25, default=21, label="Difficulty (18 - Easy, 25 - Expert)")
|
83 |
-
reset_button = gr.Button("Reset")
|
84 |
-
|
85 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot, label, image_output])
|
86 |
-
slider.release(update_difficulty, inputs=[slider], outputs=[slider])
|
87 |
-
show_image_button.click(give_up, outputs=[image_output, chatbot, label], queue=False)
|
88 |
-
reset_button.click(reset_everything, outputs=[slider, image_output, label, chatbot], queue=False)
|
89 |
-
|
90 |
-
if __name__ == "__main__":
|
91 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|