Spaces:
Running on Zero
A newer version of the Gradio SDK is available: 6.25.0
Fix Issues Plan
Top-Level Overview
Eight issues were identified across app.py and requirements.txt. This plan addresses each one in focused, independently reviewable sub-tasks. The goal is to bring the project to a safe, correct, and deployable state without adding new features or refactors beyond what is needed to fix the identified problems.
Sub-Task 1 β Fix requirements.txt dependency issues
Intent
Correct the four dependency problems: wrong version operator for transformers, unpinned gradio, and missing torch and Pillow.
Expected Outcomes
requirements.txtinstalls correctly on a clean environmentgradioversion matchesREADME.mdsdk_version: 5.32.0torchandPilloware explicitly listed
Todo List
- Change
transformers<=5.5.3βtransformers>=4.40.0 - Change
gradioβgradio==5.32.0 - Add
torch>=2.0.0(HuggingFace Spaces pre-installs it, but it should be declared) - Add
Pillow>=9.0.0
Relevant Context
requirements.txtREADME.mdβsdk_version: 5.32.0
Status β [x] done
Sub-Task 2 β Replace tempfile.mktemp() with safe alternative
Intent
tempfile.mktemp() is deprecated and has a TOCTOU race condition vulnerability. Replace with tempfile.NamedTemporaryFile.
Expected Outcomes
save_png()no longer usesmktemp()- Temp file is created safely with no race condition window
Todo List
- In
save_png(), replacetempfile.mktemp(".png")with aNamedTemporaryFile(suffix=".png", delete=False)pattern that returns the file path
Relevant Context
app.py:53-59βsave_png()function
Status β [x] done
Sub-Task 3 β Fix torch.seed() overflow risk in seed generation
Intent
torch.seed() can return values larger than 2^63 - 1, which may overflow torch.Generator.manual_seed(). Replace with a bounded random integer.
Expected Outcomes
- Random seed is always within a safe range for
manual_seed() - No change to user-visible behaviour
Todo List
- Replace
seed = torch.seed()ingenerate_txt2img()withseed = torch.randint(0, 2**32 - 1, (1,)).item()
Relevant Context
app.py:64-65βgenerate_txt2img()seed handling
Status β [x] done
Sub-Task 4 β Offload pipelines from GPU after inference
Intent
Neither pipeline calls .to("cpu") after inference inside the @spaces.GPU context. Before the ZeroGPU context releases, pipelines should be moved back to CPU and VRAM explicitly cleared to avoid memory pressure between requests.
Expected Outcomes
- After each generation function returns, the pipeline is moved back to CPU
torch.cuda.empty_cache()is called to release VRAM before the ZeroGPU context drops- Both
generate_txt2img()andgenerate_img2img()follow this pattern
Todo List
- In
generate_txt2img(), afterimageis captured, calltxt2img_pipe.to("cpu")thentorch.cuda.empty_cache() - In
generate_img2img(), afterresultis captured, callimg2img_pipe.to("cpu")thentorch.cuda.empty_cache()
Relevant Context
app.py:62-93βgenerate_txt2img()app.py:95-116βgenerate_img2img()
Status β [x] done
Notes on Accepted / Out-of-Scope Issues
- Shared model weights (shallow copy of components) β This is intentional. Sharing weights between the two pipeline instances saves RAM. The schedulers are correctly set independently after construction. No change needed.