smartdigitalnetworks commited on
Commit
40ab0a4
·
verified ·
1 Parent(s): 0ac9d51

Upload 2 files

Browse files
Files changed (2) hide show
  1. appAnything.py +82 -0
  2. appOmniGenPipeline.py +83 -0
appAnything.py ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ #from torch import autocast // only for GPU
4
+
5
+ from PIL import Image
6
+ import numpy as np
7
+ from io import BytesIO
8
+ import os
9
+ MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
10
+
11
+ #from diffusers import StableDiffusionPipeline
12
+ from diffusers import StableDiffusionImg2ImgPipeline
13
+
14
+ print("hello")
15
+
16
+ YOUR_TOKEN=MY_SECRET_TOKEN
17
+
18
+ device="cpu"
19
+
20
+ #prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
21
+ #prompt_pipe.to(device)
22
+
23
+ img_pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
24
+ "Linaqruf/anything-v3-1",
25
+ use_auth_token=YOUR_TOKEN,
26
+ safety_checker=None, # ← disable safety checker
27
+ )
28
+ img_pipe.to(device)
29
+
30
+ source_img = gr.Image(sources=["upload", "webcam", "clipboard"], type="filepath", label="Add Image for Text Guided Editing and Sketch")
31
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto")
32
+
33
+ def resize(value,img):
34
+ #baseheight = value
35
+ img = Image.open(img)
36
+ #hpercent = (baseheight/float(img.size[1]))
37
+ #wsize = int((float(img.size[0])*float(hpercent)))
38
+ #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
39
+ img = img.resize((value,value), Image.Resampling.LANCZOS)
40
+ return img
41
+
42
+ def infer(source_img, prompt, guide, steps, seed, strength):
43
+ generator = torch.Generator("cpu").manual_seed(seed)
44
+
45
+ source_image = Image.open(source_img).convert("RGB")
46
+ source_image = source_image.resize((512, 512), Image.Resampling.LANCZOS)
47
+
48
+ result = img_pipe(
49
+ [prompt],
50
+ image=source_image,
51
+ strength=strength,
52
+ guidance_scale=guide,
53
+ num_inference_steps=steps,
54
+ generator=generator
55
+ )
56
+
57
+ output_images = result["images"]
58
+ output_paths = []
59
+
60
+ for idx, img in enumerate(output_images):
61
+ filename = f"output_{seed}_{idx}.png"
62
+ save_path = os.path.join("outputs", filename)
63
+ os.makedirs("outputs", exist_ok=True)
64
+ img.save(save_path)
65
+ print(f"Saved image to: {save_path}")
66
+ output_paths.append(save_path)
67
+
68
+ # Optional: return paths or Gradio can render them too
69
+ return output_images
70
+
71
+ print("Great ! Everything is working fine !")
72
+
73
+ title="Text Guided Image Editing"
74
+ description="<p style='text-align: center;'>Text Guided Image Editing via Stable Diffusion Image to Image using CPU and HF token. <br />Warning: CPU processing is slow... 6/7 min inference time.</p>"
75
+
76
+ gr.Interface(fn=infer, inputs=[source_img,
77
+ "text",
78
+ gr.Slider(2, 15, value = 6.7, label = 'Guidence Scale'),
79
+ gr.Slider(10, 50, value = 13, step = 1, label = 'Number of Iterations'),
80
+ gr.Slider(label = "Seed", minimum = 0, maximum = 676767, step = 67, randomize = False),
81
+ gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .67)],
82
+ outputs=gallery,title=title,description=description, allow_flagging="never", flagging_dir="flagged").queue(max_size=67).launch(enable_queue=True)
appOmniGenPipeline.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ #from torch import autocast // only for GPU
4
+
5
+ from PIL import Image
6
+ import numpy as np
7
+ from io import BytesIO
8
+ import os
9
+ MY_SECRET_TOKEN=os.environ.get('HF_TOKEN_SD')
10
+
11
+ #from diffusers import StableDiffusionPipeline
12
+ from diffusers import StableDiffusionImg2ImgPipeline
13
+
14
+ print("hello")
15
+
16
+ YOUR_TOKEN=MY_SECRET_TOKEN
17
+
18
+ device="cpu"
19
+
20
+ #prompt_pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", use_auth_token=YOUR_TOKEN)
21
+ #prompt_pipe.to(device)
22
+
23
+ img_pipe = OmniGenPipeline.from_pretrained(
24
+ "Shitao/OmniGen-v1-diffusers",
25
+ torch_dtype=torch.bfloat16,
26
+ use_auth_token=YOUR_TOKEN,
27
+ safety_checker=None, # ← disable safety checker
28
+ )
29
+ img_pipe.to(device)
30
+
31
+ source_img = gr.Image(sources=["upload", "webcam", "clipboard"], type="filepath", label="Add Image for Text Guided Editing and Sketch")
32
+ gallery = gr.Gallery(label="Generated images", show_label=False, elem_id="gallery").style(grid=[1], height="auto")
33
+
34
+ def resize(value,img):
35
+ #baseheight = value
36
+ img = Image.open(img)
37
+ #hpercent = (baseheight/float(img.size[1]))
38
+ #wsize = int((float(img.size[0])*float(hpercent)))
39
+ #img = img.resize((wsize,baseheight), Image.Resampling.LANCZOS)
40
+ img = img.resize((value,value), Image.Resampling.LANCZOS)
41
+ return img
42
+
43
+ def infer(source_img, prompt, guide, steps, seed, strength):
44
+ generator = torch.Generator("cpu").manual_seed(seed)
45
+
46
+ source_image = Image.open(source_img).convert("RGB")
47
+ source_image = source_image.resize((512, 512), Image.Resampling.LANCZOS)
48
+
49
+ result = img_pipe(
50
+ [prompt],
51
+ image=source_image,
52
+ strength=strength,
53
+ guidance_scale=guide,
54
+ num_inference_steps=steps,
55
+ generator=generator
56
+ )
57
+
58
+ output_images = result["images"]
59
+ output_paths = []
60
+
61
+ for idx, img in enumerate(output_images):
62
+ filename = f"output_{seed}_{idx}.png"
63
+ save_path = os.path.join("outputs", filename)
64
+ os.makedirs("outputs", exist_ok=True)
65
+ img.save(save_path)
66
+ print(f"Saved image to: {save_path}")
67
+ output_paths.append(save_path)
68
+
69
+ # Optional: return paths or Gradio can render them too
70
+ return output_images
71
+
72
+ print("Great ! Everything is working fine !")
73
+
74
+ title="Text Guided Image Editing"
75
+ description="<p style='text-align: center;'>Text Guided Image Editing via Stable Diffusion Image to Image using CPU and HF token. <br />Warning: CPU processing is slow... 6/7 min inference time.</p>"
76
+
77
+ gr.Interface(fn=infer, inputs=[source_img,
78
+ "text",
79
+ gr.Slider(2, 15, value = 6.7, label = 'Guidence Scale'),
80
+ gr.Slider(10, 50, value = 13, step = 1, label = 'Number of Iterations'),
81
+ gr.Slider(label = "Seed", minimum = 0, maximum = 676767, step = 67, randomize = False),
82
+ gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .05, value = .67)],
83
+ outputs=gallery,title=title,description=description, allow_flagging="never", flagging_dir="flagged").queue(max_size=67).launch(enable_queue=True)