WaveCut commited on
Commit
cbf6f6f
·
verified ·
1 Parent(s): d298dcc

Upload folder using huggingface_hub

Browse files
Files changed (2) hide show
  1. app.py +117 -0
  2. requirements.txt +12 -0
app.py ADDED
@@ -0,0 +1,117 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import spaces # Import spaces for ZeroGPU compatibility on Hugging Face
4
+ from models import infer, load_model
5
+
6
+ # Initialize model on startup
7
+ try:
8
+ load_model()
9
+ except Exception as e:
10
+ print(f"Warning: Model failed to load on startup (might load on first request): {e}")
11
+
12
+ # Define the inference function wrapper for Gradio
13
+ # Decorate with @spaces.GPU to enable ZeroGPU usage on Hugging Face Spaces
14
+ @spaces.GPU
15
+ def process_image(prompt, negative_prompt, height, width, steps, guidance_scale, seed):
16
+ try:
17
+ image, used_seed = infer(
18
+ prompt=prompt,
19
+ negative_prompt=negative_prompt,
20
+ height=int(height),
21
+ width=int(width),
22
+ steps=int(steps),
23
+ guidance_scale=float(guidance_scale),
24
+ seed=int(seed)
25
+ )
26
+ return image, f"Seed used: {used_seed}"
27
+ except Exception as e:
28
+ return None, f"Error: {str(e)}"
29
+
30
+ # CSS for styling
31
+ css = """
32
+ .container { max_width: 1200px; margin: auto; }
33
+ .header-link { font-weight: bold; color: #3b82f6; text-decoration: none; }
34
+ .header-link:hover { text-decoration: underline; }
35
+ """
36
+
37
+ # Create the Gradio Interface
38
+ with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
39
+
40
+ # Header Section
41
+ with gr.Row():
42
+ with gr.Column():
43
+ gr.Markdown(
44
+ """
45
+ # 🎨 Kandinsky 5.0 T2I Lite
46
+ ### Text-to-Image Generation using Kandinsky 5.0 Lite SFT Diffusers
47
+ [Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)
48
+ """
49
+ )
50
+
51
+ with gr.Row(elem_classes="container"):
52
+ # Left Column: Inputs
53
+ with gr.Column(scale=1):
54
+ prompt = gr.Textbox(
55
+ label="Prompt",
56
+ placeholder="Describe the image you want to generate...",
57
+ lines=3,
58
+ value="A fluffy, expressive cat wearing a bright red hat with a soft, slightly textured fabric. The hat should look cozy and well-fitted on the cat’s head. On the front of the hat, add clean, bold white text that reads “SWEET”, clearly visible and neatly centered."
59
+ )
60
+
61
+ negative_prompt = gr.Textbox(
62
+ label="Negative Prompt",
63
+ placeholder="What to avoid (e.g., blurry, bad anatomy)...",
64
+ lines=2,
65
+ value="low quality, bad quality, sketches, lowres, blurry"
66
+ )
67
+
68
+ with gr.Accordion("Advanced Settings", open=False):
69
+ with gr.Row():
70
+ height = gr.Slider(label="Height", minimum=512, maximum=2048, step=64, value=1024)
71
+ width = gr.Slider(label="Width", minimum=512, maximum=2048, step=64, value=1024)
72
+
73
+ steps = gr.Slider(label="Inference Steps", minimum=10, maximum=100, step=1, value=50)
74
+ guidance_scale = gr.Slider(label="Guidance Scale", minimum=1.0, maximum=20.0, step=0.1, value=3.5)
75
+ seed = gr.Number(label="Seed", value=-1, precision=0, info="Set to -1 for random")
76
+
77
+ run_btn = gr.Button("Generate Image", variant="primary", size="lg")
78
+
79
+ # Right Column: Output
80
+ with gr.Column(scale=1):
81
+ output_image = gr.Image(label="Generated Image", type="pil")
82
+ status_text = gr.Textbox(label="Status", interactive=False)
83
+
84
+ # Event Handling
85
+ run_btn.click(
86
+ fn=process_image,
87
+ inputs=[prompt, negative_prompt, height, width, steps, guidance_scale, seed],
88
+ outputs=[output_image, status_text]
89
+ )
90
+
91
+ # Examples
92
+ gr.Examples(
93
+ examples=[
94
+ [
95
+ "A fluffy, expressive cat wearing a bright red hat with a soft, slightly textured fabric. The hat should look cozy and well-fitted on the cat’s head. On the front of the hat, add clean, bold white text that reads “SWEET”, clearly visible and neatly centered.",
96
+ "low quality, bad quality",
97
+ 1024, 1024, 50, 3.5, -1
98
+ ],
99
+ [
100
+ "A futuristic cityscape at sunset with flying cars and neon lights, cyberpunk style, highly detailed.",
101
+ "blurry, low resolution, monochrome",
102
+ 1024, 1024, 30, 4.0, -1
103
+ ],
104
+ [
105
+ "A serene landscape painting of a mountain lake with reflection, oil painting style, textured brushstrokes.",
106
+ "photorealistic, vector art, watermark",
107
+ 1024, 1024, 40, 3.0, -1
108
+ ]
109
+ ],
110
+ inputs=[prompt, negative_prompt, height, width, steps, guidance_scale, seed],
111
+ fn=process_image,
112
+ outputs=[output_image, status_text],
113
+ cache_examples=False
114
+ )
115
+
116
+ if __name__ == "__main__":
117
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ spaces
2
+ gradio
3
+ torch
4
+ torchvision
5
+ torchaudio
6
+ git+https://github.com/huggingface/transformers
7
+ accelerate
8
+ tokenizers
9
+ sentencepiece
10
+ requests
11
+ Pillow
12
+ numpy