mikonvergence commited on
Commit
0f33784
1 Parent(s): 1f54d7b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from src.process import *
3
+
4
+ THEME=gr.themes.Default(font=[gr.themes.GoogleFont("IBM Plex Mono"), "ui-monospace","monospace"],
5
+ primary_hue="lime",
6
+ secondary_hue="emerald",
7
+ neutral_hue="stone",
8
+ )
9
+
10
+ def create_demo():
11
+
12
+ with gr.Blocks(theme=THEME) as demo:
13
+
14
+ gr.Markdown('# 🎭 theaTRON')
15
+ gr.Markdown('<img src="file/data/image-only.png">')
16
+ gr.Markdown('Type what you want to see and the app will create images with the faces in the input while preserving body pose. Should work for multiple humans too!')
17
+
18
+ prompt = gr.Textbox(value="Astronauts, photograph inside the ISS International Space Station corridor", label="Prompt:")
19
+ n_prompt = gr.Textbox(value="", label="Negative Prompt: Avoid these features in the image...")
20
+
21
+ with gr.Group():
22
+ with gr.Box():
23
+ with gr.Column():
24
+ with gr.Row() as main_blocks:
25
+ with gr.Column() as input_step:
26
+ gr.Markdown('# Input')
27
+ with gr.Tab("Upload Image"):
28
+ image_upload = gr.Image(source='upload',
29
+ height=500,
30
+ type='pil',#numpy',
31
+ tool=None,
32
+ elem_classes="image_upload",
33
+ label='Image Upload')
34
+ with gr.Tab("Webcam"):
35
+ image_cam = gr.Image(source='webcam',
36
+ height=500,
37
+ type='pil',#numpy',
38
+ tool=None,
39
+ elem_classes="image_upload",
40
+ label='Webcam')
41
+ send_button = gr.Button(label='Generate', value='Generate')
42
+
43
+ with gr.Column() as output_step:
44
+ gr.Markdown('# Output')
45
+ output_image = gr.Gallery(label="Generated images",
46
+ show_label=False,
47
+ preview=True,
48
+ elem_id="output_image",
49
+ object_fit="contain",
50
+ height="auto")
51
+ with gr.Accordion('Settings', open=False):
52
+ num_steps = gr.Slider(label='Steps',
53
+ minimum=1,
54
+ maximum=100,
55
+ value=25,
56
+ step=1)
57
+ original_resolution=gr.Checkbox(value=False,
58
+ label="Preserve Resolution",
59
+ info="Prevent Downscaling to 512 pixels (default)")
60
+ seed = gr.Slider(label='Seed',
61
+ minimum=-1,
62
+ maximum=2147483647,
63
+ step=1,
64
+ randomize=True)
65
+
66
+
67
+ with gr.Accordion('How does it work?', open=False):
68
+ gr.Markdown('This demo was created by Mikolaj Czerkawski [@mikonvergence](https://twitter.com/mikonvergence) based on several 🌱 open-source tools. It only puts together existing models, so in some sense, nothing new here!')
69
+ gr.Markdown('## Pipeline Details')
70
+ gr.Markdown('### Step 1: Face Detection')
71
+ gr.Markdown('I use the model from kornia for face detection, since at the time of release, Segment Anything does not yet work with text prompts. Thank you, kornia team! https://kornia.readthedocs.io/en/latest/applications/face_detection.html')
72
+ gr.Markdown('### Step 2: Segment Anything')
73
+ gr.Markdown('I use Segment Anything from Meta (via HF transformers library) to segment the face based on the detection points. https://huggingface.co/docs/transformers/main/model_doc/sam')
74
+ gr.Markdown('### Step 3: Mask Post-processing')
75
+ gr.Markdown('The mask is blurred to achieve a smoother blend when recomposing the photo. Also, if a separate mask is used for the top of the head and the face, then the potential space between the two masks is morphologically filled.')
76
+ gr.Markdown('### Step 4: ControlNetInpaint')
77
+ gr.Markdown('Finally, the resulting mask is used with the ControlNetInpaint tool [ControlNetInpaint](https://github.com/mikonvergence/ControlNetInpaint) and the pose guide.')
78
+ gr.Markdown('---')
79
+ gr.Markdown('# Learn More About Diffusion 💡')
80
+ gr.Markdown('Check out my open-source ⏩[DiffusionFastForward](https://github.com/mikonvergence/DiffusionFastForward) course. It contains example code, executable notebooks, videos, notes, and a few use cases for training from scratch!')
81
+
82
+ send_button.click(fn=forward, inputs=[image_cam, image_upload, prompt, n_prompt, num_steps, seed, original_resolution], outputs=[output_image])
83
+ return demo
84
+
85
+ if __name__ == "__main__":
86
+ demo = create_demo()
87
+ demo.launch(debug=True,
88
+ share=True)