Spaces:
Running
Running
Working version
Browse files- app.py +53 -4
- requirements.txt +2 -0
app.py
CHANGED
@@ -1,7 +1,56 @@
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
import plotly.express as px
|
5 |
|
6 |
+
class DownloadsTracker:
|
7 |
+
def __init__(self):
|
8 |
+
self.all_packages = {}
|
9 |
+
|
10 |
+
def fetch_if_needed(self, packages):
|
11 |
+
errors = []
|
12 |
+
for pkg in packages:
|
13 |
+
if pkg not in self.all_packages:
|
14 |
+
try:
|
15 |
+
response = requests.get(f"https://pypistats.org/api/packages/{pkg}/overall")
|
16 |
+
if response.status_code != 200:
|
17 |
+
errors.append(f"Package not found: {pkg}")
|
18 |
+
continue
|
19 |
+
|
20 |
+
data = response.json()["data"]
|
21 |
+
df = pd.DataFrame([
|
22 |
+
{"date": d["date"], "downloads": d["downloads"]}
|
23 |
+
for d in data if d["category"] == "without_mirrors"
|
24 |
+
])
|
25 |
+
df["date"] = pd.to_datetime(df["date"])
|
26 |
+
df = df.sort_values("date")
|
27 |
+
df["cumulative_downloads"] = df["downloads"].cumsum()
|
28 |
+
self.all_packages[pkg] = df
|
29 |
+
except:
|
30 |
+
errors.append(f"Error fetching {pkg}")
|
31 |
+
if len(errors) > 0:
|
32 |
+
return "\n".join(errors)
|
33 |
+
return None
|
34 |
|
35 |
+
def plot(self, package_list):
|
36 |
+
package_list = [p.strip() for p in package_list.split(",") if p.strip()]
|
37 |
+
errors = self.fetch_if_needed(package_list)
|
38 |
+
|
39 |
+
fig = px.line(title="Cumulative Package Downloads")
|
40 |
+
for pkg in package_list:
|
41 |
+
if pkg in self.all_packages:
|
42 |
+
df = self.all_packages[pkg]
|
43 |
+
fig.add_scatter(x=df["date"], y=df["cumulative_downloads"], name=pkg)
|
44 |
+
|
45 |
+
return fig, errors
|
46 |
+
|
47 |
+
tracker = DownloadsTracker()
|
48 |
+
|
49 |
+
with gr.Blocks() as app:
|
50 |
+
packages = gr.Textbox("transformers", label="Package names (comma-separated)")
|
51 |
+
error_box = gr.Textbox(label="Errors:", interactive=False)
|
52 |
+
render_btn = gr.Button("Render")
|
53 |
+
plot = gr.Plot()
|
54 |
+
render_btn.click(tracker.plot, inputs=[packages], outputs=[plot, error_box])
|
55 |
+
|
56 |
+
app.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
plotly
|