zR commited on
Commit
6359a1f
1 Parent(s): 6c17684
Files changed (2) hide show
  1. app.py +87 -60
  2. requirements.txt +4 -4
app.py CHANGED
@@ -1,51 +1,75 @@
 
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- #import spaces #[uncomment to use ZeroGPU]
5
- from diffusers import DiffusionPipeline
6
  import torch
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
- model_repo_id = "stabilityai/sdxl-turbo" #Replace to the model you would like to use
10
 
11
  if torch.cuda.is_available():
12
  torch_dtype = torch.float16
13
  else:
14
  torch_dtype = torch.float32
15
 
16
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
17
  pipe = pipe.to(device)
18
 
19
  MAX_SEED = np.iinfo(np.int32).max
20
- MAX_IMAGE_SIZE = 1024
21
 
22
- #@spaces.GPU #[uncomment to use ZeroGPU]
23
- def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  if randomize_seed:
26
  seed = random.randint(0, MAX_SEED)
27
-
28
  generator = torch.Generator().manual_seed(seed)
29
-
30
  image = pipe(
31
- prompt = prompt,
32
- negative_prompt = negative_prompt,
33
- guidance_scale = guidance_scale,
34
- num_inference_steps = num_inference_steps,
35
- width = width,
36
- height = height,
37
- generator = generator
38
- ).images[0]
39
-
40
  return image, seed
41
 
 
42
  examples = [
43
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
44
- "An astronaut riding a green horse",
45
- "A delicious ceviche cheesecake slice",
46
  ]
47
 
48
- css="""
49
  #col-container {
50
  margin: 0 auto;
51
  max-width: 640px;
@@ -53,14 +77,28 @@ css="""
53
  """
54
 
55
  with gr.Blocks(css=css) as demo:
56
-
57
  with gr.Column(elem_id="col-container"):
58
  gr.Markdown(f"""
59
- # Text-to-Image Gradio Template
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  """)
61
-
62
  with gr.Row():
63
-
64
  prompt = gr.Text(
65
  label="Prompt",
66
  show_label=False,
@@ -68,20 +106,11 @@ with gr.Blocks(css=css) as demo:
68
  placeholder="Enter your prompt",
69
  container=False,
70
  )
71
-
72
  run_button = gr.Button("Run", scale=0)
73
-
74
  result = gr.Image(label="Result", show_label=False)
75
 
76
  with gr.Accordion("Advanced Settings", open=False):
77
-
78
- negative_prompt = gr.Text(
79
- label="Negative prompt",
80
- max_lines=1,
81
- placeholder="Enter a negative prompt",
82
- visible=False,
83
- )
84
-
85
  seed = gr.Slider(
86
  label="Seed",
87
  minimum=0,
@@ -89,54 +118,52 @@ with gr.Blocks(css=css) as demo:
89
  step=1,
90
  value=0,
91
  )
92
-
93
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
94
-
95
  with gr.Row():
96
-
97
  width = gr.Slider(
98
  label="Width",
99
- minimum=256,
100
- maximum=MAX_IMAGE_SIZE,
101
  step=32,
102
- value=1024, #Replace with defaults that work for your model
103
  )
104
-
105
  height = gr.Slider(
106
  label="Height",
107
- minimum=256,
108
- maximum=MAX_IMAGE_SIZE,
109
  step=32,
110
- value=1024, #Replace with defaults that work for your model
111
  )
112
-
113
  with gr.Row():
114
-
115
  guidance_scale = gr.Slider(
116
  label="Guidance scale",
117
  minimum=0.0,
118
  maximum=10.0,
119
  step=0.1,
120
- value=0.0, #Replace with defaults that work for your model
121
  )
122
-
123
  num_inference_steps = gr.Slider(
124
  label="Number of inference steps",
125
- minimum=1,
126
- maximum=50,
127
  step=1,
128
- value=2, #Replace with defaults that work for your model
129
  )
130
-
131
  gr.Examples(
132
- examples = examples,
133
- inputs = [prompt]
134
  )
135
  gr.on(
136
  triggers=[run_button.click, prompt.submit],
137
- fn = infer,
138
- inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
139
- outputs = [result, seed]
140
  )
141
 
142
- demo.queue().launch()
 
1
+ import os
2
+ import threading
3
+ import time
4
+ from datetime import datetime, timedelta
5
+
6
  import gradio as gr
7
  import numpy as np
8
  import random
9
+ import spaces # [uncomment to use ZeroGPU]
10
+ from diffusers import FluxPipeline
11
  import torch
12
 
13
  device = "cuda" if torch.cuda.is_available() else "cpu"
14
+ model_repo_id = "black-forest-labs/FLUX.1-dev"
15
 
16
  if torch.cuda.is_available():
17
  torch_dtype = torch.float16
18
  else:
19
  torch_dtype = torch.float32
20
 
21
+ pipe = FluxPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
22
  pipe = pipe.to(device)
23
 
24
  MAX_SEED = np.iinfo(np.int32).max
 
25
 
 
 
26
 
27
+ def delete_old_files():
28
+ while True:
29
+ now = datetime.now()
30
+ cutoff = now - timedelta(minutes=10)
31
+ directories = ["./gradio_tmp"]
32
+
33
+ for directory in directories:
34
+ for filename in os.listdir(directory):
35
+ file_path = os.path.join(directory, filename)
36
+ if os.path.isfile(file_path):
37
+ file_mtime = datetime.fromtimestamp(os.path.getmtime(file_path))
38
+ if file_mtime < cutoff:
39
+ os.remove(file_path)
40
+ time.sleep(600)
41
+
42
+
43
+ threading.Thread(target=delete_old_files, daemon=True).start()
44
+
45
+
46
+ @spaces.GPU # [uncomment to use ZeroGPU]
47
+ def infer(prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps,
48
+ progress=gr.Progress(track_tqdm=True)):
49
  if randomize_seed:
50
  seed = random.randint(0, MAX_SEED)
51
+
52
  generator = torch.Generator().manual_seed(seed)
53
+
54
  image = pipe(
55
+ prompt=prompt,
56
+ guidance_scale=guidance_scale,
57
+ num_images_per_prompt=1,
58
+ num_inference_steps=num_inference_steps,
59
+ width=width,
60
+ height=height,
61
+ generator=generator
62
+ ).images[0]
 
63
  return image, seed
64
 
65
+
66
  examples = [
67
+ "A vintage pink convertible with glossy chrome finishes and whitewall tires sits parked on an open road, surrounded by a field of wildflowers under a clear blue sky. The car's body is a delicate pastel pink, complementing the vibrant greens and colors of the meadow. Its interior boasts cream leather seats and a polished wooden dashboard, evoking a sense of classic elegance. The sun casts a soft light on the vehicle, highlighting its curves and shiny surfaces, creating a picture of nostalgia mixed with dreamy escapism.",
68
+ "A noble black Labrador retriever sits serenely in a sunlit meadow, its glossy coat absorbing the golden rays of a late afternoon sun. The dog's intelligent eyes sparkle with a mixture of curiosity and loyalty, as it gazes off into the distance where the meadow meets a line of tall, slender birch trees. The dog's posture is regal, yet approachable, with its tongue playfully hanging out to the side slightly, suggesting a friendly disposition. The idyllic setting is filled with the vibrant greens of lush grass and the soft colors of wildflowers speckled throughout, creating a peaceful harmony between the dog and its natural surroundings.",
69
+ "A vibrant red-colored dog of medium build stands attentively in an autumn forest setting. Its fur is a deep, rich red, reminiscent of autumn leaves, contrasting with its bright, intelligent eyes, a clear sky blue. The dog's ears perk up, and its tail wags slightly as it looks off into the distance, its posture suggesting alertness and curiosity. Golden sunlight filters through the canopy of russet and gold leaves above, casting dappled light onto the forest floor and the glossy coat of the canine, creating a serene and heartwarming scene."
70
  ]
71
 
72
+ css = """
73
  #col-container {
74
  margin: 0 auto;
75
  max-width: 640px;
 
77
  """
78
 
79
  with gr.Blocks(css=css) as demo:
 
80
  with gr.Column(elem_id="col-container"):
81
  gr.Markdown(f"""
82
+ <div style="text-align: center; font-size: 32px; font-weight: bold; margin-bottom: 20px;">
83
+ CogView3-Plus Huggingface Space🤗
84
+ </div>
85
+ <div style="text-align: center;">
86
+ <a href="https://huggingface.co/THUDM/CogView3-Plus">🤗 Model Hub |
87
+ <a href="https://github.com/THUDM/CogView3">🌐 Github</a> |
88
+ <a href="https://arxiv.org/abs/2403.05121">📜 arxiv </a>
89
+ </div>
90
+ <div style="text-align: center;display: flex;justify-content: center;align-items: center;margin-top: 1em;margin-bottom: .5em;">
91
+ <span>If the Space is too busy, duplicate it to use privately</span>
92
+ <a href="https://huggingface.co/spaces/THUDM-HF-SPACE/CogView-3-Plus?duplicate=true"><img src="https://huggingface.co/datasets/huggingface/badges/resolve/main/duplicate-this-space-lg.svg" width="160" style="
93
+ margin-left: .75em;
94
+ "></a>
95
+ </div>
96
+ <div style="text-align: center; font-size: 15px; font-weight: bold; color: red; margin-bottom: 20px;">
97
+ ⚠️ This demo is for academic research and experiential use only.
98
+ </div>
99
  """)
100
+
101
  with gr.Row():
 
102
  prompt = gr.Text(
103
  label="Prompt",
104
  show_label=False,
 
106
  placeholder="Enter your prompt",
107
  container=False,
108
  )
109
+
110
  run_button = gr.Button("Run", scale=0)
 
111
  result = gr.Image(label="Result", show_label=False)
112
 
113
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
 
 
114
  seed = gr.Slider(
115
  label="Seed",
116
  minimum=0,
 
118
  step=1,
119
  value=0,
120
  )
121
+
122
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
123
+
124
  with gr.Row():
 
125
  width = gr.Slider(
126
  label="Width",
127
+ minimum=512,
128
+ maximum=2048,
129
  step=32,
130
+ value=1024, # 替换为你的模型默认值
131
  )
132
+
133
  height = gr.Slider(
134
  label="Height",
135
+ minimum=512,
136
+ maximum=2048,
137
  step=32,
138
+ value=1024,
139
  )
140
+
141
  with gr.Row():
 
142
  guidance_scale = gr.Slider(
143
  label="Guidance scale",
144
  minimum=0.0,
145
  maximum=10.0,
146
  step=0.1,
147
+ value=7.0,
148
  )
149
+
150
  num_inference_steps = gr.Slider(
151
  label="Number of inference steps",
152
+ minimum=10,
153
+ maximum=100,
154
  step=1,
155
+ value=50,
156
  )
157
+
158
  gr.Examples(
159
+ examples=examples,
160
+ inputs=[prompt]
161
  )
162
  gr.on(
163
  triggers=[run_button.click, prompt.submit],
164
+ fn=infer,
165
+ inputs=[prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
166
+ outputs=[result, seed]
167
  )
168
 
169
+ demo.queue().launch(server_name="127.0.0.1")
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
- accelerate
2
- diffusers
3
- invisible_watermark
4
  torch
5
- transformers
 
6
  xformers
 
1
+ accelerate>=0.34.2
2
+ diffusers>=0.30.3
 
3
  torch
4
+ gradio>=4.44
5
+ transformers>=4.45.2
6
  xformers