Spaces:
Running
Running
dong
commited on
Commit
·
c62628b
1
Parent(s):
1456b3a
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# @title chạy với giao diện Gradio (load mode từ local)
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
from diffusers import StableDiffusionPipeline
|
5 |
+
from googletrans import Translator
|
6 |
+
|
7 |
+
import torch
|
8 |
+
|
9 |
+
# Model initialization
|
10 |
+
def load_model(model_path):
|
11 |
+
pipe = StableDiffusionPipeline.from_single_file(model_path, safety_checker=None, requires_safety_checker=False, torch_dtype=torch.float16)
|
12 |
+
return pipe.to("cuda")
|
13 |
+
|
14 |
+
model_path = r'D:\Truong_apacons_2023\EasySD\models\stable-diffusion\realisticVisionV51_v51VAE.safetensors'
|
15 |
+
pipe = load_model(model_path)
|
16 |
+
|
17 |
+
translator = Translator()
|
18 |
+
|
19 |
+
#mô tả tiêu cực
|
20 |
+
negative_prompt = "disfigured, ugly, verybadimagenegative_v1.3, ng_deepnegative_v1_75t, (ugly face:0.8),cross-eyed,sketches, (worst quality:2), (low quality:2), (normal quality:2), lowres, normal quality, ((monochrome)), ((grayscale)), bad anatomy, DeepNegative, facing away, tilted head, {Multiple people}, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worstquality, low quality, normal quality, jpegartifacts, signature, watermark, username, blurry..."
|
21 |
+
|
22 |
+
def generate(prompt, mau_nuoc, render, height=512, width=768):
|
23 |
+
height = int(height)
|
24 |
+
width = int(width)
|
25 |
+
|
26 |
+
if mau_nuoc:
|
27 |
+
prompt = "Tranh Màu nước, " + prompt
|
28 |
+
|
29 |
+
if render:
|
30 |
+
prompt = "Octain render and Vray render and Corona render 3d model, fog effect, perfect perspective, perfect edges, " + prompt
|
31 |
+
|
32 |
+
# Xoá từ "realistic", "photographic" và "photorealistic"
|
33 |
+
prompt = prompt.replace("realistic", "").replace("photographic", "").replace("photorealistic", "")
|
34 |
+
|
35 |
+
# Translate
|
36 |
+
translated_prompt = translator.translate(prompt).text
|
37 |
+
|
38 |
+
# Hàm sinh ảnh
|
39 |
+
images = pipe(
|
40 |
+
prompt=translated_prompt,
|
41 |
+
height=height,
|
42 |
+
width=width,
|
43 |
+
num_inference_steps=50,
|
44 |
+
guidance_scale=7,
|
45 |
+
negative_prompt=negative_prompt,
|
46 |
+
).images
|
47 |
+
|
48 |
+
image = images[0]
|
49 |
+
return image
|
50 |
+
|
51 |
+
Ví_dụ = [
|
52 |
+
["Chân dung chi tiết cao của một cô gái trẻ xinh đẹp với mái tóc trắng và đôi mắt sắc nét, 35 mm, 8k, sắc nét"],
|
53 |
+
["Cô gái xinh đẹp mặc váy màu vàng chơi guitar, đính kim tuyến lộng lẫy, hình ảnh sắc nét, không bị mờ"],
|
54 |
+
["Ngôi nhà màu vàng trong rừng, ánh sáng mặt trời tuyệt đẹp"],
|
55 |
+
["A cat playing with a ball of yarn"],
|
56 |
+
["Chú chó chạy xe đạp màu hồng"],
|
57 |
+
["Bình minh trên bờ biển"]
|
58 |
+
]
|
59 |
+
|
60 |
+
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=generate,
|
67 |
+
inputs=[
|
68 |
+
gr.inputs.Textbox(),
|
69 |
+
gr.inputs.Checkbox(label="Màu nước"),
|
70 |
+
gr.inputs.Checkbox(label="Render"),
|
71 |
+
gr.inputs.Number(label="Kích thước ảnh - Chiều cao", default=512),
|
72 |
+
gr.inputs.Number(label="Kích thước ảnh - Chiều rộng", default=768)
|
73 |
+
],
|
74 |
+
outputs="image",
|
75 |
+
title="Sáng tạo hình ảnh",
|
76 |
+
description="Mô tả về chương trình<br>Hãy viết mô tả mà bạn mong muốn vào ô nhập prompt và bấm vào nút Submit",
|
77 |
+
examples=Ví_dụ
|
78 |
+
)
|
79 |
+
|
80 |
+
iface.launch(share=True, debug=True)
|
81 |
+
|