Spaces:
Runtime error
Runtime error
Update
Browse filesFiles changed (5)
- .pre-commit-config.yaml+3-12
- README.md+4-1
- app.py+69-102
- model.py+9-14
- requirements.txt+5-5
20 unmodified lines212223242526272829303132333435363738394041424344454620 unmodified lines- id: docformatterargs: ['--in-place']- repo: https://github.com/pycqa/isortrev: 5.10.1hooks:- id: isort- repo: https://github.com/pre-commit/mirrors-mypyrev: v0.812hooks:- id: mypyargs: ['--ignore-missing-imports']- repo: https://github.com/google/yapfrev: v0.32.0hooks:- id: yapfargs: ['--parallel', '--in-place']- repo: https://github.com/kynan/nbstripoutrev: 0.5.0hooks:- id: nbstripoutargs: ['--extra-keys', 'metadata.interpreter metadata.kernelspec cell.metadata.pycharm']- repo: https://github.com/nbQA-dev/nbQArev: 1.3.1hooks:- id: nbqa-isort- id: nbqa-yapf20 unmodified lines212223242526272829303132333435363720 unmodified lines- id: docformatterargs: ['--in-place']- repo: https://github.com/pycqa/isortrev: 5.12.0hooks:- id: isort- repo: https://github.com/pre-commit/mirrors-mypyrev: v0.991hooks:- id: mypyargs: ['--ignore-missing-imports']additional_dependencies: ['types-python-slugify']- repo: https://github.com/google/yapfrev: v0.32.0hooks:- id: yapfargs: ['--parallel', '--in-place']
3 unmodified lines4567891011123 unmodified linescolorFrom: indigocolorTo: redsdk: gradiosdk_version: 3.1.3app_file: app.pypinned: false---Check out the configuration reference at https://huggingface.co/docs/hub/spaces#reference3 unmodified lines4567891011121314153 unmodified linescolorFrom: indigocolorTo: redsdk: gradiosdk_version: 3.35.2app_file: app.pypinned: falsesuggested_hardware: t4-small---Check out the configuration reference at https://huggingface.co/docs/hub/spaces#referencehttps://arxiv.org/abs/2202.12211
1 unmodified line23456781 unmodified line101112131415161718192021222324252627282930313223 unmodified lines56575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421 unmodified linefrom __future__ import annotationsimport argparseimport pathlibimport gradio as gr1 unmodified linefrom model import ModelTITLE = '# Self-Distilled StyleGAN'DESCRIPTION = '''This is an unofficial demo for [https://github.com/self-distilled-stylegan/self-distilled-internet-photos](https://github.com/self-distilled-stylegan/self-distilled-internet-photos).Expected execution time on Hugging Face Spaces: 2s'''FOOTER = '<img id="visitor-badge" src="https://visitor-badge.glitch.me/badge?page_id=hysts.self-distilled-stylegan" alt="visitor badge" />'def parse_args() -> argparse.Namespace:parser = argparse.ArgumentParser()parser.add_argument('--device', type=str, default='cpu')parser.add_argument('--theme', type=str)parser.add_argument('--share', action='store_true')parser.add_argument('--port', type=int)parser.add_argument('--disable-queue',dest='enable_queue',action='store_false')return parser.parse_args()def get_sample_image_url(name: str) -> str:23 unmodified linesreturn f''def main():args = parse_args()model = Model(args.device)with gr.Blocks(theme=args.theme, css='style.css') as demo:gr.Markdown(TITLE)gr.Markdown(DESCRIPTION)with gr.Tabs():with gr.TabItem('App'):with gr.Row():with gr.Column():with gr.Group():model_name = gr.Dropdown(model.MODEL_NAMES,value=model.MODEL_NAMES[0],label='Model')seed = gr.Slider(0,np.iinfo(np.uint32).max,value=0,step=1,label='Seed')psi = gr.Slider(0,2,step=0.05,value=0.7,label='Truncation psi')truncation_type = gr.Dropdown(model.TRUNCATION_TYPES,value=model.TRUNCATION_TYPES[0],label='Truncation Type')run_button = gr.Button('Run')with gr.Column():result = gr.Image(label='Result', elem_id='result')with gr.TabItem('Sample Images'):with gr.Row():paths = sorted(pathlib.Path('samples').glob('*'))names = [path.stem for path in paths]model_name2 = gr.Dropdown(names,value='dogs_1024_multimodal_lpips',label='Type')with gr.Row():text = get_sample_image_markdown(model_name2.value)sample_images = gr.Markdown(text)with gr.TabItem('Cluster Center Images'):with gr.Row():model_name3 = gr.Dropdown(model.MODEL_NAMES,value=model.MODEL_NAMES[0],label='Model')with gr.Row():text = get_cluster_center_image_markdown(model_name3.value)cluster_center_images = gr.Markdown(value=text)gr.Markdown(FOOTER)model_name.change(fn=model.set_model, inputs=model_name, outputs=None)run_button.click(fn=model.set_model_and_generate_image,inputs=[model_name,seed,psi,truncation_type,],outputs=result)model_name2.change(fn=get_sample_image_markdown,inputs=model_name2,outputs=sample_images)model_name3.change(fn=get_cluster_center_image_markdown,inputs=model_name3,outputs=cluster_center_images)demo.launch(enable_queue=args.enable_queue,server_port=args.port,share=args.share,)if __name__ == '__main__':main()1 unmodified line2345671 unmodified line910111213141523 unmodified lines394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091 unmodified linefrom __future__ import annotationsimport pathlibimport gradio as gr1 unmodified linefrom model import ModelDESCRIPTION = '# [Self-Distilled StyleGAN](https://github.com/self-distilled-stylegan/self-distilled-internet-photos)'def get_sample_image_url(name: str) -> str:23 unmodified linesreturn f''model = Model()with gr.Blocks(css='style.css') as demo:gr.Markdown(DESCRIPTION)with gr.Tabs():with gr.TabItem('App'):with gr.Row():with gr.Column():with gr.Group():model_name = gr.Dropdown(label='Model',choices=model.MODEL_NAMES,value=model.MODEL_NAMES[0])seed = gr.Slider(label='Seed',minimum=0,maximum=np.iinfo(np.uint32).max,step=1,value=0)psi = gr.Slider(label='Truncation psi',minimum=0,maximum=2,step=0.05,value=0.7)truncation_type = gr.Dropdown(label='Truncation Type',choices=model.TRUNCATION_TYPES,value=model.TRUNCATION_TYPES[0])run_button = gr.Button('Run')with gr.Column():result = gr.Image(label='Result', elem_id='result')with gr.TabItem('Sample Images'):with gr.Row():paths = sorted(pathlib.Path('samples').glob('*'))names = [path.stem for path in paths]model_name2 = gr.Dropdown(label='Type',choices=names,value='dogs_1024_multimodal_lpips')with gr.Row():text = get_sample_image_markdown(model_name2.value)sample_images = gr.Markdown(text)with gr.TabItem('Cluster Center Images'):with gr.Row():model_name3 = gr.Dropdown(label='Model',choices=model.MODEL_NAMES,value=model.MODEL_NAMES[0])with gr.Row():text = get_cluster_center_image_markdown(model_name3.value)cluster_center_images = gr.Markdown(value=text)model_name.change(fn=model.set_model, inputs=model_name)run_button.click(fn=model.set_model_and_generate_image,inputs=[model_name,seed,psi,truncation_type,],outputs=result)model_name2.change(fn=get_sample_image_markdown,inputs=model_name2,outputs=sample_images)model_name3.change(fn=get_cluster_center_image_markdown,inputs=model_name3,outputs=cluster_center_images)demo.queue(max_size=10).launch()
1234568 unmodified lines151617181920212237 unmodified lines60616263646566679 unmodified lines7778798081828384851 unmodified line87888990919293949596979899100101102103from __future__ import annotationsimport osimport pathlibimport pickleimport sys8 unmodified linessubmodule_dir = current_dir / 'stylegan3'sys.path.insert(0, submodule_dir.as_posix())HF_TOKEN = os.environ['HF_TOKEN']class LPIPS(lpips.LPIPS):@staticmethod37 unmodified lines'Global',]def __init__(self, device: str | torch.device):self.device = torch.device(device)self._download_all_models()self._download_all_cluster_centers()self._download_all_cluster_center_images()9 unmodified lines)def _load_model(self, model_name: str) -> nn.Module:path = hf_hub_download('hysts/Self-Distilled-StyleGAN',f'models/{model_name}_pytorch.pkl',use_auth_token=HF_TOKEN)with open(path, 'rb') as f:model = pickle.load(f)['G_ema']model.eval()1 unmodified linereturn modeldef _load_cluster_centers(self, model_name: str) -> torch.Tensor:path = hf_hub_download('hysts/Self-Distilled-StyleGAN',f'cluster_centers/{model_name}.npy',use_auth_token=HF_TOKEN)centers = np.load(path)centers = torch.from_numpy(centers).float().to(self.device)return centersdef _load_cluster_center_images(self, model_name: str) -> np.ndarray:path = hf_hub_download('hysts/Self-Distilled-StyleGAN',f'cluster_center_images/{model_name}.npy',use_auth_token=HF_TOKEN)return np.load(path)def set_model(self, model_name: str) -> None:123458 unmodified lines14151617181937 unmodified lines5758596061626364659 unmodified lines75767778798081821 unmodified line848586878889909192939495969798from __future__ import annotationsimport pathlibimport pickleimport sys8 unmodified linessubmodule_dir = current_dir / 'stylegan3'sys.path.insert(0, submodule_dir.as_posix())class LPIPS(lpips.LPIPS):@staticmethod37 unmodified lines'Global',]def __init__(self):self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')self._download_all_models()self._download_all_cluster_centers()self._download_all_cluster_center_images()9 unmodified lines)def _load_model(self, model_name: str) -> nn.Module:path = hf_hub_download('public-data/Self-Distilled-StyleGAN',f'models/{model_name}_pytorch.pkl')with open(path, 'rb') as f:model = pickle.load(f)['G_ema']model.eval()1 unmodified linereturn modeldef _load_cluster_centers(self, model_name: str) -> torch.Tensor:path = hf_hub_download('public-data/Self-Distilled-StyleGAN',f'cluster_centers/{model_name}.npy')centers = np.load(path)centers = torch.from_numpy(centers).float().to(self.device)return centersdef _load_cluster_center_images(self, model_name: str) -> np.ndarray:path = hf_hub_download('public-data/Self-Distilled-StyleGAN',f'cluster_center_images/{model_name}.npy')return np.load(path)def set_model(self, model_name: str) -> None:
123456lpips==0.1.4numpy==1.22.3Pillow==9.0.1scipy==1.8.0torch==1.11.0torchvision==0.12.0123456lpips==0.1.4numpy==1.23.5Pillow==10.0.0scipy==1.10.1torch==2.0.1torchvision==0.15.2
.pre-commit-config.yaml+3-12
README.md+4-1
app.py+69-102
model.py+9-14
requirements.txt+5-5