CoolFace
Apppublic

realfill-library/RealFill-Training-UI

sourceHugging Facemitupdated 2y agoView on Hugging Face
5likes
app.py76 linesDownload Raw Back to root
1#!/usr/bin/env python2 3from __future__ import annotations4 5import os6 7import gradio as gr8import torch9 10from app_inference import create_inference_demo11from app_training import create_training_demo12from app_upload import create_upload_demo13from inference import InferencePipeline14from trainer import Trainer15 16TITLE = '# RealFill Training UI'17 18ORIGINAL_SPACE_ID = 'realfill-library/RealFill-Training-UI'19SPACE_ID = os.getenv('SPACE_ID', ORIGINAL_SPACE_ID)20SHARED_UI_WARNING = f'''# Attention - This Space doesn't work in this shared UI. You can duplicate and use it with a paid private T4 GPU.21<center><a class="duplicate-button" style="display:inline-block" target="_blank" href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></center>22'''23 24if os.getenv('SYSTEM') == 'spaces' and SPACE_ID != ORIGINAL_SPACE_ID:25    SETTINGS = f'<a href="https://huggingface.co/spaces/{SPACE_ID}/settings">Settings</a>'26else:27    SETTINGS = 'Settings'28CUDA_NOT_AVAILABLE_WARNING = f'''# Attention - Running on CPU.29<center>30You can assign a GPU in the {SETTINGS} tab if you are running this on HF Spaces.31"T4 small" is sufficient to run this demo.32</center>33'''34 35HF_TOKEN_NOT_SPECIFIED_WARNING = f'''# Attention - The environment variable `HF_TOKEN` is not specified. Please specify your Hugging Face token with write permission as the value of it.36<center>37You can check and create your Hugging Face tokens <a href="https://huggingface.co/settings/tokens" target="_blank">here</a>.38You can specify environment variables in the "Repository secrets" section of the {SETTINGS} tab.39</center>40'''41 42HF_TOKEN = os.getenv('HF_TOKEN')43 44 45def show_warning(warning_text: str) -> gr.Blocks:46    with gr.Blocks() as demo:47        with gr.Box():48            gr.Markdown(warning_text)49    return demo50 51 52pipe = InferencePipeline(HF_TOKEN)53trainer = Trainer(HF_TOKEN)54 55with gr.Blocks(css='style.css') as demo:56    if os.getenv('IS_SHARED_UI'):57        show_warning(SHARED_UI_WARNING)58    if not torch.cuda.is_available():59        show_warning(CUDA_NOT_AVAILABLE_WARNING)60    if not HF_TOKEN:61        show_warning(HF_TOKEN_NOT_SPECIFIED_WARNING)62 63    gr.Markdown(TITLE)64    with gr.Tabs():65        with gr.TabItem('Train'):66            create_training_demo(trainer, pipe)67        with gr.TabItem('Test'):68            create_inference_demo(pipe, HF_TOKEN)69        with gr.TabItem('Upload'):70            gr.Markdown('''71            - You can use this tab to upload models later if you choose not to upload models in training time or if upload in training time failed.72            ''')73            create_upload_demo(HF_TOKEN)74 75demo.queue(max_size=1).launch(share=False)76