kenfoo commited on
Commit
45af972
·
verified ·
1 Parent(s): 5f214f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -99
app.py CHANGED
@@ -1,60 +1,77 @@
1
  import gradio as gr
2
- import numpy as np
3
  import random
4
 
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
- import torch
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
-
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
 
 
 
 
 
 
 
23
 
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
  def infer(
 
26
  prompt,
27
- negative_prompt,
28
  seed,
29
  randomize_seed,
30
- width,
31
- height,
32
  guidance_scale,
33
- num_inference_steps,
34
  progress=gr.Progress(track_tqdm=True),
35
  ):
 
 
 
 
 
 
 
 
 
36
  if randomize_seed:
37
  seed = random.randint(0, MAX_SEED)
38
 
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
-
 
 
 
 
53
 
54
  examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
  ]
59
 
60
  css = """
@@ -66,89 +83,80 @@ css = """
66
 
67
  with gr.Blocks(css=css) as demo:
68
  with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
 
 
 
 
 
 
 
 
 
71
  with gr.Row():
72
  prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
  )
79
 
80
- run_button = gr.Button("Run", scale=0, variant="primary")
 
 
 
 
 
81
 
82
- result = gr.Image(label="Result", show_label=False)
83
 
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
 
 
92
  seed = gr.Slider(
93
- label="Seed",
94
  minimum=0,
95
  maximum=MAX_SEED,
96
  step=1,
97
  value=0,
98
  )
99
 
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
- )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
  gr.on(
138
  triggers=[run_button.click, prompt.submit],
139
  fn=infer,
140
  inputs=[
 
141
  prompt,
142
- negative_prompt,
143
  seed,
144
  randomize_seed,
145
- width,
146
- height,
147
  guidance_scale,
148
- num_inference_steps,
149
  ],
150
  outputs=[result, seed],
151
  )
152
 
153
  if __name__ == "__main__":
154
- demo.launch()
 
1
  import gradio as gr
2
+ from gradio_client import Client
3
  import random
4
 
5
+ # API client for the external Space
6
+ space_client = Client("prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast")
7
+
8
+ LORA_STYLES = [
9
+ 'Multiple-Angles',
10
+ 'Photo-to-Anime',
11
+ 'Anime-V2',
12
+ 'Light-Migration',
13
+ 'Upscaler',
14
+ 'Style-Transfer',
15
+ 'Manga-Tone',
16
+ 'Anything2Real',
17
+ 'Fal-Multiple-Angles',
18
+ 'Polaroid-Photo',
19
+ 'Unblur-Anything',
20
+ 'Midnight-Noir-Eyes-Spotlight',
21
+ 'Hyper-Realistic-Portrait',
22
+ 'Ultra-Realistic-Portrait',
23
+ 'Pixar-Inspired-3D',
24
+ 'Noir-Comic-Book',
25
+ 'Any-light',
26
+ 'Studio-DeLight',
27
+ 'Cinematic-FlatLog',
28
+ ]
29
+ MAX_SEED = 2**32-1
30
 
 
31
  def infer(
32
+ image,
33
  prompt,
34
+ lora_adapter,
35
  seed,
36
  randomize_seed,
 
 
37
  guidance_scale,
38
+ steps,
39
  progress=gr.Progress(track_tqdm=True),
40
  ):
41
+ # Prepare images input as per API (expects Gallery [list of dicts])
42
+ images = []
43
+ if image is not None:
44
+ if isinstance(image, list): # Gradio Gallery
45
+ for im in image:
46
+ images.append({"image": {"path": im}, "caption": None})
47
+ else:
48
+ images.append({"image": {"path": image}, "caption": None})
49
+
50
  if randomize_seed:
51
  seed = random.randint(0, MAX_SEED)
52
 
53
+ try:
54
+ result = space_client.predict(
55
+ images=images,
56
+ prompt=prompt,
57
+ lora_adapter=lora_adapter,
58
+ seed=float(seed),
59
+ randomize_seed=bool(randomize_seed),
60
+ guidance_scale=float(guidance_scale),
61
+ steps=float(steps),
62
+ api_name="/infer",
63
+ )
64
+ # result is a tuple: (image_dict, seed)
65
+ image_info, seed_used = result
66
+ # The API may return image at .url or .path, we use .url if available
67
+ img_url = image_info.get("url") or image_info.get("path")
68
+ return img_url, seed_used
69
+ except Exception as e:
70
+ return None, seed
71
 
72
  examples = [
73
+ ["", "Astronaut in jungle, anime style", "Photo-to-Anime", 0, True, 1.0, 4],
74
+ ["", "A delicious ceviche cheesecake slice", "Style-Transfer", 0, True, 1.0, 4],
 
75
  ]
76
 
77
  css = """
 
83
 
84
  with gr.Blocks(css=css) as demo:
85
  with gr.Column(elem_id="col-container"):
86
+ gr.Markdown(" # 图像编辑 API Demo (基于 prithivMLmods/Qwen-Image-Edit-2511-LoRAs-Fast)")
87
 
88
+ with gr.Row():
89
+ image = gr.Image(
90
+ label="上传图片",
91
+ source="upload",
92
+ tool=None,
93
+ type="filepath",
94
+ elem_id="input-image"
95
+ )
96
  with gr.Row():
97
  prompt = gr.Text(
98
+ label="编辑描述(Prompt",
99
+ placeholder="请输入图片编辑描述...",
 
 
 
100
  )
101
 
102
+ with gr.Row():
103
+ lora_adapter = gr.Dropdown(
104
+ label="编辑风格(Style)",
105
+ choices=LORA_STYLES,
106
+ value="Photo-to-Anime"
107
+ )
108
 
109
+ run_button = gr.Button("执行编辑", scale=1, variant="primary")
110
 
111
+ result = gr.Image(label="结果图片", show_label=True)
 
 
 
 
 
 
112
 
113
+ with gr.Accordion("高级设置", open=False):
114
  seed = gr.Slider(
115
+ label="随机种子",
116
  minimum=0,
117
  maximum=MAX_SEED,
118
  step=1,
119
  value=0,
120
  )
121
 
122
+ randomize_seed = gr.Checkbox(label="随机种子", value=True)
123
+
124
+ guidance_scale = gr.Slider(
125
+ label="引导强度(Guidance Scale)",
126
+ minimum=0.1,
127
+ maximum=10.0,
128
+ step=0.1,
129
+ value=1.0,
130
+ )
131
+
132
+ steps = gr.Slider(
133
+ label="推理步数(Steps)",
134
+ minimum=1,
135
+ maximum=50,
136
+ step=1,
137
+ value=4,
138
+ )
139
+
140
+ gr.Examples(
141
+ examples=examples,
142
+ inputs=[image, prompt, lora_adapter, seed, randomize_seed, guidance_scale, steps],
143
+ label="示例",
144
+ )
145
+
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  gr.on(
147
  triggers=[run_button.click, prompt.submit],
148
  fn=infer,
149
  inputs=[
150
+ image,
151
  prompt,
152
+ lora_adapter,
153
  seed,
154
  randomize_seed,
 
 
155
  guidance_scale,
156
+ steps,
157
  ],
158
  outputs=[result, seed],
159
  )
160
 
161
  if __name__ == "__main__":
162
+ demo.launch()