stephenleejm commited on
Commit
220f52e
•
1 Parent(s): 1baa33a

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +54 -0
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
3
+ import torch
4
+
5
+
6
+ title = "GPT-J-6B"
7
+ description = "Gradio Demo for GPT-J 6B, a transformer model trained using Ben Wang's Mesh Transformer JAX. 'GPT-J' refers to the class of model, while '6B' represents the number of trainable parameters. To use it, simply add your text, or click one of the examples to load them. I've used the API on this Space to deploy the GPT-J-6B model on a Telegram bot. Link to blog post below 👇"
8
+ article = "<p style='text-align: center'><a href='https://dicksonneoh.com/portfolio/deploy_gpt_hf_models_on_telegram/' target='_blank'>⚡ Blog post 👉 Deploying GPT-J Models on a Telegram Bot with Hugging Face Hub - For Free</a></p>"
9
+ examples = [
10
+ ['The tower is 324 metres (1,063 ft) tall,'],
11
+ ["The Moon's orbit around Earth has"],
12
+ ["The smooth Borealis basin in the Northern Hemisphere covers 40%"]
13
+ ]
14
+
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
+ print(device)
17
+ tokenizer = T5Tokenizer.from_pretrained('stephenleejm/T5_yoda_translator')
18
+ t5_model = T5ForConditionalGeneration.from_pretrained('stephenleejm/T5_yoda_translator')
19
+ t5_model.to(device)
20
+
21
+ def test_text(s, task):
22
+ test_sent = task + ' ' + s
23
+ # +' </s>'
24
+ test_tokenized = tokenizer.encode_plus(test_sent, return_tensors="pt").to(device)
25
+
26
+ test_input_ids = test_tokenized["input_ids"]
27
+ test_attention_mask = test_tokenized["attention_mask"]
28
+
29
+ t5_model.eval()
30
+ beam_outputs = t5_model.generate(
31
+ input_ids=test_input_ids, attention_mask=test_attention_mask,
32
+ max_length=96,
33
+ early_stopping=True,
34
+ num_beams=10,
35
+ num_return_sequences=3,
36
+ no_repeat_ngram_size=2
37
+ )
38
+
39
+ answer = ""
40
+ for beam_output in beam_outputs:
41
+ sent = tokenizer.decode(beam_output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
42
+ print(sent)
43
+ answer += sent + "\n"
44
+ return answer
45
+
46
+
47
+ def run(dropdown, text):
48
+ dropdown = "e_to_y:" if dropdown == "English To Yoda" else "y_to_e:"
49
+ print(dropdown)
50
+ return test_text(text, dropdown)
51
+
52
+
53
+ gr.Interface(fn=run,
54
+ inputs=[gr.inputs.Dropdown(["Yoda To English", "English To Yoda"]),gr.inputs.Textbox(lines=5, label="Input Text")], outputs="text").launch()