Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
# Load questions
|
| 5 |
+
with open('only_connect_questions.json', 'r') as f:
|
| 6 |
+
questions = json.load(f)
|
| 7 |
+
|
| 8 |
+
def create_question_tab(question):
|
| 9 |
+
with gr.Column():
|
| 10 |
+
gr.Markdown(f"## Question {question['id']}")
|
| 11 |
+
|
| 12 |
+
revealed_state = gr.State(value=0)
|
| 13 |
+
countdown_state = gr.State(value=0)
|
| 14 |
+
|
| 15 |
+
time_label = gr.Label(value="0s", label="⏱️ Time remaining")
|
| 16 |
+
|
| 17 |
+
status_box = gr.Markdown(value="Press **▶ Start Timer** to begin")
|
| 18 |
+
|
| 19 |
+
clue_boxes = []
|
| 20 |
+
for i in range(4):
|
| 21 |
+
box = gr.Textbox(
|
| 22 |
+
value="?",
|
| 23 |
+
label=f"Clue {i+1}",
|
| 24 |
+
interactive=False,
|
| 25 |
+
scale=1
|
| 26 |
+
)
|
| 27 |
+
clue_boxes.append(box)
|
| 28 |
+
|
| 29 |
+
answer_box = gr.Markdown(value="", visible=True)
|
| 30 |
+
|
| 31 |
+
with gr.Row():
|
| 32 |
+
start_btn = gr.Button("▶ Start Timer", variant="primary")
|
| 33 |
+
next_clue_btn = gr.Button("➡️ Next Clue", variant="secondary")
|
| 34 |
+
stop_btn = gr.Button("⏹ Stop Timer")
|
| 35 |
+
reveal_btn = gr.Button("🎯 Reveal Answer", variant="secondary")
|
| 36 |
+
|
| 37 |
+
timer = gr.Timer(value=1, active=False)
|
| 38 |
+
|
| 39 |
+
def update_status(revealed_count):
|
| 40 |
+
if revealed_count == 0:
|
| 41 |
+
return "Press **▶ Start Timer** to begin"
|
| 42 |
+
elif revealed_count < 4:
|
| 43 |
+
return f"⏱️ **Clues revealed: {revealed_count}/4**"
|
| 44 |
+
else:
|
| 45 |
+
return f"✅ **All clues revealed: 4/4** | Can you find the connection?"
|
| 46 |
+
|
| 47 |
+
def on_timer_tick(remaining_time):
|
| 48 |
+
new_time = max(remaining_time - 1, 0)
|
| 49 |
+
return new_time, f"{new_time}s"
|
| 50 |
+
|
| 51 |
+
def next_clue(revealed_count):
|
| 52 |
+
new_count = min(revealed_count + 1, 4)
|
| 53 |
+
|
| 54 |
+
clue_values = []
|
| 55 |
+
for i in range(4):
|
| 56 |
+
if i < new_count:
|
| 57 |
+
clue_values.append(question['clues'][i])
|
| 58 |
+
else:
|
| 59 |
+
clue_values.append("?")
|
| 60 |
+
|
| 61 |
+
status_text = update_status(new_count)
|
| 62 |
+
return new_count, status_text, *clue_values
|
| 63 |
+
|
| 64 |
+
def start_timer(revealed_count):
|
| 65 |
+
new_count = 1
|
| 66 |
+
|
| 67 |
+
clue_values = []
|
| 68 |
+
for i in range(4):
|
| 69 |
+
if i < new_count:
|
| 70 |
+
clue_values.append(question['clues'][i])
|
| 71 |
+
else:
|
| 72 |
+
clue_values.append("?")
|
| 73 |
+
|
| 74 |
+
status_text = update_status(new_count)
|
| 75 |
+
return new_count, 45, "45s", status_text, *clue_values, gr.update(active=True)
|
| 76 |
+
|
| 77 |
+
def stop_timer():
|
| 78 |
+
return gr.update(active=False)
|
| 79 |
+
|
| 80 |
+
def reveal_answer():
|
| 81 |
+
return f"**Answer:** {question['answer']}\n\n_{question['explanation']}_"
|
| 82 |
+
|
| 83 |
+
timer.tick(
|
| 84 |
+
on_timer_tick,
|
| 85 |
+
inputs=countdown_state,
|
| 86 |
+
outputs=[countdown_state, time_label]
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
start_btn.click(start_timer, inputs=revealed_state, outputs=[revealed_state, countdown_state, time_label, status_box, *clue_boxes, timer])
|
| 90 |
+
next_clue_btn.click(next_clue, inputs=revealed_state, outputs=[revealed_state, status_box, *clue_boxes])
|
| 91 |
+
stop_btn.click(stop_timer, outputs=timer)
|
| 92 |
+
reveal_btn.click(reveal_answer, outputs=answer_box)
|
| 93 |
+
|
| 94 |
+
with gr.Blocks(title="Gradi-Only Connect", theme=gr.themes.Soft()) as demo:
|
| 95 |
+
gr.Markdown("# 🎭 Only Connect: Connections Game")
|
| 96 |
+
gr.Markdown("Find the connection between 4 clues.")
|
| 97 |
+
|
| 98 |
+
with gr.Tabs():
|
| 99 |
+
for question in questions:
|
| 100 |
+
with gr.Tab(f"Question {question['id']}"):
|
| 101 |
+
create_question_tab(question)
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
demo.launch()
|