Spaces:
Sleeping
Sleeping
Upload with huggingface_hub
Browse files- ._predict.py +0 -0
- ._requirements.txt +0 -0
- app.py +79 -0
- predict.py +47 -0
- requirements.txt +2 -0
._predict.py
ADDED
Binary file (212 Bytes). View file
|
|
._requirements.txt
ADDED
Binary file (212 Bytes). View file
|
|
app.py
ADDED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#from summary_reverse_pred_native import *
|
2 |
+
#### daspartho/prompt-extend
|
3 |
+
|
4 |
+
import gradio as gr
|
5 |
+
import os
|
6 |
+
from predict import *
|
7 |
+
|
8 |
+
#device = "cuda:0"
|
9 |
+
device = "cpu"
|
10 |
+
assert device.startswith("cpu") or device.startswith("cuda")
|
11 |
+
|
12 |
+
from transformers import (
|
13 |
+
T5ForConditionalGeneration,
|
14 |
+
MT5ForConditionalGeneration,
|
15 |
+
ByT5Tokenizer,
|
16 |
+
PreTrainedTokenizer,
|
17 |
+
T5TokenizerFast as T5Tokenizer,
|
18 |
+
MT5TokenizerFast as MT5Tokenizer,
|
19 |
+
AutoModelForSeq2SeqLM,
|
20 |
+
AutoTokenizer,
|
21 |
+
BertTokenizer,
|
22 |
+
GPT2LMHeadModel,
|
23 |
+
)
|
24 |
+
|
25 |
+
#### "svjack/prompt-extend-chinese-gpt"
|
26 |
+
#model_path = "/home/featurize/zh_p_extend_outputs/simplet5-epoch-3-train-loss-1.2628-val-loss-1.6293"
|
27 |
+
model_path = "svjack/prompt-extend-chinese-gpt"
|
28 |
+
tokenizer1 = BertTokenizer.from_pretrained(model_path)
|
29 |
+
model1 = GPT2LMHeadModel.from_pretrained(model_path)
|
30 |
+
|
31 |
+
if device.startswith("cuda"):
|
32 |
+
zh_pe_model = Obj(model1, tokenizer1, device = "cuda:0")
|
33 |
+
else:
|
34 |
+
zh_pe_model = Obj(model1, tokenizer1, device = "cpu")
|
35 |
+
|
36 |
+
def one_ele_trans(x):
|
37 |
+
x = x.strip()
|
38 |
+
x = x[1:] if x.startswith("'") else x
|
39 |
+
x = x[:-1] if x.endswith("'") else x
|
40 |
+
x = x[1:] if x.startswith('"') else x
|
41 |
+
x = x[:-1] if x.endswith('"') else x
|
42 |
+
return x
|
43 |
+
|
44 |
+
def stdf_prompt_expander(x, do_sample):
|
45 |
+
assert type(x) == type("")
|
46 |
+
return zh_pe_model.predict(
|
47 |
+
one_ele_trans(x.strip()).strip(),
|
48 |
+
max_length = 128,
|
49 |
+
do_sample = do_sample
|
50 |
+
)[0].replace(" ", "").strip()
|
51 |
+
|
52 |
+
#text0 = "飓风格特是1993年9月在墨西哥和整个中美洲引发严重洪灾的大规模热带气旋,源于9月14日西南加勒比海上空一股东风波。次日从尼加拉瓜登岸,经过洪都拉斯后于9月17日在洪都拉斯湾再次达到热带风暴标准,但次日进入伯利兹上空后就减弱成热带低气压。穿过尤卡坦半岛后,在9月20日强化成二级飓风,从韦拉克鲁斯州的图斯潘附近登陆墨西哥。9月21日从纳亚里特州进入太平洋时已降级成热带低气压,最终于5天后在开放水域上空消散。"
|
53 |
+
#text1 = "珊瑚坝是长江中的一处河漫滩,位于长江重庆市渝中区区段主航道左侧[1],靠近渝中半岛,原分属重庆市市中区菜园坝街道和石板坡街道[2],现属渝中区菜园坝街道石板坡社区[3],是长江上游缓冲地段自然冲积沙洲,略呈纺锤形[4]或椭圆形,长约1800米,宽约600米,坝上遍布鹅卵石和水草。每年夏季洪水时均被淹没,其余时间常露水面,枯水期则与长江左岸相连[5]。"
|
54 |
+
prompt = "一只凶猛的老虎,咬死了一只豺狼。"
|
55 |
+
|
56 |
+
example_sample = [
|
57 |
+
[prompt, False],
|
58 |
+
#[text1, False],
|
59 |
+
]
|
60 |
+
|
61 |
+
def demo_func(prefix, do_sample):
|
62 |
+
#l = simple_pred(prefix, do_sample = do_sample)
|
63 |
+
x = stdf_prompt_expander(prefix, do_sample = do_sample)
|
64 |
+
return {
|
65 |
+
"Prompt extend": x
|
66 |
+
}
|
67 |
+
|
68 |
+
demo = gr.Interface(
|
69 |
+
fn=demo_func,
|
70 |
+
inputs=[gr.Text(label = "Prompt"),
|
71 |
+
gr.Checkbox(label="do sample"),
|
72 |
+
],
|
73 |
+
outputs="json",
|
74 |
+
title=f"Stable Diffusion Chinese Prompt Extend 🐰 demonstration",
|
75 |
+
examples=example_sample if example_sample else None,
|
76 |
+
cache_examples = False
|
77 |
+
)
|
78 |
+
|
79 |
+
demo.launch(server_name=None, server_port=None)
|
predict.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
class Obj:
|
2 |
+
def __init__(self, model, tokenizer, device = "cpu"):
|
3 |
+
self.model = model
|
4 |
+
self.tokenizer = tokenizer
|
5 |
+
self.device = device
|
6 |
+
self.model = self.model.to(self.device)
|
7 |
+
|
8 |
+
def predict(
|
9 |
+
self,
|
10 |
+
source_text: str,
|
11 |
+
max_length: int = 512,
|
12 |
+
num_return_sequences: int = 1,
|
13 |
+
num_beams: int = 2,
|
14 |
+
top_k: int = 50,
|
15 |
+
top_p: float = 0.95,
|
16 |
+
do_sample: bool = True,
|
17 |
+
repetition_penalty: float = 2.5,
|
18 |
+
length_penalty: float = 1.0,
|
19 |
+
early_stopping: bool = True,
|
20 |
+
skip_special_tokens: bool = True,
|
21 |
+
clean_up_tokenization_spaces: bool = True,
|
22 |
+
):
|
23 |
+
input_ids = self.tokenizer.encode(
|
24 |
+
source_text, return_tensors="pt", add_special_tokens=True
|
25 |
+
)
|
26 |
+
input_ids = input_ids.to(self.device)
|
27 |
+
generated_ids = self.model.generate(
|
28 |
+
input_ids=input_ids,
|
29 |
+
num_beams=num_beams,
|
30 |
+
max_length=max_length,
|
31 |
+
repetition_penalty=repetition_penalty,
|
32 |
+
length_penalty=length_penalty,
|
33 |
+
early_stopping=early_stopping,
|
34 |
+
top_p=top_p,
|
35 |
+
top_k=top_k,
|
36 |
+
num_return_sequences=num_return_sequences,
|
37 |
+
do_sample = do_sample
|
38 |
+
)
|
39 |
+
preds = [
|
40 |
+
self.tokenizer.decode(
|
41 |
+
g,
|
42 |
+
skip_special_tokens=skip_special_tokens,
|
43 |
+
clean_up_tokenization_spaces=clean_up_tokenization_spaces,
|
44 |
+
)
|
45 |
+
for g in generated_ids
|
46 |
+
]
|
47 |
+
return preds
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|