daniel-dona commited on
Commit
1483ea1
·
verified ·
1 Parent(s): 7360587

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -60
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
 
4
 
5
  from PIL import Image
6
 
@@ -20,23 +21,24 @@ pipe = DiffusionPipeline.from_pretrained(model_name, torch_dtype=torch.bfloat16)
20
  pipe.to('cuda')
21
 
22
  MAX_SEED = np.iinfo(np.int32).max
23
- MAX_IMAGE_SIZE = 1440
24
 
 
25
 
26
  # (1664, 928), (1472, 1140), (1328, 1328)
27
  def get_image_size(aspect_ratio):
28
  if aspect_ratio == "1:1":
29
- return 1328, 1328
30
  elif aspect_ratio == "16:9":
31
- return 1664, 928
32
  elif aspect_ratio == "9:16":
33
- return 928, 1664
34
  elif aspect_ratio == "4:3":
35
- return 1472, 1140
36
  elif aspect_ratio == "3:4":
37
- return 1140, 1472
38
  else:
39
- return 1328, 1328
40
 
41
  @spaces.GPU(duration=60)
42
  def infer_diffusers(
@@ -71,50 +73,7 @@ def infer_diffusers(
71
  return image, seed
72
 
73
 
74
- @spaces.GPU(duration=65)
75
- def infer(
76
- prompt,
77
- negative_prompt=" ",
78
- seed=42,
79
- randomize_seed=False,
80
- aspect_ratio="16:9",
81
- guidance_scale=4,
82
- num_inference_steps=50,
83
- progress=gr.Progress(track_tqdm=True),
84
- ):
85
- if randomize_seed:
86
- seed = random.randint(0, MAX_SEED)
87
- width, height = get_image_size(aspect_ratio)
88
- print("calling with prompt: %s" % prompt)
89
- rsp = ImageSynthesis.call(api_key=os.environ.get("DASH_API_KEY"),
90
- model="qwen-image",
91
- prompt=prompt,
92
- negative_prompt=negative_prompt,
93
- n=1,
94
- seed=seed,
95
- guidance_scale=guidance_scale,
96
- steps=num_inference_steps,
97
- size=f'{width}*{height}'
98
- ) # support 1664*928, 1472*1140, 1328*1328, 1140*1472, 928*1664
99
- print('response: %s' % rsp)
100
- if rsp.status_code == HTTPStatus.OK:
101
- # 在当前目录下保存图片
102
- for result in rsp.output.results:
103
- file_name = PurePosixPath(unquote(urlparse(result.url).path)).parts[-1]
104
- with open('./%s' % file_name, 'wb+') as f:
105
- f.write(requests.get(result.url).content)
106
- print(f'save image to {file_name}')
107
- else:
108
- print('sync_call Failed, status_code: %s, code: %s, message: %s' %
109
- (rsp.status_code, rsp.code, rsp.message))
110
- image = Image.open(file_name)
111
- return image, seed
112
 
113
- examples = [
114
- "A capybara wearing a suit holding a sign that reads Hello World",
115
- """A young girl wearing school uniform stands in a classroom, writing on a chalkboard. The text "Introducing Qwen-Image, a foundational image generation model that excels in complex text rendering and precise image editing" appears in neat white chalk at the center of the blackboard. Soft natural light filters through windows, casting gentle shadows. The scene is rendered in a realistic photography style with fine details, shallow depth of field, and warm tones. The girl's focused expression and chalk dust in the air add dynamism. Background elements include desks and educational posters, subtly blurred to emphasize the central action. Ultra-detailed 32K resolution, DSLR-quality, soft bokeh effect, documentary-style composition""",
116
- "Realistic still life photography style: A single, fresh apple resting on a clean, soft-textured surface. The apple is slightly off-center, softly backlit to highlight its natural gloss and subtle color gradients—deep crimson red blending into light golden hues. Fine details such as small blemishes, dew drops, and a few light highlights enhance its lifelike appearance. A shallow depth of field gently blurs the neutral background, drawing full attention to the apple. Hyper-detailed 8K resolution, studio lighting, photorealistic render, emphasizing texture and form."
117
- ]
118
 
119
  css = """
120
  #col-container {
@@ -124,7 +83,6 @@ css = """
124
  """
125
 
126
 
127
-
128
  with gr.Blocks(css=css) as demo:
129
  with gr.Column(elem_id="col-container"):
130
  # gr.Markdown('<div style="text-align: center;"><a href="https://huggingface.co/Qwen/Qwen-Image"><img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_logo.png" width="400"/></a></div>')
@@ -147,13 +105,7 @@ with gr.Blocks(css=css) as demo:
147
  label="Negative prompt",
148
  max_lines=1,
149
  placeholder="Enter a negative prompt",
150
- visible=False,
151
- )
152
- negative_prompt = gr.Text(
153
- label="Negative prompt",
154
- max_lines=1,
155
- placeholder="Enter a negative prompt",
156
- visible=False,
157
  )
158
 
159
  seed = gr.Slider(
@@ -168,7 +120,7 @@ with gr.Blocks(css=css) as demo:
168
 
169
  with gr.Row():
170
  aspect_ratio = gr.Radio(
171
- label="Aspect ratio(width:height)",
172
  choices=["1:1", "16:9", "9:16", "4:3", "3:4"],
173
  value="16:9",
174
  )
@@ -187,7 +139,7 @@ with gr.Blocks(css=css) as demo:
187
  minimum=1,
188
  maximum=50,
189
  step=1,
190
- value=50,
191
  )
192
 
193
  gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False, cache_mode="lazy")
 
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
+ import json
5
 
6
  from PIL import Image
7
 
 
21
  pipe.to('cuda')
22
 
23
  MAX_SEED = np.iinfo(np.int32).max
24
+ #MAX_IMAGE_SIZE = 1440
25
 
26
+ examples = json.loads(open("examples.json").read())
27
 
28
  # (1664, 928), (1472, 1140), (1328, 1328)
29
  def get_image_size(aspect_ratio):
30
  if aspect_ratio == "1:1":
31
+ return 1920, 1920
32
  elif aspect_ratio == "16:9":
33
+ return 1920, 1080
34
  elif aspect_ratio == "9:16":
35
+ return 1080, 1920
36
  elif aspect_ratio == "4:3":
37
+ return 1920, 1440
38
  elif aspect_ratio == "3:4":
39
+ return 1440, 1920
40
  else:
41
+ return 640, 640
42
 
43
  @spaces.GPU(duration=60)
44
  def infer_diffusers(
 
73
  return image, seed
74
 
75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
 
 
 
 
 
77
 
78
  css = """
79
  #col-container {
 
83
  """
84
 
85
 
 
86
  with gr.Blocks(css=css) as demo:
87
  with gr.Column(elem_id="col-container"):
88
  # gr.Markdown('<div style="text-align: center;"><a href="https://huggingface.co/Qwen/Qwen-Image"><img src="https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-Image/qwen_image_logo.png" width="400"/></a></div>')
 
105
  label="Negative prompt",
106
  max_lines=1,
107
  placeholder="Enter a negative prompt",
108
+ visible=True,
 
 
 
 
 
 
109
  )
110
 
111
  seed = gr.Slider(
 
120
 
121
  with gr.Row():
122
  aspect_ratio = gr.Radio(
123
+ label="Image size (ratio, max dim 1920)",
124
  choices=["1:1", "16:9", "9:16", "4:3", "3:4"],
125
  value="16:9",
126
  )
 
139
  minimum=1,
140
  maximum=50,
141
  step=1,
142
+ value=35,
143
  )
144
 
145
  gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False, cache_mode="lazy")