m-ric's picture
m-ric HF staff
Working version
cb966df
raw
history blame
2.08 kB
import gradio as gr
import requests
import pandas as pd
import plotly.express as px
class DownloadsTracker:
def __init__(self):
self.all_packages = {}
def fetch_if_needed(self, packages):
errors = []
for pkg in packages:
if pkg not in self.all_packages:
try:
response = requests.get(f"https://pypistats.org/api/packages/{pkg}/overall")
if response.status_code != 200:
errors.append(f"Package not found: {pkg}")
continue
data = response.json()["data"]
df = pd.DataFrame([
{"date": d["date"], "downloads": d["downloads"]}
for d in data if d["category"] == "without_mirrors"
])
df["date"] = pd.to_datetime(df["date"])
df = df.sort_values("date")
df["cumulative_downloads"] = df["downloads"].cumsum()
self.all_packages[pkg] = df
except:
errors.append(f"Error fetching {pkg}")
if len(errors) > 0:
return "\n".join(errors)
return None
def plot(self, package_list):
package_list = [p.strip() for p in package_list.split(",") if p.strip()]
errors = self.fetch_if_needed(package_list)
fig = px.line(title="Cumulative Package Downloads")
for pkg in package_list:
if pkg in self.all_packages:
df = self.all_packages[pkg]
fig.add_scatter(x=df["date"], y=df["cumulative_downloads"], name=pkg)
return fig, errors
tracker = DownloadsTracker()
with gr.Blocks() as app:
packages = gr.Textbox("transformers", label="Package names (comma-separated)")
error_box = gr.Textbox(label="Errors:", interactive=False)
render_btn = gr.Button("Render")
plot = gr.Plot()
render_btn.click(tracker.plot, inputs=[packages], outputs=[plot, error_box])
app.launch()