Spaces:
Build error
Build error
Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from math import sqrt
|
| 2 |
+
|
| 3 |
+
import matplotlib
|
| 4 |
+
matplotlib.use('Agg')
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy as np
|
| 7 |
+
import plotly.express as px
|
| 8 |
+
import pandas as pd
|
| 9 |
+
import bokeh.plotting as bk
|
| 10 |
+
from bokeh.models import ColumnDataSource
|
| 11 |
+
from bokeh.embed import json_item
|
| 12 |
+
|
| 13 |
+
import gradio as gr
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def outbreak(plot_type, r, month, countries, social_distancing):
|
| 17 |
+
months = ["January", "February", "March", "April", "May"]
|
| 18 |
+
m = months.index(month)
|
| 19 |
+
start_day = 30 * m
|
| 20 |
+
final_day = 30 * (m + 1)
|
| 21 |
+
x = np.arange(start_day, final_day + 1)
|
| 22 |
+
pop_count = {"USA": 350, "Canada": 40, "Mexico": 300, "UK": 120}
|
| 23 |
+
if social_distancing:
|
| 24 |
+
r = sqrt(r)
|
| 25 |
+
df = pd.DataFrame({'day': x})
|
| 26 |
+
for country in countries:
|
| 27 |
+
df[country] = ( x ** (r) * (pop_count[country] + 1))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
if plot_type == "Matplotlib":
|
| 31 |
+
fig = plt.figure()
|
| 32 |
+
plt.plot(df['day'], df[countries].to_numpy())
|
| 33 |
+
plt.title("Outbreak in " + month)
|
| 34 |
+
plt.ylabel("Cases")
|
| 35 |
+
plt.xlabel("Days since Day 0")
|
| 36 |
+
plt.legend(countries)
|
| 37 |
+
return fig
|
| 38 |
+
elif plot_type == "Plotly":
|
| 39 |
+
fig = px.line(df, x='day', y=countries)
|
| 40 |
+
fig.update_layout(title="Outbreak in " + month,
|
| 41 |
+
xaxis_title="Cases",
|
| 42 |
+
yaxis_title="Days Since Day 0")
|
| 43 |
+
return fig
|
| 44 |
+
else:
|
| 45 |
+
source = ColumnDataSource(df)
|
| 46 |
+
p = bk.figure(title="Outbreak in " + month, x_axis_label="Cases", y_axis_label="Days Since Day 0")
|
| 47 |
+
for country in countries:
|
| 48 |
+
p.line(x='day', y=country, line_width=2, source=source)
|
| 49 |
+
item_text = json_item(p, "plotDiv")
|
| 50 |
+
return item_text
|
| 51 |
+
|
| 52 |
+
inputs = [
|
| 53 |
+
gr.Dropdown(["Matplotlib", "Plotly", "Bokeh"], label="Plot Type"),
|
| 54 |
+
gr.Slider(1, 4, 3.2, label="R"),
|
| 55 |
+
gr.Dropdown(["January", "February", "March", "April", "May"], label="Month"),
|
| 56 |
+
gr.CheckboxGroup(["USA", "Canada", "Mexico", "UK"], label="Countries",
|
| 57 |
+
value=["USA", "Canada"]),
|
| 58 |
+
gr.Checkbox(label="Social Distancing?"),
|
| 59 |
+
]
|
| 60 |
+
outputs = gr.Plot()
|
| 61 |
+
|
| 62 |
+
demo = gr.Interface(fn=outbreak, inputs=inputs, outputs=outputs, examples=[
|
| 63 |
+
["Matplotlib", 2, "March", ["Mexico", "UK"], True],
|
| 64 |
+
["Plotly", 3.6, "February", ["Canada", "Mexico", "UK"], False],
|
| 65 |
+
["Bokeh", 1.2, "May", ["UK"], True]
|
| 66 |
+
], cache_examples=True)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|