quocnhut134 commited on
Commit
8231383
·
1 Parent(s): b04d072

Initialize deployment

Browse files
Dockerfile CHANGED
@@ -17,4 +17,4 @@ EXPOSE 8501
17
 
18
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
19
 
20
- ENTRYPOINT ["streamlit", "run", "src/streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
 
17
 
18
  HEALTHCHECK CMD curl --fail http://localhost:8501/_stcore/health
19
 
20
+ ENTRYPOINT ["streamlit", "run", "streamlit_app.py", "--server.port=8501", "--server.address=0.0.0.0"]
requirements.txt CHANGED
@@ -1,3 +1,16 @@
1
- altair
2
- pandas
3
- streamlit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ opencv-python-headless
2
+ numpy
3
+ tqdm
4
+ torch
5
+ torchvision
6
+ diffusers
7
+ transformers
8
+ accelerate
9
+ controlnet-aux
10
+ datasets
11
+ torch-fidelity
12
+ lpips
13
+ pillow
14
+ hf_xet
15
+ streamlit
16
+ huggingface-hub
src/__init__.py ADDED
File without changes
src/config.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
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 device == "cuda" else torch.float32
src/inference.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
src/streamlit_app.py DELETED
@@ -1,40 +0,0 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
- import streamlit as st
5
-
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
streamlit_app.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ")
7
+
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")
16
+ prompt = st.text_area(
17
+ "Positive Prompt",
18
+ "(hyper-realistic photo:1.2), (ultra-detailed skin texture:1.1), detailed pores, realistic eyes, (Caucasian man:1.1), sharp focus, 8k UHD, professional studio lighting",
19
+ height=100
20
+ )
21
+ negative_prompt = st.text_area(
22
+ "Negative Prompt",
23
+ "(drawing:1.4), (sketch:1.4), (painting:1.3), cartoon, 3D, render, CGI, anime, illustration, (deformed:1.2), (disfigured:1.2), ugly, bad anatomy, (blurry:1.1)",
24
+ height=100
25
+ )
26
+ guidance_scale = st.slider("Guidance Scale", 1.0, 15.0, 8.0, 0.5)
27
+ control_scale = st.slider("ControlNet Scale", 0.0, 1.0, 0.9, 0.1)
28
+ uploaded_file = st.file_uploader("Upload your face sketch here...", type=["png", "jpg", "jpeg"])
29
+ run_button = st.button("Generate Image", width='stretch', type="primary")
30
+
31
+ col1, col2 = st.columns(2)
32
+
33
+ with col1:
34
+ st.header("1. Uploaded Face Sketch")
35
+ st.markdown("Input Face Sketch")
36
+ input_image_placeholder = st.empty()
37
+ st.markdown("HED Image")
38
+ hed_image_placeholder = st.empty()
39
+
40
+ with col2:
41
+ st.header("2. Generated Image")
42
+
43
+ st.markdown("Output Generated Image")
44
+ output_image_placeholder = st.empty()
45
+
46
+ if run_button and uploaded_file:
47
+ input_image = Image.open(uploaded_file).convert("RGB")
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,
55
+ prompt=prompt,
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')
63
+ output_image_placeholder.image(output_image, caption="Generated Image", width='stretch')
64
+
65
+ elif uploaded_file:
66
+ input_image_placeholder.info("Click the generate image button on sidebar")