File size: 2,885 Bytes
0db648b 7728909 0db648b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
#!/usr/bin/env python
from __future__ import annotations
import shutil
import tempfile
import gradio as gr
from huggingface_hub import HfApi
title = 'Model Demo Creation'
description = 'This is a Space that creates demo apps for models in Model Hub.'
article = ''
examples = [
[
'',
'google/vit-base-patch16-224',
'',
'Demo for google/vit-base-patch16-224',
'',
'',
],
[
'',
'google/vit-base-patch16-224, microsoft/resnet-50',
'',
'Compare Image Classification Models',
'',
'',
],
[
'',
'EleutherAI/gpt-j-6B, EleutherAI/gpt-neo-1.3B',
'',
'Compare Text Generation Models',
'',
'',
],
]
api = HfApi()
def save_space_info(dirname: str, filename: str, content: str) -> None:
with open(f'{dirname}/{filename}', 'w') as f:
f.write(content)
def run(space_name: str, model_names_str: str, hf_token: str, title: str,
description: str, article: str) -> str:
model_names = [name.strip() for name in model_names_str.split(',')]
model_names_str = '\n'.join(model_names)
try:
space_url = api.create_repo(repo_id=space_name,
repo_type='space',
private=True,
token=hf_token,
space_sdk='gradio')
except Exception as e:
return str(e)
with tempfile.TemporaryDirectory() as temp_dir:
shutil.copy('assets/template.py', f'{temp_dir}/app.py')
save_space_info(temp_dir, 'TITLE', title)
save_space_info(temp_dir, 'DESCRIPTION', description)
save_space_info(temp_dir, 'ARTICLE', article)
save_space_info(temp_dir, 'MODEL_NAMES', model_names_str)
api.upload_folder(repo_id=space_name,
folder_path=temp_dir,
path_in_repo='.',
token=hf_token,
repo_type='space')
return f'Successfully created: {space_url}'
gr.Interface(
fn=run,
inputs=[
gr.Textbox(label='Space Name', placeholder='<user_name>/<space_name>'),
gr.Textbox(label='Model Names',
placeholder='e.g. microsoft/resnet-50'),
gr.Textbox(
label='Hugging Face Token',
placeholder=
'This should be a token with write permission. See: https://huggingface.co/settings/tokens'
),
gr.Textbox(label='Title (Optional)'),
gr.Textbox(label='Description (Optional)'),
gr.Textbox(label='Article (Optional)'),
],
outputs=gr.Textbox(label='Output'),
title=title,
description=description,
article=article,
examples=examples,
cache_examples=False,
).launch(enable_queue=True, share=False)
|