wasdcutecat commited on
Commit
8d3f7b8
·
verified ·
1 Parent(s): 84357f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -54
app.py CHANGED
@@ -1,68 +1,31 @@
 
1
  import torch
2
- import cv2
3
  from PIL import Image
4
  import numpy as np
5
- import gradio as gr
6
-
7
- from diffusers import (
8
- StableDiffusionControlNetImg2ImgPipeline,
9
- ControlNetModel,
10
- UniPCMultistepScheduler,
11
- )
12
- from transformers import AutoTokenizer # bazı yeni modellerde gerekli olabilir
13
-
14
- device = "cuda" if torch.cuda.is_available() else "cpu"
15
- dtype = torch.float16 if device == "cuda" else torch.float32
16
 
17
- controlnet = ControlNetModel.from_pretrained(
18
- "lllyasviel/sd-controlnet-canny", torch_dtype=dtype
19
- )
20
 
21
- pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
22
  "runwayml/stable-diffusion-v1-5",
23
- controlnet=controlnet,
24
  torch_dtype=dtype,
25
- safety_checker=None # Hata almamak için bazı modellerde gerekir
26
  )
27
-
28
- pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
29
  pipe.to(device)
30
 
31
- def get_prompt_settings(magnitude):
32
- if 3 <= magnitude < 5:
33
- return ("building with slight cracks after magnitude 3 to 5 earthquake, minor damage, realistic",
34
- "blurry, bad quality, low resolution, destruction, ruins, collapse", 9.0, 0.85, 0.5)
35
- elif 5 <= magnitude < 6:
36
- return ("building with visible cracks and slight wall damage after magnitude 5 to 6 earthquake, realistic",
37
- "blurry, bad quality, low resolution, destroyed building", 10.0, 0.75, 0.6)
38
- elif 6 <= magnitude < 7:
39
- return ("building with large cracks, broken windows, partial wall destruction after magnitude 6 to 7 earthquake, realistic",
40
- "blurry, bad quality, low resolution", 11.0, 0.6, 0.7)
41
- elif 7 <= magnitude < 8:
42
- return ("building heavily damaged, collapsed sections, broken walls, rubble, after magnitude 7 to 8 earthquake, realistic",
43
- "blurry, bad quality, low resolution, intact building", 12.0, 0.27, 0.8)
44
- elif 8 <= magnitude <= 9:
45
- return ("building completely destroyed, full collapse, ruins, debris, after magnitude above 8 earthquake, realistic",
46
- "blurry, bad quality, low resolution, intact building", 13.0, 0.12, 0.9)
47
- else:
48
- raise ValueError("Magnitude must be between 3 and 9.")
49
-
50
  def process(image, magnitude):
51
- image = image.resize((512, 512))
52
- edges = cv2.Canny(np.array(image), 100, 200)
53
- control_image = Image.fromarray(edges)
54
-
55
- prompt, neg_prompt, guidance, cond_scale, strength = get_prompt_settings(magnitude)
56
-
57
  result = pipe(
58
  prompt=prompt,
59
  negative_prompt=neg_prompt,
60
  image=image,
61
- control_image=control_image,
62
- strength=strength,
63
- guidance_scale=guidance,
64
- controlnet_conditioning_scale=cond_scale,
65
- num_inference_steps=40
66
  ).images[0]
67
 
68
  return result
@@ -70,12 +33,12 @@ def process(image, magnitude):
70
  demo = gr.Interface(
71
  fn=process,
72
  inputs=[
73
- gr.Image(type="pil", label="Bina Fotoğrafı Yükle"),
74
  gr.Slider(3, 9, step=0.1, label="Deprem Şiddeti")
75
  ],
76
- outputs=gr.Image(type="pil", label="Depremli Görsel"),
77
- title="Earthquake Damage Visualizer",
78
- description="Yüklediğiniz bina görselini deprem şiddetine göre hasarlı hale getirir."
79
  )
80
 
81
  if __name__ == "__main__":
 
1
+ from diffusers import StableDiffusionImg2ImgPipeline
2
  import torch
3
+ import gradio as gr
4
  from PIL import Image
5
  import numpy as np
 
 
 
 
 
 
 
 
 
 
 
6
 
7
+ device = "cpu"
8
+ dtype = torch.float32
 
9
 
10
+ pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
11
  "runwayml/stable-diffusion-v1-5",
 
12
  torch_dtype=dtype,
13
+ safety_checker=None
14
  )
 
 
15
  pipe.to(device)
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def process(image, magnitude):
18
+ prompt = f"building slightly damaged after magnitude {magnitude} earthquake, realistic"
19
+ neg_prompt = "blurry, low quality, bad composition"
20
+
21
+ image = image.resize((384, 384)) # küçük boyut daha hızlı
 
 
22
  result = pipe(
23
  prompt=prompt,
24
  negative_prompt=neg_prompt,
25
  image=image,
26
+ strength=0.5,
27
+ guidance_scale=7.5,
28
+ num_inference_steps=20 # az adım
 
 
29
  ).images[0]
30
 
31
  return result
 
33
  demo = gr.Interface(
34
  fn=process,
35
  inputs=[
36
+ gr.Image(type="pil", label="Bina Görseli"),
37
  gr.Slider(3, 9, step=0.1, label="Deprem Şiddeti")
38
  ],
39
+ outputs=gr.Image(type="pil"),
40
+ title="Earthquake Visualizer (CPU Mode)",
41
+ description="Deprem hasarını CPU üzerinde hızlı simüle eder."
42
  )
43
 
44
  if __name__ == "__main__":