dgoot commited on
Commit
2c20db4
Β·
verified Β·
1 Parent(s): 3538d3e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -19
app.py CHANGED
@@ -1,37 +1,50 @@
1
- from pathlib import Path
2
- import torch
3
- import spaces
4
- import shutil
5
  import gradio as gr
6
- from loguru import logger
7
  import requests
8
- from tqdm import tqdm
9
- from diffusers import AutoPipelineForImage2Image
 
 
 
 
10
  from PIL import Image
 
11
 
12
- model_path = Path("pony-diffusion-v6-xl.safetensors")
13
 
14
- if not model_path.exists():
15
- logger.info(f"Downloading model: {model_path}")
 
 
16
 
17
- r = requests.get(
18
- "https://civitai.com/api/download/models/290640?type=Model&format=SafeTensor&size=pruned&fp=fp16",
19
- stream=True,
20
- )
21
  r.raise_for_status()
22
 
23
- temp_path = f"/tmp/{model_path.name}"
24
  with tqdm(
25
- total=int(r.headers["content-length"]), unit="B", unit_scale=True
26
  ) as pbar, open(temp_path, "wb") as f:
27
  for chunk in r.iter_content(chunk_size=1024 * 1024):
28
  f.write(chunk)
29
  pbar.update(len(chunk))
30
 
31
- shutil.move(temp_path, model_path)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- pipe = AutoPipelineForImage2Image.from_pretrained(
34
- model_path, torch_dtype=torch.float16, use_safetensors=True, variant="fp16"
35
  )
36
  pipe = pipe.to("cuda")
37
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import requests
3
+ import shutil
4
+ import spaces
5
+ import torch
6
+ from diffusers import AutoencoderKL, StableDiffusionXLImg2ImgPipeline
7
+ from loguru import logger
8
+ from pathlib import Path
9
  from PIL import Image
10
+ from tqdm import tqdm
11
 
 
12
 
13
+ def download(file: str, url: str):
14
+ file_path = Path(file)
15
+ if file_path.exists():
16
+ return
17
 
18
+ r = requests.get(url, stream=True)
 
 
 
19
  r.raise_for_status()
20
 
21
+ temp_path = f"/tmp/{file_path.name}"
22
  with tqdm(
23
+ desc=file, total=int(r.headers["content-length"]), unit="B", unit_scale=True
24
  ) as pbar, open(temp_path, "wb") as f:
25
  for chunk in r.iter_content(chunk_size=1024 * 1024):
26
  f.write(chunk)
27
  pbar.update(len(chunk))
28
 
29
+ shutil.move(temp_path, file_path)
30
+
31
+
32
+ model_path = "pony-diffusion-v6-xl.safetensors"
33
+ download(
34
+ model_path,
35
+ "https://civitai.com/api/download/models/290640?type=Model&format=SafeTensor&size=pruned&fp=fp16",
36
+ )
37
+
38
+ vae_path = "pony-diffusion-v6-xl.vae.safetensors"
39
+ download(
40
+ vae_path,
41
+ "https://civitai.com/api/download/models/290640?type=VAE&format=SafeTensor",
42
+ )
43
+
44
+ vae = AutoencoderKL.from_single_file(vae_path)
45
 
46
+ pipe = StableDiffusionXLImg2ImgPipeline.from_single_file(
47
+ model_path, torch_dtype=torch.float16, use_safetensors=True, variant="fp16", vae=vae
48
  )
49
  pipe = pipe.to("cuda")
50