Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# coding=utf-8
|
2 |
+
#
|
3 |
+
# Copyright 2024 Toshihiko Aoki
|
4 |
+
#
|
5 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
6 |
+
# you may not use this file except in compliance with the License.
|
7 |
+
# You may obtain a copy of the License at
|
8 |
+
#
|
9 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
10 |
+
#
|
11 |
+
# Unless required by applicable law or agreed to in writing, software
|
12 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
13 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
14 |
+
# See the License for the specific language governing permissions and
|
15 |
+
# limitations under the License.
|
16 |
+
|
17 |
+
import argparse
|
18 |
+
import os.path
|
19 |
+
|
20 |
+
import torch
|
21 |
+
from diffusers import AutoPipelineForText2Image, LCMScheduler
|
22 |
+
from llama_cpp import Llama
|
23 |
+
import gradio as gr
|
24 |
+
|
25 |
+
parser = argparse.ArgumentParser(description='Japanese translation and hallucinations for SD')
|
26 |
+
parser.add_argument('--gguf_path',
|
27 |
+
type=str,
|
28 |
+
default=None,
|
29 |
+
help='load gguf filepath')
|
30 |
+
parser.add_argument('--sd_model_name',
|
31 |
+
type=str,
|
32 |
+
default="segmind/SSD-1B",
|
33 |
+
help='sd model HF name')
|
34 |
+
parser.add_argument('--sd_adapter_name',
|
35 |
+
type=str,
|
36 |
+
default="latent-consistency/lcm-lora-ssd-1b",
|
37 |
+
help='sd lora adaptor HF name')
|
38 |
+
parser.add_argument('--cpu',
|
39 |
+
action='store_true',
|
40 |
+
help='force use cpu (intel).')
|
41 |
+
parser.add_argument('--share',
|
42 |
+
action='store_true',
|
43 |
+
help='force use cpu.')
|
44 |
+
parser.add_argument('--openvino_path',
|
45 |
+
type=str,
|
46 |
+
default=None,
|
47 |
+
help='load openvio model filepath')
|
48 |
+
|
49 |
+
args = parser.parse_args()
|
50 |
+
|
51 |
+
llm_model_path = args.gguf_path
|
52 |
+
sd_model_name = args.sd_model_name
|
53 |
+
sd_adapter_name = args.sd_adapter_name
|
54 |
+
|
55 |
+
use_cuda = torch.cuda.is_available() and not args.cpu
|
56 |
+
|
57 |
+
width = 512
|
58 |
+
height = 512
|
59 |
+
num_inference_steps = 4
|
60 |
+
guidance_scale = 1.0
|
61 |
+
|
62 |
+
if args.cpu:
|
63 |
+
if args.openvino_path is None:
|
64 |
+
if not os.path.exists('./sd-1.5-lcm-openvino'):
|
65 |
+
from huggingface_hub import snapshot_download
|
66 |
+
download_folder = snapshot_download(repo_id="Intel/sd-1.5-lcm-openvino")
|
67 |
+
import shutil
|
68 |
+
shutil.copytree(download_folder, "./sd-1.5-lcm-openvino'")
|
69 |
+
args.openvino_path = './sd-1.5-lcm-openvino'
|
70 |
+
else:
|
71 |
+
args.openvino_path = './sd-1.5-lcm-openvino'
|
72 |
+
from openvino_pipe import LatentConsistencyEngine
|
73 |
+
pipe = LatentConsistencyEngine(
|
74 |
+
args.openvino_path
|
75 |
+
)
|
76 |
+
else:
|
77 |
+
pipe = AutoPipelineForText2Image.from_pretrained(
|
78 |
+
sd_model_name,
|
79 |
+
torch_dtype=torch.float16 if use_cuda else torch.float32,
|
80 |
+
)
|
81 |
+
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
|
82 |
+
|
83 |
+
if use_cuda:
|
84 |
+
pipe.enable_xformers_memory_efficient_attention()
|
85 |
+
pipe.enable_model_cpu_offload()
|
86 |
+
pipe.to("cuda")
|
87 |
+
|
88 |
+
if sd_adapter_name is not None:
|
89 |
+
pipe.load_lora_weights(sd_adapter_name)
|
90 |
+
if use_cuda:
|
91 |
+
pipe.fuse_lora()
|
92 |
+
|
93 |
+
if llm_model_path is None:
|
94 |
+
from huggingface_hub import hf_hub_download
|
95 |
+
llm_model_path = hf_hub_download(
|
96 |
+
repo_id="taoki/llm-jp-1.3b-v1.0-staircaptions-FT",
|
97 |
+
filename="llm-jp-1.3b-v1.0_staircaptions-FT_Q4_K_S.gguf",
|
98 |
+
)
|
99 |
+
|
100 |
+
llm = Llama(
|
101 |
+
model_path=llm_model_path,
|
102 |
+
n_gpu_layers=25 if use_cuda else -1,
|
103 |
+
)
|
104 |
+
|
105 |
+
|
106 |
+
def ja2prompt(ja_prompt):
|
107 |
+
response = llm(f"### Instruction:\n{ja_prompt}\n### Response:\n", max_tokens=128)
|
108 |
+
return response['choices'][0]['text']
|
109 |
+
|
110 |
+
|
111 |
+
def prompt2img(sd_prompt):
|
112 |
+
return pipe(
|
113 |
+
sd_prompt,
|
114 |
+
num_inference_steps=num_inference_steps,
|
115 |
+
guidance_scale=1.0,
|
116 |
+
).images[0]
|
117 |
+
|
118 |
+
|
119 |
+
with gr.Blocks(title="tiny sd web-ui") as demo:
|
120 |
+
gr.Markdown(f"## Japanese translation and hallucinations for Stable Diffusion")
|
121 |
+
with gr.Row():
|
122 |
+
with gr.Column(scale=3):
|
123 |
+
ja = gr.Text(label="ζ₯ζ¬θͺ")
|
124 |
+
translate = gr.Button("ε€ζ")
|
125 |
+
prompt = gr.Text(label="γγγ³γγ")
|
126 |
+
with gr.Column(scale=2):
|
127 |
+
result = gr.Image()
|
128 |
+
t2i = gr.Button("ηζ")
|
129 |
+
translate.click(ja2prompt, ja, prompt)
|
130 |
+
t2i.click(prompt2img, prompt, result)
|
131 |
+
|
132 |
+
if args.share:
|
133 |
+
demo.launch(share=True, server_name="0.0.0.0")
|
134 |
+
else:
|
135 |
+
demo.launch()
|