Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import threading
|
| 4 |
+
import time
|
| 5 |
+
import os
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
|
| 8 |
+
FLIP_LOG_FILE = "flips.md"
|
| 9 |
+
flip_results = []
|
| 10 |
+
is_flipping = False
|
| 11 |
+
|
| 12 |
+
def log_flip(result):
|
| 13 |
+
timestamp = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")
|
| 14 |
+
line = f"- {timestamp}: **{result}**\n"
|
| 15 |
+
with open(FLIP_LOG_FILE, "a") as f:
|
| 16 |
+
f.write(line)
|
| 17 |
+
|
| 18 |
+
def flip_forever():
|
| 19 |
+
global flip_results
|
| 20 |
+
while True:
|
| 21 |
+
result = random.choice(["Heads", "Tails"])
|
| 22 |
+
flip_results.append(result)
|
| 23 |
+
log_flip(result)
|
| 24 |
+
time.sleep(1) # 1 flip per second
|
| 25 |
+
|
| 26 |
+
def start_flipping():
|
| 27 |
+
global is_flipping
|
| 28 |
+
if not is_flipping:
|
| 29 |
+
is_flipping = True
|
| 30 |
+
t = threading.Thread(target=flip_forever, daemon=True)
|
| 31 |
+
t.start()
|
| 32 |
+
return "Flipping forever! Check the logs below."
|
| 33 |
+
return "Already flipping..."
|
| 34 |
+
|
| 35 |
+
def get_recent_flips():
|
| 36 |
+
if not os.path.exists(FLIP_LOG_FILE):
|
| 37 |
+
return "No flips yet."
|
| 38 |
+
with open(FLIP_LOG_FILE, "r") as f:
|
| 39 |
+
lines = f.readlines()
|
| 40 |
+
return "".join(lines[-20:]) # Last 20 flips
|
| 41 |
+
|
| 42 |
+
with gr.Blocks() as app:
|
| 43 |
+
gr.Markdown("# 🪙 Eternal Coin Flipper")
|
| 44 |
+
gr.Markdown("Start it once, and it flips a coin every second. Forever... kinda.")
|
| 45 |
+
start_button = gr.Button("Start Flipping (Only once!)")
|
| 46 |
+
log_box = gr.Textbox(label="Recent Flips", lines=20)
|
| 47 |
+
refresh_button = gr.Button("Refresh Log")
|
| 48 |
+
|
| 49 |
+
start_button.click(fn=start_flipping, outputs=None)
|
| 50 |
+
refresh_button.click(fn=get_recent_flips, outputs=log_box)
|
| 51 |
+
|
| 52 |
+
# auto-load some content on launch
|
| 53 |
+
app.load(fn=get_recent_flips, outputs=log_box)
|
| 54 |
+
|
| 55 |
+
app.launch()
|