Spaces:
Runtime error
Runtime error
Commit
•
da03e9c
0
Parent(s):
Duplicate from yizhangliu/Text-to-Image
Browse filesCo-authored-by: yizhangliu <yizhangliu@users.noreply.huggingface.co>
- .gitattributes +34 -0
- README.md +13 -0
- app.py +157 -0
- baidu_translate/module.py +104 -0
- requirements.txt +8 -0
.gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Text To Image
|
3 |
+
emoji: 👀
|
4 |
+
colorFrom: purple
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.12.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: yizhangliu/Text-to-Image
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,157 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import gradio as gr
|
3 |
+
import sys
|
4 |
+
import random
|
5 |
+
import paddlehub as hub
|
6 |
+
from loguru import logger
|
7 |
+
|
8 |
+
language_translation_model = hub.Module(directory=f'./baidu_translate')
|
9 |
+
def getTextTrans(text, source='zh', target='en'):
|
10 |
+
try:
|
11 |
+
text_translation = language_translation_model.translate(text, source, target)
|
12 |
+
return text_translation
|
13 |
+
except Exception as e:
|
14 |
+
return text
|
15 |
+
|
16 |
+
|
17 |
+
model_ids = {
|
18 |
+
"models/stabilityai/stable-diffusion-2-1":"sd-v2-1",
|
19 |
+
"models/stabilityai/stable-diffusion-2":"sd-v2-0",
|
20 |
+
"models/runwayml/stable-diffusion-v1-5":"sd-v1-5",
|
21 |
+
"models/CompVis/stable-diffusion-v1-4":"sd-v1-4",
|
22 |
+
"models/prompthero/openjourney":"openjourney",
|
23 |
+
# "models/ShadoWxShinigamI/Midjourney-Rangoli":"midjourney",
|
24 |
+
"models/hakurei/waifu-diffusion":"waifu-diffusion",
|
25 |
+
"models/Linaqruf/anything-v3.0":"anything-v3.0",
|
26 |
+
}
|
27 |
+
tab_actions = []
|
28 |
+
tab_titles = []
|
29 |
+
for model_id in model_ids.keys():
|
30 |
+
print(model_id, model_ids[model_id])
|
31 |
+
try:
|
32 |
+
tab = gr.Interface.load(model_id)
|
33 |
+
tab_actions.append(tab)
|
34 |
+
tab_titles.append(model_ids[model_id])
|
35 |
+
except:
|
36 |
+
logger.info(f"load_fail__{model_id}_")
|
37 |
+
|
38 |
+
def infer(prompt):
|
39 |
+
logger.info(f"infer_1__")
|
40 |
+
prompt = getTextTrans(prompt, source='zh', target='en') + f',{random.randint(0,sys.maxsize)}'
|
41 |
+
return prompt
|
42 |
+
|
43 |
+
start_work = """async() => {
|
44 |
+
function isMobile() {
|
45 |
+
try {
|
46 |
+
document.createEvent("TouchEvent"); return true;
|
47 |
+
} catch(e) {
|
48 |
+
return false;
|
49 |
+
}
|
50 |
+
}
|
51 |
+
|
52 |
+
function getClientHeight()
|
53 |
+
{
|
54 |
+
var clientHeight=0;
|
55 |
+
if(document.body.clientHeight&&document.documentElement.clientHeight) {
|
56 |
+
var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
57 |
+
} else {
|
58 |
+
var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;
|
59 |
+
}
|
60 |
+
return clientHeight;
|
61 |
+
}
|
62 |
+
|
63 |
+
function setNativeValue(element, value) {
|
64 |
+
const valueSetter = Object.getOwnPropertyDescriptor(element.__proto__, 'value').set;
|
65 |
+
const prototype = Object.getPrototypeOf(element);
|
66 |
+
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set;
|
67 |
+
|
68 |
+
if (valueSetter && valueSetter !== prototypeValueSetter) {
|
69 |
+
prototypeValueSetter.call(element, value);
|
70 |
+
} else {
|
71 |
+
valueSetter.call(element, value);
|
72 |
+
}
|
73 |
+
}
|
74 |
+
|
75 |
+
var gradioEl = document.querySelector('body > gradio-app').shadowRoot;
|
76 |
+
if (!gradioEl) {
|
77 |
+
gradioEl = document.querySelector('body > gradio-app');
|
78 |
+
}
|
79 |
+
|
80 |
+
if (typeof window['gradioEl'] === 'undefined') {
|
81 |
+
window['gradioEl'] = gradioEl;
|
82 |
+
|
83 |
+
tabitems = window['gradioEl'].querySelectorAll('.tabitem');
|
84 |
+
for (var i = 0; i < tabitems.length; i++) {
|
85 |
+
tabitems[i].childNodes[0].children[0].style.display='none';
|
86 |
+
tabitems[i].childNodes[0].children[1].children[0].style.display='none';
|
87 |
+
tabitems[i].childNodes[0].children[1].children[1].children[0].children[1].style.display="none";
|
88 |
+
}
|
89 |
+
tab_demo = window['gradioEl'].querySelectorAll('#tab_demo')[0];
|
90 |
+
tab_demo.style.display = "block";
|
91 |
+
tab_demo.setAttribute('style', 'height: 100%;');
|
92 |
+
|
93 |
+
const page1 = window['gradioEl'].querySelectorAll('#page_1')[0];
|
94 |
+
const page2 = window['gradioEl'].querySelectorAll('#page_2')[0];
|
95 |
+
|
96 |
+
page1.style.display = "none";
|
97 |
+
page2.style.display = "block";
|
98 |
+
|
99 |
+
window['prevPrompt'] = '';
|
100 |
+
window['doCheckPrompt'] = 0;
|
101 |
+
window['checkPrompt'] = function checkPrompt() {
|
102 |
+
try {
|
103 |
+
texts = window['gradioEl'].querySelectorAll('textarea');
|
104 |
+
text0 = texts[0];
|
105 |
+
text1 = texts[1];
|
106 |
+
if (window['doCheckPrompt'] === 0 && window['prevPrompt'] !== text1.value) {
|
107 |
+
console.log('_____new prompt___[' + text1.value + ']_');
|
108 |
+
window['doCheckPrompt'] = 1;
|
109 |
+
window['prevPrompt'] = text1.value;
|
110 |
+
for (var i = 2; i < texts.length; i++) {
|
111 |
+
setNativeValue(texts[i], text1.value);
|
112 |
+
texts[i].dispatchEvent(new Event('input', { bubbles: true }));
|
113 |
+
}
|
114 |
+
setTimeout(function() {
|
115 |
+
btns = window['gradioEl'].querySelectorAll('button');
|
116 |
+
for (var i = 0; i < btns.length; i++) {
|
117 |
+
if (btns[i].innerText == 'Submit') {
|
118 |
+
btns[i].click();
|
119 |
+
}
|
120 |
+
}
|
121 |
+
window['doCheckPrompt'] = 0;
|
122 |
+
}, 10);
|
123 |
+
}
|
124 |
+
} catch(e) {
|
125 |
+
}
|
126 |
+
}
|
127 |
+
window['checkPrompt_interval'] = window.setInterval("window.checkPrompt()", 100);
|
128 |
+
}
|
129 |
+
|
130 |
+
return false;
|
131 |
+
}"""
|
132 |
+
|
133 |
+
with gr.Blocks(title='Text to Image') as demo:
|
134 |
+
with gr.Group(elem_id="page_1", visible=True) as page_1:
|
135 |
+
with gr.Box():
|
136 |
+
with gr.Row():
|
137 |
+
start_button = gr.Button("Let's GO!", elem_id="start-btn", visible=True)
|
138 |
+
start_button.click(fn=None, inputs=[], outputs=[], _js=start_work)
|
139 |
+
|
140 |
+
with gr.Group(elem_id="page_2", visible=False) as page_2:
|
141 |
+
with gr.Row(elem_id="prompt_row"):
|
142 |
+
prompt_input0 = gr.Textbox(lines=4, label="prompt")
|
143 |
+
prompt_input1 = gr.Textbox(lines=4, label="prompt", visible=False)
|
144 |
+
with gr.Row():
|
145 |
+
submit_btn = gr.Button(value = "submit",elem_id="erase-btn").style(
|
146 |
+
margin=True,
|
147 |
+
rounded=(True, True, True, True),
|
148 |
+
)
|
149 |
+
with gr.Row(elem_id='tab_demo', visible=True).style(height=5):
|
150 |
+
tab_demo = gr.TabbedInterface(tab_actions, tab_titles)
|
151 |
+
|
152 |
+
submit_btn.click(fn=infer, inputs=[prompt_input0], outputs=[prompt_input1])
|
153 |
+
|
154 |
+
if __name__ == "__main__":
|
155 |
+
demo.launch()
|
156 |
+
|
157 |
+
|
baidu_translate/module.py
ADDED
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import random
|
3 |
+
from hashlib import md5
|
4 |
+
from typing import Optional
|
5 |
+
|
6 |
+
import requests
|
7 |
+
|
8 |
+
import paddlehub as hub
|
9 |
+
from paddlehub.module.module import moduleinfo
|
10 |
+
from paddlehub.module.module import runnable
|
11 |
+
from paddlehub.module.module import serving
|
12 |
+
|
13 |
+
|
14 |
+
def make_md5(s, encoding='utf-8'):
|
15 |
+
return md5(s.encode(encoding)).hexdigest()
|
16 |
+
|
17 |
+
|
18 |
+
@moduleinfo(name="baidu_translate",
|
19 |
+
version="1.0.0",
|
20 |
+
type="text/machine_translation",
|
21 |
+
summary="",
|
22 |
+
author="baidu-nlp",
|
23 |
+
author_email="paddle-dev@baidu.com")
|
24 |
+
class BaiduTranslate:
|
25 |
+
|
26 |
+
def __init__(self, appid=None, appkey=None):
|
27 |
+
"""
|
28 |
+
:param appid: appid for requesting Baidu translation service.
|
29 |
+
:param appkey: appkey for requesting Baidu translation service.
|
30 |
+
"""
|
31 |
+
# Set your own appid/appkey.
|
32 |
+
if appid == None:
|
33 |
+
self.appid = '20201015000580007'
|
34 |
+
else:
|
35 |
+
self.appid = appid
|
36 |
+
if appkey is None:
|
37 |
+
self.appkey = 'IFJB6jBORFuMmVGDRud1'
|
38 |
+
else:
|
39 |
+
self.appkey = appkey
|
40 |
+
self.url = 'http://api.fanyi.baidu.com/api/trans/vip/translate'
|
41 |
+
|
42 |
+
def translate(self, query: str, from_lang: Optional[str] = "en", to_lang: Optional[int] = "zh"):
|
43 |
+
"""
|
44 |
+
Create image by text prompts using ErnieVilG model.
|
45 |
+
|
46 |
+
:param query: Text to be translated.
|
47 |
+
:param from_lang: Source language.
|
48 |
+
:param to_lang: Dst language.
|
49 |
+
|
50 |
+
Return translated string.
|
51 |
+
"""
|
52 |
+
# Generate salt and sign
|
53 |
+
salt = random.randint(32768, 65536)
|
54 |
+
sign = make_md5(self.appid + query + str(salt) + self.appkey)
|
55 |
+
|
56 |
+
# Build request
|
57 |
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
58 |
+
payload = {'appid': self.appid, 'q': query, 'from': from_lang, 'to': to_lang, 'salt': salt, 'sign': sign}
|
59 |
+
|
60 |
+
# Send request
|
61 |
+
try:
|
62 |
+
r = requests.post(self.url, params=payload, headers=headers)
|
63 |
+
result = r.json()
|
64 |
+
except Exception as e:
|
65 |
+
error_msg = str(e)
|
66 |
+
raise RuntimeError(error_msg)
|
67 |
+
if 'error_code' in result:
|
68 |
+
raise RuntimeError(result['error_msg'])
|
69 |
+
return result['trans_result'][0]['dst']
|
70 |
+
|
71 |
+
@runnable
|
72 |
+
def run_cmd(self, argvs):
|
73 |
+
"""
|
74 |
+
Run as a command.
|
75 |
+
"""
|
76 |
+
self.parser = argparse.ArgumentParser(description="Run the {} module.".format(self.name),
|
77 |
+
prog='hub run {}'.format(self.name),
|
78 |
+
usage='%(prog)s',
|
79 |
+
add_help=True)
|
80 |
+
self.arg_input_group = self.parser.add_argument_group(title="Input options", description="Input data. Required")
|
81 |
+
self.add_module_input_arg()
|
82 |
+
args = self.parser.parse_args(argvs)
|
83 |
+
if args.appid is not None and args.appkey is not None:
|
84 |
+
self.appid = args.appid
|
85 |
+
self.appkey = args.appkey
|
86 |
+
result = self.translate(args.query, args.from_lang, args.to_lang)
|
87 |
+
return result
|
88 |
+
|
89 |
+
@serving
|
90 |
+
def serving_method(self, query, from_lang, to_lang):
|
91 |
+
"""
|
92 |
+
Run as a service.
|
93 |
+
"""
|
94 |
+
return self.translate(query, from_lang, to_lang)
|
95 |
+
|
96 |
+
def add_module_input_arg(self):
|
97 |
+
"""
|
98 |
+
Add the command input options.
|
99 |
+
"""
|
100 |
+
self.arg_input_group.add_argument('--query', type=str)
|
101 |
+
self.arg_input_group.add_argument('--from_lang', type=str, default='en', help="源语言")
|
102 |
+
self.arg_input_group.add_argument('--to_lang', type=str, default='zh', help="目标语言")
|
103 |
+
self.arg_input_group.add_argument('--appid', type=str, default=None, help="注册得到的个人appid")
|
104 |
+
self.arg_input_group.add_argument('--appkey', type=str, default=None, help="注册得到的个人appkey")
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
ftfy
|
3 |
+
spacy
|
4 |
+
diffusers
|
5 |
+
transformers
|
6 |
+
paddlepaddle==2.3.2
|
7 |
+
paddlehub
|
8 |
+
loguru
|