NMachine commited on
Commit
09e2af9
·
verified ·
1 Parent(s): 98f9b9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -18
app.py CHANGED
@@ -1,28 +1,31 @@
1
  import gradio as gr
 
2
  import torch
3
  from PIL import Image
4
- from transformers import AutoProcessor, AutoModelForCausalLM
5
 
6
- model_id = "Phr00t/Qwen-Image-Edit-Rapid-AIO"
7
 
8
- processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
9
- model = AutoModelForCausalLM.from_pretrained(
10
- model_id,
11
- torch_dtype=torch.bfloat16,
12
- device_map="auto",
13
  trust_remote_code=True
14
  )
15
 
16
- def generate(prompt, input_img):
17
- if input_img is not None:
18
- img = Image.fromarray(input_img)
19
- inputs = processor(prompt, image=img, return_tensors="pt").to(model.device)
 
 
 
 
20
  else:
21
- inputs = processor(prompt, return_tensors="pt").to(model.device)
 
 
 
22
 
23
- output = model.generate(**inputs)
24
- image = processor.batch_decode(output, output_type="pil")[0]
25
- return image
26
 
27
  demo = gr.Interface(
28
  fn=generate,
@@ -30,9 +33,9 @@ demo = gr.Interface(
30
  gr.Textbox(label="Prompt"),
31
  gr.Image(label="Input Image (optional)")
32
  ],
33
- outputs=gr.Image(label="Generated Image"),
34
- title="Qwen Image Edit Rapid AIO",
35
- description="Uses Qwen2-VL model for text-to-image and image editing."
36
  )
37
 
38
  demo.launch()
 
1
  import gradio as gr
2
+ from diffusers import DiffusionPipeline
3
  import torch
4
  from PIL import Image
 
5
 
6
+ MODEL_ID = "Phr00t/Qwen-Image-Edit-Rapid-AIO"
7
 
8
+ pipe = DiffusionPipeline.from_pretrained(
9
+ MODEL_ID,
10
+ torch_dtype=torch.float32,
11
+ safety_checker=None,
 
12
  trust_remote_code=True
13
  )
14
 
15
+ pipe.to("cpu")
16
+
17
+
18
+ def generate(prompt, image):
19
+ if image is None:
20
+ # text → image
21
+ result = pipe(prompt)
22
+ return result.images[0]
23
  else:
24
+ # image editing mode
25
+ img = Image.fromarray(image)
26
+ result = pipe(prompt=prompt, image=img)
27
+ return result.images[0]
28
 
 
 
 
29
 
30
  demo = gr.Interface(
31
  fn=generate,
 
33
  gr.Textbox(label="Prompt"),
34
  gr.Image(label="Input Image (optional)")
35
  ],
36
+ outputs=gr.Image(label="Generated / Edited Image"),
37
+ title="Qwen Image Edit Rapid AIO (CPU Friendly)",
38
+ description="Works on CPU with Diffusers. Supports text-to-image AND image editing."
39
  )
40
 
41
  demo.launch()