Spaces:
Runtime error
Runtime error
Commit
•
703dffd
1
Parent(s):
7a0d3f8
draft
Browse files- app.py +90 -0
- requirements.in +4 -0
- requirements.txt +184 -0
app.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import list_spaces
|
3 |
+
from cachetools import TTLCache, cached
|
4 |
+
from toolz import groupby, valmap
|
5 |
+
|
6 |
+
|
7 |
+
@cached(cache=TTLCache(maxsize=100, ttl=60 * 10))
|
8 |
+
def get_spaces():
|
9 |
+
return list(iter(list_spaces(full=True, limit=None)))
|
10 |
+
|
11 |
+
|
12 |
+
get_spaces() # to warm up the cache
|
13 |
+
|
14 |
+
|
15 |
+
def create_space_to_like_dict():
|
16 |
+
spaces = get_spaces()
|
17 |
+
return {space.id: space.likes for space in spaces}
|
18 |
+
|
19 |
+
|
20 |
+
def create_org_to_like_dict():
|
21 |
+
spaces = get_spaces()
|
22 |
+
grouped = groupby(lambda x: x.author, spaces)
|
23 |
+
return valmap(lambda x: sum(s.likes for s in x), grouped)
|
24 |
+
|
25 |
+
|
26 |
+
def relative_rank(my_dict, target_key, filter_zero=False):
|
27 |
+
if filter_zero:
|
28 |
+
my_dict = {k: v for k, v in my_dict.items() if v != 0}
|
29 |
+
|
30 |
+
if target_key not in my_dict:
|
31 |
+
raise ValueError(
|
32 |
+
f"'{target_key}' not found in the dictionary or its value is 0."
|
33 |
+
)
|
34 |
+
|
35 |
+
sorted_items = sorted(my_dict.items(), key=lambda item: item[1], reverse=True)
|
36 |
+
|
37 |
+
position = [key for key, _ in sorted_items].index(target_key)
|
38 |
+
|
39 |
+
return (position + 1) / len(my_dict) * 100
|
40 |
+
|
41 |
+
|
42 |
+
def relative_rank_for_space(space_id, filter_zero=False):
|
43 |
+
space_to_like_dict = create_space_to_like_dict()
|
44 |
+
return relative_rank(space_to_like_dict, space_id, filter_zero=filter_zero)
|
45 |
+
|
46 |
+
|
47 |
+
def relative_rank_for_org(org_id, filter_zero=False):
|
48 |
+
org_to_like_dict = create_org_to_like_dict()
|
49 |
+
return relative_rank(org_to_like_dict, org_id, filter_zero=filter_zero)
|
50 |
+
|
51 |
+
|
52 |
+
def rank_space(space_id):
|
53 |
+
return relative_rank_for_space(space_id)
|
54 |
+
|
55 |
+
|
56 |
+
def rank_space_and_org(space_or_org_id, filter_zero):
|
57 |
+
filter_zero = filter_zero == "yes"
|
58 |
+
split = space_or_org_id.split("/")
|
59 |
+
if len(split) == 2:
|
60 |
+
space_rank = relative_rank_for_space(space_or_org_id, filter_zero=filter_zero)
|
61 |
+
return f"Space {space_or_org_id} is ranked {space_rank:.2f}%"
|
62 |
+
if len(split) == 1:
|
63 |
+
org_rank = relative_rank_for_org(space_or_org_id, filter_zero=filter_zero)
|
64 |
+
return f"Organization {space_or_org_id} is ranked {org_rank:.2f}%"
|
65 |
+
|
66 |
+
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.HTML("<h1 style='text-align: center;'> 🏆 HuggyRanker 🏆 </h1>")
|
69 |
+
gr.HTML(
|
70 |
+
"""<p style='text-align: center;'>Rank a single Space or all of the Spaces created by an organization or user by likes</p>"""
|
71 |
+
)
|
72 |
+
gr.HTML(
|
73 |
+
"""<p style="text-align: center;"><i>Remember likes aren't everything!</i></p></div>"""
|
74 |
+
)
|
75 |
+
gr.Markdown(
|
76 |
+
"""## Rank Spaces
|
77 |
+
Provide this app with a Space ID or a Username/Organization name to rank by likes."""
|
78 |
+
)
|
79 |
+
with gr.Row():
|
80 |
+
space_id = gr.Textbox(max_lines=1, label="Space ID")
|
81 |
+
filter_zero = gr.Radio(
|
82 |
+
choices=["yes", "no"],
|
83 |
+
label="Filter out spaces with 0 likes in the ranking?",
|
84 |
+
value="yes",
|
85 |
+
)
|
86 |
+
run_btn = gr.Button("Rank Space!", label="Rank Space")
|
87 |
+
result = gr.Markdown()
|
88 |
+
run_btn.click(rank_space_and_org, inputs=[space_id, filter_zero], outputs=result)
|
89 |
+
|
90 |
+
demo.launch()
|
requirements.in
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
cachetools
|
2 |
+
gradio
|
3 |
+
huggingface_hub
|
4 |
+
toolz
|
requirements.txt
ADDED
@@ -0,0 +1,184 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#
|
2 |
+
# This file is autogenerated by pip-compile with Python 3.11
|
3 |
+
# by the following command:
|
4 |
+
#
|
5 |
+
# pip-compile requirements.in
|
6 |
+
#
|
7 |
+
aiofiles==23.2.1
|
8 |
+
# via gradio
|
9 |
+
altair==5.1.1
|
10 |
+
# via gradio
|
11 |
+
annotated-types==0.5.0
|
12 |
+
# via pydantic
|
13 |
+
anyio==3.7.1
|
14 |
+
# via
|
15 |
+
# fastapi
|
16 |
+
# httpcore
|
17 |
+
# starlette
|
18 |
+
attrs==23.1.0
|
19 |
+
# via
|
20 |
+
# jsonschema
|
21 |
+
# referencing
|
22 |
+
cachetools==5.3.1
|
23 |
+
# via -r requirements.in
|
24 |
+
certifi==2023.7.22
|
25 |
+
# via
|
26 |
+
# httpcore
|
27 |
+
# httpx
|
28 |
+
# requests
|
29 |
+
charset-normalizer==3.2.0
|
30 |
+
# via requests
|
31 |
+
click==8.1.7
|
32 |
+
# via uvicorn
|
33 |
+
contourpy==1.1.0
|
34 |
+
# via matplotlib
|
35 |
+
cycler==0.11.0
|
36 |
+
# via matplotlib
|
37 |
+
fastapi==0.103.1
|
38 |
+
# via gradio
|
39 |
+
ffmpy==0.3.1
|
40 |
+
# via gradio
|
41 |
+
filelock==3.12.3
|
42 |
+
# via huggingface-hub
|
43 |
+
fonttools==4.42.1
|
44 |
+
# via matplotlib
|
45 |
+
fsspec==2023.9.0
|
46 |
+
# via
|
47 |
+
# gradio-client
|
48 |
+
# huggingface-hub
|
49 |
+
gradio==3.44.0
|
50 |
+
# via -r requirements.in
|
51 |
+
gradio-client==0.5.0
|
52 |
+
# via gradio
|
53 |
+
h11==0.14.0
|
54 |
+
# via
|
55 |
+
# httpcore
|
56 |
+
# uvicorn
|
57 |
+
httpcore==0.18.0
|
58 |
+
# via httpx
|
59 |
+
httpx==0.25.0
|
60 |
+
# via
|
61 |
+
# gradio
|
62 |
+
# gradio-client
|
63 |
+
huggingface-hub==0.17.1
|
64 |
+
# via
|
65 |
+
# -r requirements.in
|
66 |
+
# gradio
|
67 |
+
# gradio-client
|
68 |
+
idna==3.4
|
69 |
+
# via
|
70 |
+
# anyio
|
71 |
+
# httpx
|
72 |
+
# requests
|
73 |
+
importlib-resources==6.0.1
|
74 |
+
# via gradio
|
75 |
+
jinja2==3.1.2
|
76 |
+
# via
|
77 |
+
# altair
|
78 |
+
# gradio
|
79 |
+
jsonschema==4.19.0
|
80 |
+
# via altair
|
81 |
+
jsonschema-specifications==2023.7.1
|
82 |
+
# via jsonschema
|
83 |
+
kiwisolver==1.4.5
|
84 |
+
# via matplotlib
|
85 |
+
markupsafe==2.1.3
|
86 |
+
# via
|
87 |
+
# gradio
|
88 |
+
# jinja2
|
89 |
+
matplotlib==3.7.3
|
90 |
+
# via gradio
|
91 |
+
numpy==1.25.2
|
92 |
+
# via
|
93 |
+
# altair
|
94 |
+
# contourpy
|
95 |
+
# gradio
|
96 |
+
# matplotlib
|
97 |
+
# pandas
|
98 |
+
orjson==3.9.7
|
99 |
+
# via gradio
|
100 |
+
packaging==23.1
|
101 |
+
# via
|
102 |
+
# altair
|
103 |
+
# gradio
|
104 |
+
# gradio-client
|
105 |
+
# huggingface-hub
|
106 |
+
# matplotlib
|
107 |
+
pandas==2.1.0
|
108 |
+
# via
|
109 |
+
# altair
|
110 |
+
# gradio
|
111 |
+
pillow==10.0.0
|
112 |
+
# via
|
113 |
+
# gradio
|
114 |
+
# matplotlib
|
115 |
+
pydantic==2.3.0
|
116 |
+
# via
|
117 |
+
# fastapi
|
118 |
+
# gradio
|
119 |
+
pydantic-core==2.6.3
|
120 |
+
# via pydantic
|
121 |
+
pydub==0.25.1
|
122 |
+
# via gradio
|
123 |
+
pyparsing==3.1.1
|
124 |
+
# via matplotlib
|
125 |
+
python-dateutil==2.8.2
|
126 |
+
# via
|
127 |
+
# matplotlib
|
128 |
+
# pandas
|
129 |
+
python-multipart==0.0.6
|
130 |
+
# via gradio
|
131 |
+
pytz==2023.3.post1
|
132 |
+
# via pandas
|
133 |
+
pyyaml==6.0.1
|
134 |
+
# via
|
135 |
+
# gradio
|
136 |
+
# huggingface-hub
|
137 |
+
referencing==0.30.2
|
138 |
+
# via
|
139 |
+
# jsonschema
|
140 |
+
# jsonschema-specifications
|
141 |
+
requests==2.31.0
|
142 |
+
# via
|
143 |
+
# gradio
|
144 |
+
# gradio-client
|
145 |
+
# huggingface-hub
|
146 |
+
rpds-py==0.10.2
|
147 |
+
# via
|
148 |
+
# jsonschema
|
149 |
+
# referencing
|
150 |
+
semantic-version==2.10.0
|
151 |
+
# via gradio
|
152 |
+
six==1.16.0
|
153 |
+
# via python-dateutil
|
154 |
+
sniffio==1.3.0
|
155 |
+
# via
|
156 |
+
# anyio
|
157 |
+
# httpcore
|
158 |
+
# httpx
|
159 |
+
starlette==0.27.0
|
160 |
+
# via fastapi
|
161 |
+
toolz==0.12.0
|
162 |
+
# via
|
163 |
+
# -r requirements.in
|
164 |
+
# altair
|
165 |
+
tqdm==4.66.1
|
166 |
+
# via huggingface-hub
|
167 |
+
typing-extensions==4.7.1
|
168 |
+
# via
|
169 |
+
# fastapi
|
170 |
+
# gradio
|
171 |
+
# gradio-client
|
172 |
+
# huggingface-hub
|
173 |
+
# pydantic
|
174 |
+
# pydantic-core
|
175 |
+
tzdata==2023.3
|
176 |
+
# via pandas
|
177 |
+
urllib3==2.0.4
|
178 |
+
# via requests
|
179 |
+
uvicorn==0.23.2
|
180 |
+
# via gradio
|
181 |
+
websockets==11.0.3
|
182 |
+
# via
|
183 |
+
# gradio
|
184 |
+
# gradio-client
|