Spaces:
Sleeping
Sleeping
Commit
·
69f3076
1
Parent(s):
0ba68c7
Modify backend
Browse files- app.py +5 -41
- processing.py +21 -37
app.py
CHANGED
@@ -1,54 +1,18 @@
|
|
1 |
from processing import process_spaces
|
2 |
|
3 |
from apscheduler.schedulers.background import BackgroundScheduler
|
4 |
-
from huggingface_hub import hf_hub_url
|
5 |
from fastapi import FastAPI
|
6 |
from fastapi.responses import HTMLResponse
|
7 |
import uvicorn
|
8 |
-
import requests
|
9 |
-
|
10 |
-
def batch(iterable, n=1):
|
11 |
-
l = len(iterable)
|
12 |
-
for ndx in range(0, l, n):
|
13 |
-
yield iterable[ndx:min(ndx + n, l)]
|
14 |
-
|
15 |
-
SPACE = """
|
16 |
-
<a href="https://huggingface.co/spaces/{space_id}/" target="_blank" class="linkwrap">
|
17 |
-
<div class="blocker"></div>
|
18 |
-
<iframe width="300" height="300" src="{subdomain}" frameborder="0"></iframe>
|
19 |
-
</a>
|
20 |
-
"""
|
21 |
-
|
22 |
-
|
23 |
-
def embed_theme_spaces():
|
24 |
-
|
25 |
-
subdomains = requests.get(hf_hub_url("freddyaboulton/gradio-theme-subdomains",
|
26 |
-
filename="subdomains.json",
|
27 |
-
repo_type="dataset")).json()
|
28 |
-
html = """
|
29 |
-
<style>
|
30 |
-
.linkwrap { position:relative; display:inline-block; }
|
31 |
-
.blocker { position:absolute; height:100%; width:100%; z-index:1; background:rgba(116, 113, 113, 0.102); }
|
32 |
-
.linkwrap iframe { z-index: 2; }
|
33 |
-
</style>
|
34 |
-
<h1 style="font-family: sans-serif; text-align: center;">Gradio Theme Gallery</h1>
|
35 |
-
<h3 style="font-family: sans-serif; text-align: center;">Click on a theme to be taken to its preview!</h3>
|
36 |
-
<body style="background: rgb(255 165 0 / 38%)">
|
37 |
-
"""
|
38 |
-
for row in batch(list(subdomains.keys()), n=4):
|
39 |
-
html += ("\n".join(["<div>"] +
|
40 |
-
[SPACE.format(subdomain=subdomains[s], space_id=s) for s in row] +
|
41 |
-
["</div>"])
|
42 |
-
)
|
43 |
-
return html + "</body>"
|
44 |
-
|
45 |
-
|
46 |
|
47 |
app = FastAPI()
|
48 |
|
49 |
-
@app.get("/"
|
50 |
def index():
|
51 |
-
return
|
|
|
|
|
|
|
52 |
|
53 |
scheduler = BackgroundScheduler()
|
54 |
scheduler.add_job(func=process_spaces, trigger="interval", seconds=360)
|
|
|
1 |
from processing import process_spaces
|
2 |
|
3 |
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
4 |
from fastapi import FastAPI
|
5 |
from fastapi.responses import HTMLResponse
|
6 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
@app.get("/")
|
11 |
def index():
|
12 |
+
return HTMLResponse("""
|
13 |
+
<p>Backend for gradio theme gallery.
|
14 |
+
<a href="https://huggingface.co/spaces/freddyaboulton/theme-gallery-static">https://huggingface.co/spaces/freddyaboulton/theme-gallery-stati</a>
|
15 |
+
</p>""")
|
16 |
|
17 |
scheduler = BackgroundScheduler()
|
18 |
scheduler.add_job(func=process_spaces, trigger="interval", seconds=360)
|
processing.py
CHANGED
@@ -1,21 +1,20 @@
|
|
1 |
from __future__ import annotations
|
2 |
|
3 |
-
from typing import List,
|
4 |
|
5 |
import huggingface_hub
|
6 |
from huggingface_hub.hf_api import SpaceInfo
|
7 |
-
import requests
|
8 |
from concurrent.futures import ThreadPoolExecutor
|
9 |
import os
|
10 |
-
import time
|
11 |
-
import random
|
12 |
import json
|
13 |
import datetime
|
|
|
14 |
|
15 |
-
class
|
16 |
-
|
|
|
17 |
subdomain: str
|
18 |
-
|
19 |
|
20 |
repo = huggingface_hub.Repository(
|
21 |
local_dir="data",
|
@@ -29,49 +28,34 @@ repo.git_pull()
|
|
29 |
api = huggingface_hub.HfApi(token=os.getenv("HF_TOKEN"))
|
30 |
|
31 |
def get_theme_preview_spaces() -> List[SpaceInfo]:
|
32 |
-
return api.list_spaces(filter="gradio-theme")
|
33 |
|
34 |
-
def
|
35 |
-
time.sleep(random.random())
|
36 |
-
print(f"processing {space_name.id}")
|
37 |
if not space_name.id:
|
38 |
print(f"no space_name for {space_name}")
|
39 |
return None
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
)
|
45 |
-
.json()
|
46 |
-
.get("host")
|
47 |
-
)
|
48 |
-
if not subdomain:
|
49 |
-
print(f"no subdomain for {space_name.id}")
|
50 |
return None
|
51 |
-
return {
|
52 |
-
|
53 |
-
|
54 |
-
def get_all_subdomains(spaces: List[SpaceInfo], subdomains: Dict[str, str]) -> Dict[str, str]:
|
55 |
|
56 |
-
not_parsed_yet = [space for space in spaces if space.id not in subdomains]
|
57 |
|
|
|
58 |
with ThreadPoolExecutor(max_workers=10) as executor:
|
59 |
-
|
60 |
-
|
61 |
-
new_subdomains = {s["name"]: s["subdomain"] for s in new_subdomains if s}
|
62 |
-
|
63 |
-
new_subdomains.update(subdomains)
|
64 |
-
|
65 |
-
return new_subdomains
|
66 |
|
67 |
|
68 |
def process_spaces():
|
69 |
-
theme_spaces = get_theme_preview_spaces()
|
70 |
-
subdomains: Dict[str, str] = json.load(open("data/subdomains.json"))
|
71 |
|
72 |
-
|
73 |
|
74 |
-
json.dump(
|
75 |
repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
|
76 |
|
77 |
|
|
|
1 |
from __future__ import annotations
|
2 |
|
3 |
+
from typing import List, TypedDict
|
4 |
|
5 |
import huggingface_hub
|
6 |
from huggingface_hub.hf_api import SpaceInfo
|
|
|
7 |
from concurrent.futures import ThreadPoolExecutor
|
8 |
import os
|
|
|
|
|
9 |
import json
|
10 |
import datetime
|
11 |
+
import tqdm
|
12 |
|
13 |
+
class SpaceData(TypedDict):
|
14 |
+
id: str
|
15 |
+
likes: int
|
16 |
subdomain: str
|
17 |
+
lastModified: str
|
18 |
|
19 |
repo = huggingface_hub.Repository(
|
20 |
local_dir="data",
|
|
|
28 |
api = huggingface_hub.HfApi(token=os.getenv("HF_TOKEN"))
|
29 |
|
30 |
def get_theme_preview_spaces() -> List[SpaceInfo]:
|
31 |
+
return list(iter(api.list_spaces(filter="gradio-theme")))
|
32 |
|
33 |
+
def get_info(space_name: SpaceInfo) -> SpaceData | None:
|
|
|
|
|
34 |
if not space_name.id:
|
35 |
print(f"no space_name for {space_name}")
|
36 |
return None
|
37 |
+
space_info = api.space_info(space_name.id, token=os.getenv("HF_TOKEN"))
|
38 |
+
subdomain: str | None = getattr(space_info, "subdomain", None)
|
39 |
+
if subdomain is None:
|
40 |
+
print(f"no subdomain for {space_info.id}")
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
return None
|
42 |
+
return {"id": space_info.id, "likes": space_info.likes,
|
43 |
+
"subdomain": f"https://{space_info.subdomain}.hf.space",
|
44 |
+
"lastModified": space_info.lastModified} # type: ignore
|
|
|
45 |
|
|
|
46 |
|
47 |
+
def get_all_info(spaces: List[SpaceInfo]) -> List[SpaceData]:
|
48 |
with ThreadPoolExecutor(max_workers=10) as executor:
|
49 |
+
all_info = list(tqdm.tqdm(executor.map(get_info, spaces), total=len(spaces)))
|
50 |
+
return [info for info in all_info if info]
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
|
53 |
def process_spaces():
|
54 |
+
theme_spaces = list(iter(get_theme_preview_spaces()))
|
|
|
55 |
|
56 |
+
all_info = get_all_info(theme_spaces)
|
57 |
|
58 |
+
json.dump(all_info, open("data/subdomains.json", "w"))
|
59 |
repo.push_to_hub(blocking=False, commit_message=f"Updating data at {datetime.datetime.now()}")
|
60 |
|
61 |
|