Skip to content

Instantly share code, notes, and snippets.

@lewtun
Created November 22, 2022 12:30
Show Gist options
  • Save lewtun/145e2734766203e9f5e945200d89db3a to your computer and use it in GitHub Desktop.
Save lewtun/145e2734766203e9f5e945200d89db3a to your computer and use it in GitHub Desktop.
[HF Course] Format Gradio URLs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"id": "089e4a3f-d26a-4a9f-9813-607dac123d97",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"from tqdm.auto import tqdm\n",
"import glob"
]
},
{
"cell_type": "code",
"execution_count": 49,
"id": "418bba43-f18e-4725-961e-24d4d7e95f83",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3669579ab57544d883a6477590416840",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
" 0%| | 0/629 [00:00<?, ?it/s]"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"paths = list(glob.glob(\"chapters/**/**/*.mdx\"))\n",
"\n",
"for path in tqdm(paths):\n",
" with open(path, \"r\") as f:\n",
" data = f.read()\n",
"\n",
" for line in data.split(\"\\n\"):\n",
" # Skip URLs for images\n",
" if \"resolve\" not in line and \"src=\" in line and \"course-demos\" in line:\n",
" # Extract Space URLs\n",
" old_url = re.findall(r\"(?=src)src=\\\"(?P<src>[^\\\"]+)\", line)\n",
" # Skip formatted URLs\n",
" if old_url[0].endswith(\".hf.space\"):\n",
" continue\n",
" old_url_parts = old_url[0].strip(\"/+\").split(\"/\")\n",
" # URLs cannot have underscores\n",
" space_name = old_url_parts[-1].replace(\"_\", \"-\")\n",
" new_url = f\"https://course-demos-{space_name}.hf.space\"\n",
" data = data.replace(old_url[0], new_url)\n",
"\n",
" with open(path, \"wt\") as f:\n",
" f.write(data)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "hf",
"language": "python",
"name": "hf"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@lewtun
Copy link
Author

lewtun commented Nov 22, 2022

The new_url can't really be guessed from the Space ID, so a more rigorous approach is to ping

https://huggingface.co/api/spaces/course-demos/{space_id}/host

and extract the real URL from there. For now, I've left the code untouched since the required fixes were easy to handle manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment