added altair charts
Browse files- app.py +62 -6
- requirements.txt +1 -0
app.py
CHANGED
@@ -5,6 +5,8 @@ import gradio as gr
|
|
5 |
import statistics
|
6 |
from scipy import stats
|
7 |
import numpy as np # Added import for numpy to compute percentiles
|
|
|
|
|
8 |
|
9 |
DATA_DIR = './storage'
|
10 |
DATA_FILE = os.path.join(DATA_DIR, 'guesses.json')
|
@@ -31,12 +33,15 @@ def add_guess(guess):
|
|
31 |
guesses.append(guess)
|
32 |
save_guesses(guesses)
|
33 |
message = compute_statistics(guesses, include_message=True)
|
34 |
-
|
|
|
35 |
|
36 |
def get_current_results():
|
37 |
with lock:
|
38 |
guesses = load_guesses()
|
39 |
-
|
|
|
|
|
40 |
|
41 |
def compute_statistics(guesses, include_message):
|
42 |
n = len(guesses)
|
@@ -84,6 +89,49 @@ def compute_statistics(guesses, include_message):
|
|
84 |
|
85 |
return message
|
86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
with gr.Blocks() as demo:
|
88 |
gr.Markdown("# Collective Guessing Game")
|
89 |
gr.Markdown("Submit your guess and contribute to the global statistics!")
|
@@ -91,9 +139,17 @@ with gr.Blocks() as demo:
|
|
91 |
submit_button = gr.Button("Submit Guess")
|
92 |
with gr.Accordion("View Current Results!", open=False):
|
93 |
output_text = gr.Textbox(label="Results", lines=10)
|
|
|
94 |
refresh_button = gr.Button("Refresh Results")
|
95 |
-
submit_button.click(
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
demo.launch()
|
|
|
5 |
import statistics
|
6 |
from scipy import stats
|
7 |
import numpy as np # Added import for numpy to compute percentiles
|
8 |
+
import altair as alt
|
9 |
+
import pandas as pd
|
10 |
|
11 |
DATA_DIR = './storage'
|
12 |
DATA_FILE = os.path.join(DATA_DIR, 'guesses.json')
|
|
|
33 |
guesses.append(guess)
|
34 |
save_guesses(guesses)
|
35 |
message = compute_statistics(guesses, include_message=True)
|
36 |
+
chart = generate_plot(guesses) # Generate the Altair chart
|
37 |
+
return message, None, chart # Return message, cleared input, and chart
|
38 |
|
39 |
def get_current_results():
|
40 |
with lock:
|
41 |
guesses = load_guesses()
|
42 |
+
message = compute_statistics(guesses, include_message=False)
|
43 |
+
chart = generate_plot(guesses)
|
44 |
+
return message, chart
|
45 |
|
46 |
def compute_statistics(guesses, include_message):
|
47 |
n = len(guesses)
|
|
|
89 |
|
90 |
return message
|
91 |
|
92 |
+
def generate_plot(guesses):
|
93 |
+
if len(guesses) < 2:
|
94 |
+
return None # Not enough data to plot
|
95 |
+
|
96 |
+
# Convert the list of guesses into a Pandas DataFrame
|
97 |
+
df = pd.DataFrame({'Guess': guesses})
|
98 |
+
|
99 |
+
# Histogram
|
100 |
+
histogram = alt.Chart(df).mark_bar().encode(
|
101 |
+
alt.X('Guess', bin=alt.Bin(maxbins=20), title='Guess Value'),
|
102 |
+
alt.Y('count()', title='Frequency'),
|
103 |
+
tooltip=[alt.Tooltip('count()', title='Frequency')]
|
104 |
+
).properties(
|
105 |
+
title='Histogram of Guesses',
|
106 |
+
width=500,
|
107 |
+
height=300
|
108 |
+
)
|
109 |
+
|
110 |
+
# CDF
|
111 |
+
df_sorted = df.sort_values('Guess').reset_index(drop=True)
|
112 |
+
df_sorted['ECDF'] = (df_sorted.index + 1) / len(df_sorted)
|
113 |
+
|
114 |
+
cdf = alt.Chart(df_sorted).mark_line(interpolate='step-after').encode(
|
115 |
+
x=alt.X('Guess', title='Guess Value'),
|
116 |
+
y=alt.Y('ECDF', title='Cumulative Probability'),
|
117 |
+
tooltip=[
|
118 |
+
alt.Tooltip('Guess', title='Guess Value'),
|
119 |
+
alt.Tooltip('ECDF', title='Cumulative Probability', format='.2f')
|
120 |
+
]
|
121 |
+
).properties(
|
122 |
+
title='Cumulative Distribution Function (CDF)',
|
123 |
+
width=500,
|
124 |
+
height=300
|
125 |
+
)
|
126 |
+
|
127 |
+
# Combine the two charts vertically
|
128 |
+
combined_chart = alt.vconcat(histogram, cdf).configure_title(
|
129 |
+
fontSize=16,
|
130 |
+
anchor='middle'
|
131 |
+
).interactive()
|
132 |
+
|
133 |
+
return combined_chart
|
134 |
+
|
135 |
with gr.Blocks() as demo:
|
136 |
gr.Markdown("# Collective Guessing Game")
|
137 |
gr.Markdown("Submit your guess and contribute to the global statistics!")
|
|
|
139 |
submit_button = gr.Button("Submit Guess")
|
140 |
with gr.Accordion("View Current Results!", open=False):
|
141 |
output_text = gr.Textbox(label="Results", lines=10)
|
142 |
+
output_plot = gr.Plot(label="Data Visualization") # Plot component for Altair charts
|
143 |
refresh_button = gr.Button("Refresh Results")
|
144 |
+
submit_button.click(
|
145 |
+
fn=add_guess,
|
146 |
+
inputs=guess_input,
|
147 |
+
outputs=[output_text, guess_input, output_plot]
|
148 |
+
)
|
149 |
+
refresh_button.click(
|
150 |
+
fn=get_current_results,
|
151 |
+
outputs=[output_text, output_plot]
|
152 |
+
)
|
153 |
+
demo.load(fn=get_current_results, outputs=[output_text, output_plot])
|
154 |
+
|
155 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,2 +1,3 @@
|
|
1 |
gradio==5.4.0
|
2 |
scipy==1.14.1
|
|
|
|
1 |
gradio==5.4.0
|
2 |
scipy==1.14.1
|
3 |
+
altair
|