swcrazyfan commited on
Commit
106e2e7
1 Parent(s): 275c097

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -0
app.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import (T5ForConditionalGeneration,T5Tokenizer)
3
+ import gradio as gr
4
+
5
+ best_model_path = "swcrazyfan/KingJamesify-T5-large"
6
+ model = T5ForConditionalGeneration.from_pretrained(best_model_path)
7
+ tokenizer = T5Tokenizer.from_pretrained("swcrazyfan/KingJamesify-T5-large")
8
+
9
+ def tokenize_data(text):
10
+ # Tokenize the review body
11
+ input_ = "kingify: " + str(text) + ' </s>'
12
+ max_len = 512
13
+ # tokenize inputs
14
+ tokenized_inputs = tokenizer(input_, padding='max_length', truncation=True, max_length=max_len, return_attention_mask=True, return_tensors='pt')
15
+
16
+ inputs={"input_ids": tokenized_inputs['input_ids'],
17
+ "attention_mask": tokenized_inputs['attention_mask']}
18
+ return inputs
19
+
20
+ def generate_answers(text, temperature, num_beams, max_length):
21
+ inputs = tokenize_data(text)
22
+ results= model.generate(input_ids= inputs['input_ids'], attention_mask=inputs['attention_mask'], do_sample=True,
23
+ num_beams=num_beams,
24
+ max_length=max_length,
25
+ min_length=1,
26
+ early_stopping=True,
27
+ num_return_sequences=1,
28
+ temperature=temperature)
29
+ answer = tokenizer.decode(results[0], skip_special_tokens=True)
30
+ return answer
31
+
32
+ iface = gr.Interface(title="Kingify", description="Write anything below. Then, click submit to 'Kingify' it.", fn=generate_answers, inputs=[gr.inputs.Textbox(label="Original Text",lines=10), gr.inputs.Slider(label="Temperature", default=1.0, min_value=0.0, max_value=2.0, step=0.1), gr.inputs.Slider(label="Number of Beams", default=5, min_value=1, max_value=10, step=1), gr.inputs.Textbox(label="Max Length", default=512, lines=1)], outputs=["text"])