Spaces:
Sleeping
Sleeping
swcrazyfan
commited on
Commit
•
2c91872
1
Parent(s):
b3fb379
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import (T5ForConditionalGeneration,T5Tokenizer)
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
best_model_path = "swcrazyfan/Dekingify-T5-Large"
|
6 |
+
model = T5ForConditionalGeneration.from_pretrained(best_model_path)
|
7 |
+
tokenizer = T5Tokenizer.from_pretrained("swcrazyfan/Dekingify-T5-Large")
|
8 |
+
|
9 |
+
def tokenize_data(text):
|
10 |
+
# Tokenize the review body
|
11 |
+
# input_ = "paraphrase: "+ str(text) + ' >'
|
12 |
+
input_ = "deking: " + str(text) + ' </s>'
|
13 |
+
max_len = 512
|
14 |
+
# tokenize inputs
|
15 |
+
tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt')
|
16 |
+
|
17 |
+
inputs={"input_ids": tokenized_inputs['input_ids'],
|
18 |
+
"attention_mask": tokenized_inputs['attention_mask']}
|
19 |
+
return inputs
|
20 |
+
|
21 |
+
def generate_answers(text):
|
22 |
+
inputs = tokenize_data(text)
|
23 |
+
results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,
|
24 |
+
num_beams=5,
|
25 |
+
max_length=512,
|
26 |
+
min_length=1,
|
27 |
+
early_stopping=True,
|
28 |
+
num_return_sequences=1)
|
29 |
+
answer = tokenizer.decode(results[0], skip_special_tokens=True)
|
30 |
+
return answer
|
31 |
+
|
32 |
+
#iface = gr.Interface(fn=generate_answers, inputs=["Write your text here..."], outputs=["Jamesified text"])
|
33 |
+
#iface.launch(inline=False, share=True)
|
34 |
+
|
35 |
+
iface = gr.Interface(title="Dekingify", description="Write anything from around the 17th-century below. Then, click submit to 'Dekingify' it (make the text more modern).", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10)], outputs=["text"])
|
36 |
+
#iface = gr.Interface(title="King Jamesify” fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original",lines=30)],outputs=[gr.outputs.Textbox(label="King Jamesified", lines=30)])
|
37 |
+
iface.launch(inline=False)
|