added app.py and requirements.txt
Browse files- app.py +76 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import threading
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import statistics
|
| 6 |
+
from scipy import stats
|
| 7 |
+
|
| 8 |
+
DATA_DIR = './storage'
|
| 9 |
+
DATA_FILE = os.path.join(DATA_DIR, 'guesses.json')
|
| 10 |
+
lock = threading.Lock()
|
| 11 |
+
|
| 12 |
+
def ensure_data_directory():
|
| 13 |
+
os.makedirs(DATA_DIR, exist_ok=True)
|
| 14 |
+
|
| 15 |
+
def load_guesses():
|
| 16 |
+
ensure_data_directory()
|
| 17 |
+
if not os.path.exists(DATA_FILE):
|
| 18 |
+
with open(DATA_FILE, 'w') as f:
|
| 19 |
+
json.dump([], f)
|
| 20 |
+
with open(DATA_FILE, 'r') as f:
|
| 21 |
+
return json.load(f)
|
| 22 |
+
|
| 23 |
+
def save_guesses(guesses):
|
| 24 |
+
with open(DATA_FILE, 'w') as f:
|
| 25 |
+
json.dump(guesses, f)
|
| 26 |
+
|
| 27 |
+
def add_guess(guess):
|
| 28 |
+
with lock:
|
| 29 |
+
guesses = load_guesses()
|
| 30 |
+
guesses.append(guess)
|
| 31 |
+
save_guesses(guesses)
|
| 32 |
+
|
| 33 |
+
n = len(guesses)
|
| 34 |
+
average = sum(guesses) / n
|
| 35 |
+
|
| 36 |
+
if n >= 2:
|
| 37 |
+
# Calculate sample standard deviation
|
| 38 |
+
s = statistics.stdev(guesses)
|
| 39 |
+
# Calculate standard error
|
| 40 |
+
SE = s / (n ** 0.5)
|
| 41 |
+
# Degrees of freedom
|
| 42 |
+
df = n - 1
|
| 43 |
+
# Confidence level (e.g., 95%)
|
| 44 |
+
confidence_level = 0.95
|
| 45 |
+
alpha = 1 - confidence_level
|
| 46 |
+
# Calculate critical t-value
|
| 47 |
+
t_value = stats.t.ppf(1 - alpha/2, df)
|
| 48 |
+
# Calculate margin of error
|
| 49 |
+
ME = t_value * SE
|
| 50 |
+
# Calculate confidence interval
|
| 51 |
+
ci_lower = average - ME
|
| 52 |
+
ci_upper = average + ME
|
| 53 |
+
# Prepare output message
|
| 54 |
+
return (
|
| 55 |
+
f"Your guess has been recorded.\n"
|
| 56 |
+
f"Current average of all guesses: {average:.2f}\n"
|
| 57 |
+
f"95% confidence interval: ({ci_lower:.2f}, {ci_upper:.2f})"
|
| 58 |
+
)
|
| 59 |
+
else:
|
| 60 |
+
# Not enough data to compute confidence interval
|
| 61 |
+
return (
|
| 62 |
+
f"Your guess has been recorded.\n"
|
| 63 |
+
f"Current average of all guesses: {average:.2f}\n"
|
| 64 |
+
f"Not enough data to compute confidence interval."
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
demo = gr.Interface(
|
| 68 |
+
fn=add_guess,
|
| 69 |
+
inputs=gr.Number(label="Enter your guess"),
|
| 70 |
+
outputs="text",
|
| 71 |
+
title="Collective Guessing Game",
|
| 72 |
+
description="Submit your guess and contribute to the global average!"
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.4.0
|
| 2 |
+
scipy==1.14.1
|