kael558 commited on
Commit
793740b
1 Parent(s): 6ca1140

add app file

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from io import BytesIO
4
+ import requests
5
+ import PIL
6
+ from PIL import Image
7
+ import numpy as np
8
+ import os
9
+ import uuid
10
+ import torch
11
+ from torch import autocast
12
+ import cv2
13
+ from matplotlib import pyplot as plt
14
+ from torchvision import transforms
15
+ from diffusers import DiffusionPipeline
16
+
17
+ auth_token = os.environ.get("API_TOKEN") or True
18
+
19
+ device = "cuda" if torch.cuda.is_available() else "cpu"
20
+
21
+ pipe = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-inpainting", dtype=torch.float16, revision="fp16", use_auth_token=auth_token).to(device)
22
+
23
+ transform = transforms.Compose([
24
+ transforms.ToTensor(),
25
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
26
+ transforms.Resize((512, 512)),
27
+ ])
28
+
29
+ def read_content(file_path: str) -> str:
30
+ """read the content of target file
31
+ """
32
+ with open(file_path, 'r', encoding='utf-8') as f:
33
+ content = f.read()
34
+
35
+ return content
36
+
37
+ def predict(dict, prompt=""):
38
+ init_image = dict["image"].convert("RGB").resize((512, 512))
39
+ mask = dict["mask"].convert("RGB").resize((512, 512))
40
+ output = pipe(prompt = prompt, image=init_image, mask_image=mask,guidance_scale=7.5)
41
+ return output.images[0], gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
42
+
43
+
44
+
45
+
46
+
47
+
48
+
49
+ image_blocks = gr.Blocks()
50
+ with image_blocks as demo:
51
+ gr.HTML(read_content("header.html"))
52
+ with gr.Group():
53
+ with gr.Box():
54
+ with gr.Row():
55
+ with gr.Column():
56
+ image = gr.Image(source='upload', tool='sketch', elem_id="image_upload", type="pil",
57
+ label="Upload").style(height=400)
58
+ with gr.Row(elem_id="prompt-container").style(mobile_collapse=False, equal_height=True):
59
+ prompt = gr.Textbox(placeholder='Your prompt (what you want in place of what is erased)',
60
+ show_label=False, elem_id="input-text")
61
+ btn = gr.Button("Inpaint!").style(
62
+ margin=False,
63
+ rounded=(False, True, True, False),
64
+ full_width=False,
65
+ )
66
+ with gr.Column():
67
+ image_out = gr.Image(label="Output", elem_id="output-img").style(height=400)
68
+
69
+ btn.click(fn=predict, inputs=[image, prompt],
70
+ outputs=[image_out])
71
+
72
+ gr.HTML(
73
+ """
74
+ <div class="footer">
75
+ <p>Model by <a href="https://huggingface.co/runwayml" style="text-decoration: underline;" target="_blank">RunwayML</a> - Gradio Demo by 🤗 Hugging Face
76
+ </p>
77
+ </div>
78
+ <div class="acknowledgments">
79
+ <p><h4>LICENSE</h4>
80
+ The model is licensed with a <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" style="text-decoration: underline;" target="_blank">CreativeML Open RAIL-M</a> license. The authors claim no rights on the outputs you generate, you are free to use them and are accountable for their use which must not go against the provisions set in this license. The license forbids you from sharing any content that violates any laws, produce any harm to a person, disseminate any personal information that would be meant for harm, spread misinformation and target vulnerable groups. For the full list of restrictions please <a href="https://huggingface.co/spaces/CompVis/stable-diffusion-license" target="_blank" style="text-decoration: underline;" target="_blank">read the license</a></p>
81
+ <p><h4>Biases and content acknowledgment</h4>
82
+ Despite how impressive being able to turn text into image is, beware to the fact that this model may output content that reinforces or exacerbates societal biases, as well as realistic faces, pornography and violence. The model was trained on the <a href="https://laion.ai/blog/laion-5b/" style="text-decoration: underline;" target="_blank">LAION-5B dataset</a>, which scraped non-curated image-text-pairs from the internet (the exception being the removal of illegal content) and is meant for research purposes. You can read more in the <a href="https://huggingface.co/CompVis/stable-diffusion-v1-4" style="text-decoration: underline;" target="_blank">model card</a></p>
83
+ </div>
84
+ """
85
+ )
86
+
87
+ image_blocks.launch()