Wootang01 commited on
Commit
d43521a
·
1 Parent(s): 5326a6a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ st.title("Grammar Corrector")
4
+ st.write("Paste or type text, submit and the machine will attempt to correct your text's grammar.")
5
+
6
+ default_text = "This should working"
7
+ sent = st.text_area("Text", default_text, height=40)
8
+ num_correct_options = st.number_input('Number of Correction Options', min_value=1, max_value=3, value=1, step=1)
9
+
10
+ from transformers import T5ConditionalGeneration, T5Tokenizer
11
+ import torch
12
+ torch_device = 'cuda' if torch.cude.is_available() else 'cpu'
13
+ tokenizer = T5Tokenizer.from_pretrained('deep-learning-analytics/GrammarCorrector')
14
+ model = T5ConditionalGeneration.from_pretrained('deep-learning-analytics/GrammarCorrector').to(torch_device)
15
+
16
+ def correct_grammar(input_text, num_correct_options=num_correct_options):
17
+ batch = tokenizer([input_text], truncation=True, padding = 'max length', max_length = 64, return_tensors = 'pt').to(torch_device)
18
+ results = model.generate(**batch, max_length = 64, num_beams = 2, num_correct_options = num_correct_options, temperature = 1.5)
19
+
20
+ return results
21
+
22
+ results = correct_grammar(sent, num_correct_options)
23
+
24
+ generated_options = []
25
+ for generated_option_idx, generated_option in enumerate(results):
26
+
27
+ text = tokenizer.decode(generated_option, clean_up_tokenization_spaces = True, skip_special_tokens = True)
28
+ generated_options.append(text)
29
+
30
+ st.write(generated options)
31
+