ainz commited on
Commit
3cd5c6f
·
verified ·
1 Parent(s): 49185a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -38
app.py CHANGED
@@ -1,47 +1,116 @@
1
- import os
2
  import gradio as gr
3
- from huggingface_hub import login
4
- from zerogpu import ZeroGPU # Import ZeroGPU
 
 
 
5
 
6
- # Initialize ZeroGPU
7
- zero_gpu = ZeroGPU()
8
 
9
- # Gradio demo definition
10
- def create_demo():
11
- with gr.Blocks() as demo:
12
- gr.Markdown("# Flex.1-alpha Text to Image Generation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  with gr.Row():
15
- with gr.Column():
16
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...")
17
- negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What you don't want to see in the image...")
18
-
19
- with gr.Row():
20
- generate = gr.Button("Generate")
21
- clear = gr.Button("Clear")
22
 
23
- with gr.Column():
24
- result = gr.Image(label="Generated Image")
25
 
26
- def mock_generation(prompt, negative_prompt):
27
- return None # Return None as we can't generate images in zero GPU environment
28
 
29
- generate.click(
30
- fn=mock_generation,
31
- inputs=[prompt, negative_prompt],
32
- outputs=result
33
- )
34
- clear.click(lambda: ["", ""], outputs=[prompt, negative_prompt])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
- return demo
37
-
38
- # Create and launch the demo
39
- if __name__ == "__main__":
40
- demo = create_demo()
41
- # Configure the launch settings for Spaces
42
- demo.launch(
43
- server_name="0.0.0.0", # Required for Spaces
44
- server_port=7860, # Default Spaces port
45
- share=False, # Don't create a public link
46
- enable_queue=True # Enable queuing for better handling of multiple users
47
- )
 
 
 
 
 
 
1
  import gradio as gr
2
+ import numpy as np
3
+ import random
4
+ import spaces
5
+ import torch
6
+ from diffusers import DiffusionPipeline
7
 
8
+ dtype = torch.bfloat16
9
+ device = "cuda" if torch.cuda.is_available() else "cpu"
10
 
11
+ # Changed model to Flex.1-alpha
12
+ pipe = DiffusionPipeline.from_pretrained("ostris/Flex.1-alpha", torch_dtype=dtype).to(device)
13
+
14
+ MAX_SEED = np.iinfo(np.int32).max
15
+ MAX_IMAGE_SIZE = 2048
16
+
17
+ @spaces.GPU()
18
+ def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=20, progress=gr.Progress(track_tqdm=True)):
19
+ if randomize_seed:
20
+ seed = random.randint(0, MAX_SEED)
21
+ generator = torch.Generator().manual_seed(seed)
22
+ image = pipe(
23
+ prompt=prompt,
24
+ width=width,
25
+ height=height,
26
+ num_inference_steps=num_inference_steps,
27
+ generator=generator,
28
+ ).images[0]
29
+ return image, seed
30
+
31
+ examples = [
32
+ "a detailed portrait of a cyberpunk character",
33
+ "a serene landscape with mountains and lakes",
34
+ "a futuristic city at night with neon lights",
35
+ ]
36
+
37
+ css="""
38
+ #col-container {
39
+ margin: 0 auto;
40
+ max-width: 520px;
41
+ }
42
+ """
43
+
44
+ with gr.Blocks(css=css) as demo:
45
+
46
+ with gr.Column(elem_id="col-container"):
47
+ gr.Markdown(f"""# Flex.1-alpha
48
+ A fine-tuned version of FLUX optimized for high-quality image generation
49
+ """)
50
 
51
  with gr.Row():
52
+ prompt = gr.Text(
53
+ label="Prompt",
54
+ show_label=False,
55
+ max_lines=1,
56
+ placeholder="Enter your prompt",
57
+ container=False,
58
+ )
59
 
60
+ run_button = gr.Button("Run", scale=0)
 
61
 
62
+ result = gr.Image(label="Result", show_label=False)
 
63
 
64
+ with gr.Accordion("Advanced Settings", open=False):
65
+ seed = gr.Slider(
66
+ label="Seed",
67
+ minimum=0,
68
+ maximum=MAX_SEED,
69
+ step=1,
70
+ value=0,
71
+ )
72
+
73
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
74
+
75
+ with gr.Row():
76
+ width = gr.Slider(
77
+ label="Width",
78
+ minimum=256,
79
+ maximum=MAX_IMAGE_SIZE,
80
+ step=32,
81
+ value=1024,
82
+ )
83
+
84
+ height = gr.Slider(
85
+ label="Height",
86
+ minimum=256,
87
+ maximum=MAX_IMAGE_SIZE,
88
+ step=32,
89
+ value=1024,
90
+ )
91
+
92
+ with gr.Row():
93
+ num_inference_steps = gr.Slider(
94
+ label="Number of inference steps",
95
+ minimum=1,
96
+ maximum=50,
97
+ step=1,
98
+ value=20, # Changed default to 20 as Flex might need more steps
99
+ )
100
 
101
+ gr.Examples(
102
+ examples=examples,
103
+ fn=infer,
104
+ inputs=[prompt],
105
+ outputs=[result, seed],
106
+ cache_examples="lazy"
107
+ )
108
+
109
+ gr.on(
110
+ triggers=[run_button.click, prompt.submit],
111
+ fn=infer,
112
+ inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps],
113
+ outputs=[result, seed]
114
+ )
115
+
116
+ demo.launch()