hysts commited on
Commit
0db648b
1 Parent(s): e5e5805
Files changed (4) hide show
  1. .pre-commit-config.yaml +46 -0
  2. .style.yapf +5 -0
  3. app.py +94 -0
  4. assets/template.py +36 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ exclude: ^stylegan_xl
2
+ repos:
3
+ - repo: https://github.com/pre-commit/pre-commit-hooks
4
+ rev: v4.2.0
5
+ hooks:
6
+ - id: check-executables-have-shebangs
7
+ - id: check-json
8
+ - id: check-merge-conflict
9
+ - id: check-shebang-scripts-are-executable
10
+ - id: check-toml
11
+ - id: check-yaml
12
+ - id: double-quote-string-fixer
13
+ - id: end-of-file-fixer
14
+ - id: mixed-line-ending
15
+ args: ['--fix=lf']
16
+ - id: requirements-txt-fixer
17
+ - id: trailing-whitespace
18
+ - repo: https://github.com/myint/docformatter
19
+ rev: v1.4
20
+ hooks:
21
+ - id: docformatter
22
+ args: ['--in-place']
23
+ - repo: https://github.com/pycqa/isort
24
+ rev: 5.10.1
25
+ hooks:
26
+ - id: isort
27
+ - repo: https://github.com/pre-commit/mirrors-mypy
28
+ rev: v0.812
29
+ hooks:
30
+ - id: mypy
31
+ args: ['--ignore-missing-imports']
32
+ - repo: https://github.com/google/yapf
33
+ rev: v0.32.0
34
+ hooks:
35
+ - id: yapf
36
+ args: ['--parallel', '--in-place']
37
+ - repo: https://github.com/kynan/nbstripout
38
+ rev: 0.5.0
39
+ hooks:
40
+ - id: nbstripout
41
+ args: ['--extra-keys', 'metadata.interpreter metadata.kernelspec cell.metadata.pycharm']
42
+ - repo: https://github.com/nbQA-dev/nbQA
43
+ rev: 1.3.1
44
+ hooks:
45
+ - id: nbqa-isort
46
+ - id: nbqa-yapf
.style.yapf ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ [style]
2
+ based_on_style = pep8
3
+ blank_line_before_nested_class_or_def = false
4
+ spaces_before_comment = 2
5
+ split_before_logical_operator = true
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import shutil
6
+ import tempfile
7
+
8
+ import gradio as gr
9
+ from huggingface_hub import HfApi
10
+
11
+ title = 'Model Demo Creation'
12
+ description = 'This is a Space that creates demo apps for models in Model Hub.'
13
+ article = ''
14
+ examples = [
15
+ [
16
+ '',
17
+ 'google/vit-base-patch16-224',
18
+ '',
19
+ 'Demo for google/vit-base-patch16-224',
20
+ '',
21
+ '',
22
+ ],
23
+ [
24
+ '',
25
+ 'google/vit-base-patch16-224, microsoft/resnet-50',
26
+ '',
27
+ 'Compare Image Classification Models',
28
+ '',
29
+ '',
30
+ ],
31
+ [
32
+ '',
33
+ 'EleutherAI/gpt-j-6B, EleutherAI/gpt-neo-1.3B',
34
+ '',
35
+ 'Compare Text Generation Models',
36
+ '',
37
+ '',
38
+ ],
39
+ ]
40
+
41
+ api = HfApi()
42
+
43
+
44
+ def save_space_info(dirname: str, filename: str, content: str) -> None:
45
+ with open(f'{dirname}/{filename}', 'w') as f:
46
+ f.write(content)
47
+
48
+
49
+ def run(space_name: str, model_names_str: str, hf_token: str, title: str,
50
+ description: str, article: str) -> str:
51
+ model_names = [name.strip() for name in model_names_str.split(',')]
52
+ model_names_str = '\n'.join(model_names)
53
+
54
+ try:
55
+ space_url = api.create_repo(repo_id=space_name,
56
+ repo_type='space',
57
+ private=True,
58
+ token=hf_token,
59
+ space_sdk='gradio')
60
+ except Exception as e:
61
+ return str(e)
62
+
63
+ with tempfile.TemporaryDirectory() as temp_dir:
64
+ shutil.copy('assets/template.py', f'{temp_dir}/app.py')
65
+ save_space_info(temp_dir, 'TITLE', title)
66
+ save_space_info(temp_dir, 'DESCRIPTION', description)
67
+ save_space_info(temp_dir, 'ARTICLE', article)
68
+ save_space_info(temp_dir, 'MODEL_NAMES', model_names_str)
69
+ api.upload_folder(repo_id=space_name,
70
+ folder_path=temp_dir,
71
+ path_in_repo='.',
72
+ token=hf_token,
73
+ repo_type='space')
74
+
75
+ return f'Successfully created: {space_url}'
76
+
77
+
78
+ gr.Interface(
79
+ fn=run,
80
+ inputs=[
81
+ gr.Textbox(label='Space Name'),
82
+ gr.Textbox(label='Model Names'),
83
+ gr.Textbox(label='HF TOKEN'),
84
+ gr.Textbox(label='Title (Optional)'),
85
+ gr.Textbox(label='Description (Optional)'),
86
+ gr.Textbox(label='Article (Optional)'),
87
+ ],
88
+ outputs=gr.Textbox(label='Output'),
89
+ title=title,
90
+ description=description,
91
+ article=article,
92
+ examples=examples,
93
+ cache_examples=False,
94
+ ).launch(enable_queue=True, share=False)
assets/template.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ from __future__ import annotations
4
+
5
+ import gradio as gr
6
+
7
+
8
+ def read_info(file_name: str) -> str:
9
+ with open(file_name) as f:
10
+ content = f.read()
11
+ return content
12
+
13
+
14
+ def load_model(model_name: str) -> gr.Interface:
15
+ iface = gr.Interface.load(model_name, src='models')
16
+ for component in iface.output_components:
17
+ component.label = f'{component.label} ({model_name})'
18
+ return iface
19
+
20
+
21
+ def load_models(model_names: list[str]) -> list[gr.Interface]:
22
+ return [load_model(name) for name in model_names]
23
+
24
+
25
+ title = read_info('TITLE')
26
+ description = read_info('DESCRIPTION')
27
+ article = read_info('ARTICLE')
28
+ model_names = read_info('MODEL_NAMES').split('\n')
29
+
30
+ interfaces = load_models(model_names)
31
+ gr.Parallel(
32
+ *interfaces,
33
+ title=title,
34
+ description=description,
35
+ article=article,
36
+ ).launch()