quocnhut134 commited on
Commit
ec1a500
·
1 Parent(s): 7d140d1

update code

Browse files
Files changed (6) hide show
  1. Dockerfile +0 -1
  2. src/__init__.py +0 -0
  3. src/config.py +0 -9
  4. src/inference.py +0 -34
  5. src/loader.py +0 -29
  6. streamlit_app.py +69 -5
Dockerfile CHANGED
@@ -9,7 +9,6 @@ RUN apt-get update && apt-get install -y \
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  COPY requirements.txt ./
12
- COPY src/ ./src/
13
  COPY streamlit_app.py ./
14
 
15
  RUN pip3 install -r requirements.txt
 
9
  && rm -rf /var/lib/apt/lists/*
10
 
11
  COPY requirements.txt ./
 
12
  COPY streamlit_app.py ./
13
 
14
  RUN pip3 install -r requirements.txt
src/__init__.py DELETED
File without changes
src/config.py DELETED
@@ -1,9 +0,0 @@
1
- import os
2
- import torch
3
- from huggingface_hub import hf_hub_download
4
-
5
- app_controlnet_path = "SaitoHoujou/Finetuned-ControlNet_for_Diffusion-Model"
6
- app_base_model = "botp/stable-diffusion-v1-5"
7
- app_hed_model = 'lllyasviel/Annotators'
8
- app_device = "cuda" if torch.cuda.is_available() else "cpu"
9
- app_dtype = torch.float16 if app_device == "cuda" else torch.float32
 
 
 
 
 
 
 
 
 
 
src/inference.py DELETED
@@ -1,34 +0,0 @@
1
- import torch
2
- from PIL import Image
3
-
4
- def generate_image(
5
- pipe,
6
- hed,
7
- input_image: Image.Image,
8
- prompt: str,
9
- neg_prompt: str,
10
- guidance_scale: float,
11
- control_scale: float,
12
- device: str,
13
- seed: int = 42
14
- ) -> (Image.Image, Image.Image):
15
-
16
- condition_image = hed(
17
- input_image,
18
- detect_resolution=512,
19
- image_resolution=512
20
- )
21
-
22
- generator = torch.Generator(device=device).manual_seed(seed)
23
-
24
- output_image = pipe(
25
- prompt=prompt,
26
- negative_prompt=neg_prompt,
27
- image=condition_image,
28
- num_inference_steps=30,
29
- generator=generator,
30
- guidance_scale=guidance_scale,
31
- controlnet_conditioning_scale=control_scale
32
- ).images[0]
33
-
34
- return output_image, condition_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/loader.py DELETED
@@ -1,29 +0,0 @@
1
- import streamlit as st
2
- import torch
3
- from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
4
- from controlnet_aux import HEDdetector
5
- import config
6
-
7
- @st.cache_resource
8
- def load_hed_detector():
9
- hed = HEDdetector.from_pretrained(config.app_hed_model)
10
- hed = hed.to(config.app_device)
11
- return hed
12
-
13
- @st.cache_resource
14
- def load_pipeline():
15
- controlnet = ControlNetModel.from_pretrained(
16
- config.app_controlnet_path,
17
- torch_dtype=config.app_dtype
18
- )
19
-
20
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
21
- config.app_base_model,
22
- controlnet=controlnet,
23
- torch_dtype=config.app_dtype,
24
- safety_checker=None
25
- )
26
-
27
- pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
28
- pipe = pipe.to(config.app_device)
29
- return pipe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
streamlit_app.py CHANGED
@@ -1,6 +1,70 @@
1
  import streamlit as st
2
  from PIL import Image
3
- from src import config, loader, inference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  st.set_page_config(layout="wide")
6
  st.title("Demo Generating Image from Face Sketch ")
@@ -8,8 +72,8 @@ st.title("Demo Generating Image from Face Sketch ")
8
  st.markdown("---")
9
 
10
  with st.spinner("Loading models..."):
11
- pipe = loader.load_pipeline()
12
- hed = loader.load_hed_detector()
13
 
14
  with st.sidebar:
15
  st.header("Configuration")
@@ -48,7 +112,7 @@ with col2:
48
  input_image_placeholder.image(input_image, caption="Uploaded sketch", width='stretch')
49
 
50
  with st.spinner("Generating image..."):
51
- output_image, condition_image = inference.generate_image(
52
  pipe=pipe,
53
  hed=hed,
54
  input_image=input_image,
@@ -56,7 +120,7 @@ with col2:
56
  neg_prompt=negative_prompt,
57
  guidance_scale=guidance_scale,
58
  control_scale=control_scale,
59
- device=config.app_device
60
  )
61
 
62
  hed_image_placeholder.image(condition_image, caption="HED Image", width='stretch')
 
1
  import streamlit as st
2
  from PIL import Image
3
+ import torch
4
+ from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, UniPCMultistepScheduler
5
+ from controlnet_aux import HEDdetector
6
+
7
+ app_controlnet_path = "SaitoHoujou/Finetuned-ControlNet_for_Diffusion-Model"
8
+ app_base_model = "botp/stable-diffusion-v1-5"
9
+ app_hed_model = 'lllyasviel/Annotators'
10
+ app_device = "cuda" if torch.cuda.is_available() else "cpu"
11
+ app_dtype = torch.float16 if app_device == "cuda" else torch.float32
12
+
13
+ def generate_image(
14
+ pipe,
15
+ hed,
16
+ input_image: Image.Image,
17
+ prompt: str,
18
+ neg_prompt: str,
19
+ guidance_scale: float,
20
+ control_scale: float,
21
+ device: str,
22
+ seed: int = 42
23
+ ) -> (Image.Image, Image.Image):
24
+
25
+ condition_image = hed(
26
+ input_image,
27
+ detect_resolution=512,
28
+ image_resolution=512
29
+ )
30
+
31
+ generator = torch.Generator(device=device).manual_seed(seed)
32
+
33
+ output_image = pipe(
34
+ prompt=prompt,
35
+ negative_prompt=neg_prompt,
36
+ image=condition_image,
37
+ num_inference_steps=30,
38
+ generator=generator,
39
+ guidance_scale=guidance_scale,
40
+ controlnet_conditioning_scale=control_scale
41
+ ).images[0]
42
+
43
+ return output_image, condition_image
44
+
45
+ @st.cache_resource
46
+ def load_hed_detector():
47
+ hed = HEDdetector.from_pretrained(app_hed_model)
48
+ hed = hed.to(app_device)
49
+ return hed
50
+
51
+ @st.cache_resource
52
+ def load_pipeline():
53
+ controlnet = ControlNetModel.from_pretrained(
54
+ app_controlnet_path,
55
+ torch_dtype=app_dtype
56
+ )
57
+
58
+ pipe = StableDiffusionControlNetPipeline.from_pretrained(
59
+ app_base_model,
60
+ controlnet=controlnet,
61
+ torch_dtype=app_dtype,
62
+ safety_checker=None
63
+ )
64
+
65
+ pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
66
+ pipe = pipe.to(app_device)
67
+ return pipe
68
 
69
  st.set_page_config(layout="wide")
70
  st.title("Demo Generating Image from Face Sketch ")
 
72
  st.markdown("---")
73
 
74
  with st.spinner("Loading models..."):
75
+ pipe = load_pipeline()
76
+ hed = load_hed_detector()
77
 
78
  with st.sidebar:
79
  st.header("Configuration")
 
112
  input_image_placeholder.image(input_image, caption="Uploaded sketch", width='stretch')
113
 
114
  with st.spinner("Generating image..."):
115
+ output_image, condition_image = generate_image(
116
  pipe=pipe,
117
  hed=hed,
118
  input_image=input_image,
 
120
  neg_prompt=negative_prompt,
121
  guidance_scale=guidance_scale,
122
  control_scale=control_scale,
123
+ device=app_device
124
  )
125
 
126
  hed_image_placeholder.image(condition_image, caption="HED Image", width='stretch')