panelforge commited on
Commit
8bb4602
·
verified ·
1 Parent(s): 0f8e37d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +85 -67
app.py CHANGED
@@ -1,21 +1,12 @@
1
  import gradio as gr
2
  import numpy as np
3
  import random
4
- import spaces
5
  from diffusers import DiffusionPipeline, DPMSolverSDEScheduler
6
  import torch
7
- from huggingface_hub import hf_hub_download
8
- from ultralytics import YOLO
9
- from PIL import Image
10
- import cv2
11
 
12
  device = "cuda" if torch.cuda.is_available() else "cpu"
13
- model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl"
14
- adetailer_model_id = "Bingsu/adetailer" # Your ADetailer model
15
-
16
- # Load the YOLO model for face detection
17
- yolo_model_path = hf_hub_download(adetailer_model_id, "face_yolov8n.pt")
18
- yolo_model = YOLO(yolo_model_path)
19
 
20
  if torch.cuda.is_available():
21
  torch_dtype = torch.float16
@@ -29,55 +20,25 @@ pipe = pipe.to(device)
29
  MAX_SEED = np.iinfo(np.int32).max
30
  MAX_IMAGE_SIZE = 1024
31
 
32
- def correct_anime_face(image):
33
- # Convert to OpenCV format
34
- img = np.array(image)
35
- img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
36
-
37
- # Detect faces
38
- results = yolo_model(img)
39
-
40
- for detection in results[0].boxes:
41
- x1, y1, x2, y2 = map(int, detection.xyxy[0].tolist())
42
-
43
- # Crop the face region
44
- face = img[y1:y2, x1:x2]
45
- face_pil = Image.fromarray(cv2.cvtColor(face, cv2.COLOR_BGR2RGB))
46
-
47
- # Prompt for the correction model
48
- prompt = "Enhance this anime character's face, fix eyes and make features more vivid."
49
-
50
- # Process the face with the anime correction model
51
- corrected_face = pipe(prompt=prompt, image=face_pil).images[0] # Replace with your correction model
52
-
53
- # Place the corrected face back into the original image
54
- img[y1:y2, x1:x2] = np.array(corrected_face)
55
-
56
- # Convert back to PIL
57
- final_image = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
58
- return final_image
59
-
60
- @spaces.GPU
61
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
 
62
  if randomize_seed:
63
  seed = random.randint(0, MAX_SEED)
64
 
65
  generator = torch.Generator().manual_seed(seed)
66
 
67
  image = pipe(
68
- prompt=prompt,
69
- negative_prompt=negative_prompt,
70
- guidance_scale=guidance_scale,
71
- num_inference_steps=num_inference_steps,
72
- width=width,
73
- height=height,
74
- generator=generator
75
  ).images[0]
76
 
77
- # Correct anime face in the generated image
78
- corrected_image = correct_anime_face(image)
79
-
80
- return corrected_image, seed
81
 
82
  examples = [
83
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
@@ -85,7 +46,7 @@ examples = [
85
  "A delicious ceviche cheesecake slice",
86
  ]
87
 
88
- css = """
89
  #col-container {
90
  margin: 0 auto;
91
  max-width: 640px;
@@ -93,33 +54,90 @@ css = """
93
  """
94
 
95
  with gr.Blocks(css=css) as demo:
 
96
  with gr.Column(elem_id="col-container"):
97
- gr.Markdown("# Text-to-Image Gradio Template")
 
 
98
 
99
  with gr.Row():
100
- prompt = gr.Text(label="Prompt", show_label=False, max_lines=1, placeholder="Enter your prompt", container=False)
 
 
 
 
 
 
 
 
101
  run_button = gr.Button("Run", scale=0)
102
 
103
  result = gr.Image(label="Result", show_label=False)
104
 
105
  with gr.Accordion("Advanced Settings", open=False):
106
- negative_prompt = gr.Text(label="Negative prompt", max_lines=1, placeholder="Enter a negative prompt", visible=False)
107
- seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
109
 
110
  with gr.Row():
111
- width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
112
- height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  with gr.Row():
115
- guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=0.0)
116
- num_inference_steps = gr.Slider(label="Number of inference steps", minimum=1, maximum=50, step=1, value=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
117
 
118
- gr.Examples(examples=examples, inputs=[prompt])
119
-
120
- gr.on(triggers=[run_button.click, prompt.submit],
121
- fn=infer,
122
- inputs=[prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
123
- outputs=[result, seed])
 
 
 
 
124
 
125
- demo.queue().launch()
 
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, DPMSolverSDEScheduler
6
  import torch
 
 
 
 
7
 
8
  device = "cuda" if torch.cuda.is_available() else "cpu"
9
+ model_repo_id = "John6666/wai-ani-nsfw-ponyxl-v8-sdxl" #Replace to the model you would like to use
 
 
 
 
 
10
 
11
  if torch.cuda.is_available():
12
  torch_dtype = torch.float16
 
20
  MAX_SEED = np.iinfo(np.int32).max
21
  MAX_IMAGE_SIZE = 1024
22
 
23
+ @spaces.GPU #[uncomment to use ZeroGPU]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  def infer(prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
25
+
26
  if randomize_seed:
27
  seed = random.randint(0, MAX_SEED)
28
 
29
  generator = torch.Generator().manual_seed(seed)
30
 
31
  image = pipe(
32
+ prompt = prompt,
33
+ negative_prompt = negative_prompt,
34
+ guidance_scale = guidance_scale,
35
+ num_inference_steps = num_inference_steps,
36
+ width = width,
37
+ height = height,
38
+ generator = generator
39
  ).images[0]
40
 
41
+ return image, seed
 
 
 
42
 
43
  examples = [
44
  "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
 
46
  "A delicious ceviche cheesecake slice",
47
  ]
48
 
49
+ css="""
50
  #col-container {
51
  margin: 0 auto;
52
  max-width: 640px;
 
54
  """
55
 
56
  with gr.Blocks(css=css) as demo:
57
+
58
  with gr.Column(elem_id="col-container"):
59
+ gr.Markdown(f"""
60
+ # Text-to-Image Gradio Template
61
+ """)
62
 
63
  with gr.Row():
64
+
65
+ prompt = gr.Text(
66
+ label="Prompt",
67
+ show_label=False,
68
+ max_lines=1,
69
+ placeholder="Enter your prompt",
70
+ container=False,
71
+ )
72
+
73
  run_button = gr.Button("Run", scale=0)
74
 
75
  result = gr.Image(label="Result", show_label=False)
76
 
77
  with gr.Accordion("Advanced Settings", open=False):
78
+
79
+ negative_prompt = gr.Text(
80
+ label="Negative prompt",
81
+ max_lines=1,
82
+ placeholder="Enter a negative prompt",
83
+ visible=False,
84
+ )
85
+
86
+ seed = gr.Slider(
87
+ label="Seed",
88
+ minimum=0,
89
+ maximum=MAX_SEED,
90
+ step=1,
91
+ value=0,
92
+ )
93
+
94
  randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
95
 
96
  with gr.Row():
 
 
97
 
98
+ width = gr.Slider(
99
+ label="Width",
100
+ minimum=256,
101
+ maximum=MAX_IMAGE_SIZE,
102
+ step=32,
103
+ value=1024, #Replace with defaults that work for your model
104
+ )
105
+
106
+ height = gr.Slider(
107
+ label="Height",
108
+ minimum=256,
109
+ maximum=MAX_IMAGE_SIZE,
110
+ step=32,
111
+ value=1024, #Replace with defaults that work for your model
112
+ )
113
+
114
  with gr.Row():
115
+
116
+ guidance_scale = gr.Slider(
117
+ label="Guidance scale",
118
+ minimum=0.0,
119
+ maximum=10.0,
120
+ step=0.1,
121
+ value=0.0, #Replace with defaults that work for your model
122
+ )
123
+
124
+ num_inference_steps = gr.Slider(
125
+ label="Number of inference steps",
126
+ minimum=1,
127
+ maximum=50,
128
+ step=1,
129
+ value=2, #Replace with defaults that work for your model
130
+ )
131
 
132
+ gr.Examples(
133
+ examples = examples,
134
+ inputs = [prompt]
135
+ )
136
+ gr.on(
137
+ triggers=[run_button.click, prompt.submit],
138
+ fn = infer,
139
+ inputs = [prompt, negative_prompt, seed, randomize_seed, width, height, guidance_scale, num_inference_steps],
140
+ outputs = [result, seed]
141
+ )
142
 
143
+ demo.queue().launch()