Spaces:
Running on Zero
Running on Zero
Download app.py from Dekonstruktio/Scale: direct link, hf CLI and curl.
- Browser
- Download file 10 kB
-
https://huggingface.co/spaces/Dekonstruktio/Scale/resolve/main/app.py
- Command line
-
hf download hf://spaces/Dekonstruktio/Scale/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Dekonstruktio/Scale/resolve/main/app.py
10 kB
| import spaces # Must precede torch for ZeroGPU CUDA emulation. | |
| import math | |
| import os | |
| import gradio as gr | |
| import torch | |
| from geometry import output_size | |
| from pipeline import PRESETS, PRESET_FIELDS, run_pipeline, work_estimate | |
| MODELS = {} | |
| if os.environ.get('TILE_V3_UI_ONLY') != '1': | |
| from engine import RestorationModel | |
| torch.set_num_threads(2) | |
| MODELS['Restore'] = RestorationModel('Restore') | |
| MODELS['Fast'] = RestorationModel('Fast') | |
| # The multi-step teacher shares the resident VAE and vision encoder with Restore. | |
| MODELS['Detail'] = RestorationModel('Detail', shared=MODELS['Restore']) | |
| def duration(image, mode='Restore', size='2×', seed=42, restoration=1., color_strength=1., | |
| batch_size=4, passes=3, pass2_strength=.5, pass3_strength=.3, texture_only=True, | |
| detail_boost=.25, local_contrast=.1, overlap=128, variations=1, steps=10, | |
| guidance=.5, weak_condition=.1, preset='Maximum detail', progress=None, **kwargs): | |
| try: | |
| _, seconds = work_estimate(output_size(*image.size, size), mode, passes, overlap, variations, steps, batch_size) | |
| return min(180, max(20, math.ceil(seconds + 10))) | |
| except (AttributeError, ValueError, TypeError, ZeroDivisionError): | |
| return 20 | |
| def upscale(image, mode='Restore', size='2×', seed=42, restoration=1., color_strength=1., | |
| batch_size=4, passes=3, pass2_strength=.5, pass3_strength=.3, texture_only=True, | |
| detail_boost=.25, local_contrast=.1, overlap=128, variations=1, steps=10, | |
| guidance=.5, weak_condition=.1, preset='Maximum detail', progress=gr.Progress()): | |
| try: | |
| return run_pipeline(MODELS, image, mode, size, seed, restoration, color_strength, batch_size, | |
| passes, pass2_strength, pass3_strength, texture_only, detail_boost, | |
| local_contrast, overlap, variations, steps, guidance, weak_condition, preset, progress) | |
| except ValueError as e: | |
| raise gr.Error(str(e)) from e | |
| except torch.cuda.OutOfMemoryError as e: | |
| torch.cuda.empty_cache() | |
| raise gr.Error('GPU memory exceeded. Reduce tile batch size, output size, or use Restore mode.') from e | |
| def select_variant(state, evt: gr.SelectData): | |
| if not state: | |
| return gr.skip(), gr.skip() | |
| index = evt.index[0] if isinstance(evt.index, (list, tuple)) else evt.index | |
| if not isinstance(index, int) or not 0 <= index < len(state['files']): | |
| return gr.skip(), gr.skip() | |
| path = state['files'][index] | |
| return (state['reference'], path), path | |
| CSS = ''' | |
| .gradio-container {max-width: 1280px !important;} | |
| #hero {padding: 12px 0 22px;} | |
| #hero h1 {font-size: clamp(2rem, 4vw, 3.25rem); letter-spacing: -.04em; margin-bottom: .4rem;} | |
| #hero p {font-size: 1.05rem; opacity: .8;} | |
| #run {min-height: 50px; font-size: 17px;} | |
| /* Gradio's first layer is in flex flow, while the second is absolute. | |
| Give both the same viewport so contain sizing and zoom stay registered. */ | |
| #comparison .content > img { | |
| position: absolute !important; | |
| inset: 0 !important; | |
| width: 100% !important; | |
| height: 100% !important; | |
| max-height: none !important; | |
| margin: 0 !important; | |
| object-fit: contain !important; | |
| object-position: center !important; | |
| transform-origin: top left !important; | |
| } | |
| footer {display: none !important;} | |
| ''' | |
| with gr.Blocks(title=' ', theme=gr.themes.Soft(primary_hue='indigo', neutral_hue='slate'), | |
| css=CSS, delete_cache=(3600, 3600)) as demo: | |
| with gr.Row(): | |
| with gr.Column(scale=4): | |
| image = gr.Image(type='pil', image_mode='RGBA', label='Your image', sources=['upload', 'clipboard'], height=300) | |
| preset = gr.Dropdown(list(PRESETS) + ['Custom'], value='Maximum detail', label='Pipeline preset', | |
| info='Presets set starting values. Every control remains adjustable.') | |
| mode = gr.Radio([('Restore', 'Restore'), ('Fast', 'Fast'), ('Detail · slower', 'Detail')], | |
| value='Restore', label='Model', info='Restore uses VOSR 2.0. Detail uses a multi-step first pass, then Restore for refinements.') | |
| size = gr.Dropdown(['2×', '4×', '2048 px long edge', '4096 px long edge'], value='2×', label='Output size') | |
| with gr.Row(): | |
| passes = gr.Radio([1, 2, 3], value=3, label='Restoration passes') | |
| variations = gr.Dropdown([1, 2, 3], value=1, label='Seed variants', info='Distinct alternatives; more variants take longer.') | |
| run = gr.Button('Create detailed image', variant='primary', elem_id='run') | |
| with gr.Column(scale=6): | |
| comparison = gr.ImageSlider(label='Before / after', type='pil', height=460, elem_id='comparison') | |
| with gr.Row(): | |
| download = gr.File(label='Selected full-resolution PNG') | |
| bundle = gr.File(label='All variants + settings ZIP') | |
| status = gr.Markdown('Maximum detail is ready: Restore → two gentle texture passes → mild finishing.') | |
| gallery = gr.Gallery(label='Choose your result', columns=3, height=260, object_fit='contain', preview=False) | |
| selected_state = gr.State() | |
| with gr.Accordion('Refinement & finishing', open=False): | |
| with gr.Row(): | |
| restoration = gr.Slider(0, 1, value=1, step=.05, label='First pass strength') | |
| pass2_strength = gr.Slider(0, 1, value=.5, step=.05, label='Second pass strength') | |
| pass3_strength = gr.Slider(0, 1, value=.3, step=.05, label='Third pass strength') | |
| with gr.Row(): | |
| texture_only = gr.Checkbox(value=True, label='Texture-only refinements', | |
| info='Later passes add small detail while retaining broader shapes.') | |
| color_strength = gr.Slider(0, 1, value=1., step=.05, label='Original color preservation', | |
| info='At 1, keeps original hue, saturation and broad brightness while retaining restored texture.') | |
| with gr.Row(): | |
| detail_boost = gr.Slider(0, 1, value=.25, step=.05, label='Fine-detail boost', | |
| info='Luminance sharpening with local halo limits; increases crispness.') | |
| local_contrast = gr.Slider(0, 1, value=.1, step=.05, label='Local contrast') | |
| with gr.Accordion('Sampling & tiles', open=False): | |
| with gr.Row(): | |
| seed = gr.Number(value=42, precision=0, minimum=0, maximum=2147483647, label='Seed') | |
| overlap = gr.Dropdown([64, 128, 192], value=128, label='Tile overlap (px)', | |
| info='More overlap can improve texture continuity but adds work.') | |
| batch_size = gr.Dropdown([1, 2, 4, 8], value=4, label='Tile batch size') | |
| with gr.Column(visible=False) as sampler: | |
| gr.Markdown('**Detail model only:** these controls affect the multi-step first pass. Subsequent passes use Restore.') | |
| with gr.Row(): | |
| steps = gr.Dropdown([5, 10, 15, 25], value=10, label='Sampling steps') | |
| guidance = gr.Slider(-.5, 2, value=.5, step=.1, label='Fidelity guidance', | |
| info='Higher favors the input; lower gives more generative freedom.') | |
| weak_condition = gr.Slider(.05, .25, value=.1, step=.01, label='Weak conditioning', | |
| info='Higher tends toward fidelity; lower allows freer textures.') | |
| gr.Examples(examples=[['image1.jpg'], ['image2.jpg'], ['image4.jpg'], ['image5.jpg']], inputs=[image], cache_examples=False) | |
| with gr.Accordion('About, models & attribution', open=False): | |
| gr.Markdown('VOSR by Rongyuan Wu and collaborators: **Fast** (0.5B one-step), **Restore** (VOSR 2.0 one-step), ' | |
| '**Detail** (1.4B multi-step). Shared spatial noise and Gaussian tile blending keep overlaps consistent. ' | |
| 'Generated textures can differ from the original.\n\n' | |
| '[VOSR code](https://github.com/cswry/VOSR) · [Weights and licenses](https://huggingface.co/CSWRY/VOSR)\n\n' | |
| 'Hosted interface by [Gökay Aydoğan](https://orcid.org/0000-0002-2343-9433). ' | |
| 'Please cite the hosted interface and upstream projects when used in academic work.\n\n' | |
| '```bibtex\n@software{aydogan2026tileupscalerv3,\n author = {Aydoğan, Gökay},\n' | |
| ' title = {Tile Upscaler v3},\n year = {2026},\n publisher = {Hugging Face},\n' | |
| ' url = {https://huggingface.co/spaces/gokaygokay/SD3-Long-Captioner}\n}\n```') | |
| preset_outputs = [mode, passes, restoration, pass2_strength, pass3_strength, texture_only, | |
| detail_boost, local_contrast, color_strength, overlap, variations, steps, guidance, weak_condition] | |
| def apply_preset(name): | |
| if name not in PRESETS: | |
| return [gr.skip()] * (len(PRESET_FIELDS) + 1) | |
| return list(PRESETS[name]) + [gr.update(visible=PRESETS[name][0] == 'Detail')] | |
| preset.change(apply_preset, inputs=preset, outputs=preset_outputs + [sampler], queue=False, api_name=False) | |
| mode.change(lambda value: gr.update(visible=value == 'Detail'), inputs=mode, outputs=sampler, queue=False, api_name=False) | |
| inputs = [image, mode, size, seed, restoration, color_strength, batch_size, passes, pass2_strength, | |
| pass3_strength, texture_only, detail_boost, local_contrast, overlap, variations, steps, | |
| guidance, weak_condition, preset] | |
| run.click(upscale, inputs=inputs, outputs=[comparison, download, bundle, gallery, status, selected_state], | |
| api_name='upscale', concurrency_limit=1, concurrency_id='restoration') | |
| gallery.select(select_variant, inputs=selected_state, outputs=[comparison, download], queue=False, api_name=False) | |
| if __name__ == '__main__': | |
| demo.queue(max_size=16, default_concurrency_limit=1).launch(show_error=True) | |