os1187's picture
Create app.py
e0d0f44
raw
history blame
3.76 kB
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import gradio as gr
def plot_usage_volume(systems, users, timelines):
# Create empty dataframe to hold usage data
data = pd.DataFrame(columns=["System", "Month", "Usage"])
# Generate usage data for each system based on typical rollout behaviors
for i, system in enumerate(systems):
# Calculate the monthly usage volume based on the estimated number of users and timeline
usage = np.concatenate([
np.linspace(0, users[i], int(timelines[i] * 0.25)),
np.linspace(users[i] * 0.5, users[i], int(timelines[i] * 0.5)),
np.linspace(users[i], users[i] * 0.75, int(timelines[i] * 0.25))
])
# Add the usage data to the dataframe
months = ["Month {}".format(j) for j in range(1, len(usage) + 1)]
system_data = pd.DataFrame({"System": [system] * len(usage), "Month": months, "Usage": usage})
data = pd.concat([data, system_data], axis=0)
# Create a larger plot with multiple subplots
fig, axs = plt.subplots(2, 1, figsize=(12, 8), sharex=True, gridspec_kw={'hspace': 0.5})
# Plot the usage volume for each system
for i, system in enumerate(systems):
system_data = data[data["System"] == system]
axs[0].plot(system_data["Month"], system_data["Usage"], label=system)
axs[0].set_ylabel("Usage Volume")
axs[0].set_title("Usage Volume for Various Systems")
# Add a line plot that aggregates the usage of all systems
aggregated_data = data.groupby("Month").sum().reset_index()
axs[1].plot(aggregated_data["Month"], aggregated_data["Usage"], label="All Systems", linewidth=3, linestyle="--")
axs[1].set_xlabel("Month")
axs[1].set_ylabel("Usage Volume")
axs[1].set_title("Aggregated Usage Volume for All Systems")
# Set xticks to only show every 3 months
axs[0].set_xticks(aggregated_data.index[::3])
axs[1].set_xticks(aggregated_data.index[::3])
# Set xticklabels to show only the months with a step of 3
axs[0].set_xticklabels(aggregated_data["Month"][::3], rotation=45, fontsize=10)
axs[1].set_xticklabels(aggregated_data["Month"][::3], rotation=45, fontsize=10)
# Add a legend to the plot
axs[0].legend()
axs[1].legend()
# Return the plot as a Gradio output
return gr.outputs.Image(plt)
# Create input components for the Gradio app
system1 = gr.inputs.Textbox(label="System 1 Name", default="System1")
users1 = gr.inputs.Slider(minimum=100, maximum=10000, step=100, default=1000, label="System 1 Estimated Users")
timeline1 = gr.inputs.Slider(minimum=6, maximum=48, step=6, default=12, label="System 1 Timeline (Months)")
# Add input components for the remaining two systems
system2 = gr.inputs.Textbox(label="System 2 Name", default="System2")
users2 = gr.inputs.Slider(minimum=100, maximum=10000, step=100, default=500, label="System 2 Estimated Users")
timeline2 = gr.inputs.Slider(minimum=6, maximum=48, step=6, default=18, label="System 2 Timeline (Months)")
system3 = gr.inputs.Textbox(label="System 3 Name", default="System3")
users3 = gr.inputs.Slider(minimum=100, maximum=10000, step=100, default=2000, label="System 3 Estimated Users")
timeline3 = gr.inputs.Slider(minimum=6, maximum=48, step=6, default=24, label="System 3 Timeline (Months)")
# Create the Gradio interface
inputs = [system1, users1, timeline1, system2, users2, timeline2, system3, users3, timeline3]
output = gr.outputs.Image("plot.png")
title = "System Rollout Usage Volume Plot"
description = "Enter the rollout parameters for three different systems and see the resulting usage volume plot."
gr.Interface(plot_usage_volume, inputs, output, title=title, description=description).launch();