File size: 2,939 Bytes
0ef03ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3831198
0ef03ea
3831198
 
 
 
 
 
 
 
 
 
 
0ef03ea
3831198
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b572af4
3831198
 
 
 
 
 
 
f278537
3831198
 
 
0ef03ea
fd0fa88
 
 
 
 
 
 
 
 
3831198
fd0fa88
 
3831198
 
fd0fa88
 
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
93
94
95
import os
import json
import threading
import gradio as gr
import statistics
from scipy import stats

DATA_DIR = './storage'
DATA_FILE = os.path.join(DATA_DIR, 'guesses.json')
lock = threading.Lock()

def ensure_data_directory():
    os.makedirs(DATA_DIR, exist_ok=True)

def load_guesses():
    ensure_data_directory()
    if not os.path.exists(DATA_FILE):
        with open(DATA_FILE, 'w') as f:
            json.dump([], f)
    with open(DATA_FILE, 'r') as f:
        return json.load(f)

def save_guesses(guesses):
    with open(DATA_FILE, 'w') as f:
        json.dump(guesses, f)

def add_guess(guess):
    with lock:
        guesses = load_guesses()
        guesses.append(guess)
        save_guesses(guesses)
        return compute_statistics(guesses, include_message=True)

def get_current_results():
    with lock:
        guesses = load_guesses()
        return compute_statistics(guesses, include_message=False)

def compute_statistics(guesses, include_message):
    n = len(guesses)
    if n == 0:
        return "No guesses have been made yet."

    average = sum(guesses) / n

    if n >= 2:
        # Calculate sample standard deviation
        s = statistics.stdev(guesses)
        # Calculate standard error
        SE = s / (n ** 0.5)
        # Degrees of freedom
        df = n - 1
        # Confidence level (e.g., 95%)
        confidence_level = 0.95
        alpha = 1 - confidence_level
        # Calculate critical t-value
        t_value = stats.t.ppf(1 - alpha/2, df)
        # Calculate margin of error
        ME = t_value * SE
        # Calculate confidence interval
        ci_lower = average - ME
        ci_upper = average + ME
        # Prepare output message
        message = (
            (f"Your guess has been recorded.\n" if include_message else "") +
            f"Current average of all {n} guesses: {average:.2f}\n"
            f"95% confidence interval: ({ci_lower:.2f}, {ci_upper:.2f})"
        )
        return message
    else:
        # Not enough data to compute confidence interval
        message = (
            (f"Your guess has been recorded.\n" if include_message else "") +
            f"Current average of all {n} guesses: {average:.2f}\n"
            f"Not enough data to compute confidence interval."
        )
        return message

with gr.Blocks() as demo:
    gr.Markdown("# Collective Guessing Game")
    gr.Markdown("Submit your guess and contribute to the global average!")

    guess_input = gr.Number(label="Enter your guess")
    submit_button = gr.Button("Submit Guess")

    with gr.Accordion("View Current Results!", open=False):
        output_text = gr.Textbox(label="Results", lines=10)
        refresh_button = gr.Button("Refresh Results")

    submit_button.click(fn=add_guess, inputs=guess_input, outputs=output_text)
    refresh_button.click(fn=get_current_results, outputs=output_text)
    demo.load(fn=get_current_results, outputs=output_text)

demo.launch()