deep-learning-analytics commited on
Commit
0c8c105
1 Parent(s): d0f63c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -6
app.py CHANGED
@@ -1,4 +1,10 @@
1
- import gradio as gr
 
 
 
 
 
 
2
  ### Run Model
3
  from transformers import T5ForConditionalGeneration, T5Tokenizer
4
  import torch
@@ -6,11 +12,20 @@ torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
6
  tokenizer = T5Tokenizer.from_pretrained('deep-learning-analytics/GrammarCorrector')
7
  model = T5ForConditionalGeneration.from_pretrained('deep-learning-analytics/GrammarCorrector').to(torch_device)
8
 
9
- def correct_grammar(input_text,num_return_sequences=1):
10
  batch = tokenizer([input_text],truncation=True,padding='max_length',max_length=64, return_tensors="pt").to(torch_device)
11
  results = model.generate(**batch,max_length=64,num_beams=2, num_return_sequences=num_return_sequences, temperature=1.5)
12
- answer = tokenizer.batch_decode(results[0], skip_special_tokens=True)
13
- return answer
14
 
15
- iface = gr.Interface(fn=correct_grammar, inputs=[gr.inputs.Textbox(lines=5)], outputs=["text"])
16
- iface.launch(inline=False, share=True)
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ default_value = "Mike and Anna is skiing"
4
+ sent = st.text_area("Text", default_value, height = 275)
5
+ num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=3, value=1, step=1)
6
+
7
+ @st.cache
8
  ### Run Model
9
  from transformers import T5ForConditionalGeneration, T5Tokenizer
10
  import torch
 
12
  tokenizer = T5Tokenizer.from_pretrained('deep-learning-analytics/GrammarCorrector')
13
  model = T5ForConditionalGeneration.from_pretrained('deep-learning-analytics/GrammarCorrector').to(torch_device)
14
 
15
+ def correct_grammar(input_text,num_return_sequences=num_return_sequences):
16
  batch = tokenizer([input_text],truncation=True,padding='max_length',max_length=64, return_tensors="pt").to(torch_device)
17
  results = model.generate(**batch,max_length=64,num_beams=2, num_return_sequences=num_return_sequences, temperature=1.5)
18
+ #answer = tokenizer.batch_decode(results[0], skip_special_tokens=True)
19
+ return results
20
 
21
+ ##Prompts
22
+ st.title("Correct Grammar with Transformers 🦄")
23
+ results = correct_grammar(sent, num_return_sequences)
24
+
25
+ generated_sequences = []
26
+ for generated_sequence_idx, generated_sequence in enumerate(results):
27
+ # Decode text
28
+ text = tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True)
29
+ generated_sequences.append(generated_sequence)
30
+
31
+ st.write(generated_sequences)