Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.title("Correct Grammar with Transformers 🦄")
|
| 4 |
+
st.write("")
|
| 5 |
+
st.write("Input your text here!")
|
| 6 |
+
|
| 7 |
+
default_value = "Urveesh and Raj is playing cricket"
|
| 8 |
+
sent = st.text_area("Text", default_value, height=50)
|
| 9 |
+
num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=3, value=1, step=1)
|
| 10 |
+
|
| 11 |
+
# Run Model
|
| 12 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 13 |
+
import torch
|
| 14 |
+
|
| 15 |
+
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 16 |
+
tokenizer = T5Tokenizer.from_pretrained('deep-learning-analytics/GrammarCorrector')
|
| 17 |
+
model = T5ForConditionalGeneration.from_pretrained('deep-learning-analytics/GrammarCorrector').to(torch_device)
|
| 18 |
+
|
| 19 |
+
def correct_grammar(input_text, num_return_sequences):
|
| 20 |
+
batch = tokenizer([input_text], truncation=True, padding='max_length', max_length=len(input_text), return_tensors="pt").to(torch_device)
|
| 21 |
+
results = model.generate(**batch, max_length=len(input_text), num_beams=2, num_return_sequences=num_return_sequences, temperature=1.5)
|
| 22 |
+
return results
|
| 23 |
+
|
| 24 |
+
# Prompts
|
| 25 |
+
results = correct_grammar(sent, num_return_sequences)
|
| 26 |
+
|
| 27 |
+
# Decode generated sequences
|
| 28 |
+
generated_sequences = [tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True, skip_special_tokens=True) for generated_sequence in results]
|
| 29 |
+
|
| 30 |
+
# Add "Check Now" button
|
| 31 |
+
if st.button("Check Now"):
|
| 32 |
+
st.write("### Results:")
|
| 33 |
+
|
| 34 |
+
# Check correctness and display in green or red
|
| 35 |
+
for generated_sequence in generated_sequences:
|
| 36 |
+
is_correct = generated_sequence == sent
|
| 37 |
+
color = "green" if is_correct else "red"
|
| 38 |
+
st.write(f"**Generated Sentence:**", generated_sequence, f" (Matches original: {is_correct})", unsafe_allow_html=True)
|
| 39 |
+
|
| 40 |
+
# If incorrect, display correct grammar sentence in a box
|
| 41 |
+
if not is_correct:
|
| 42 |
+
st.warning(f"**Correct Grammar:** {sent}")
|
| 43 |
+
|
| 44 |
+
# Display original input
|
| 45 |
+
st.write("### Original Input:")
|
| 46 |
+
st.write(sent)
|