File size: 1,935 Bytes
bcacabc
 
3b962b5
bcacabc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d015ead
bcacabc
 
d015ead
 
bcacabc
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import streamlit as st

st.title("NLP Project- Grammar Corrector")
st.write("")
st.write("Input your text here!")

default_value = "Urveesh and Raj is playing cricket"
sent = st.text_area("Text", default_value, height=50)
num_return_sequences = st.sidebar.number_input('Number of Return Sequences', min_value=1, max_value=3, value=1, step=1)

# Run Model
from transformers import T5ForConditionalGeneration, T5Tokenizer
import torch

torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
tokenizer = T5Tokenizer.from_pretrained('deep-learning-analytics/GrammarCorrector')
model = T5ForConditionalGeneration.from_pretrained('deep-learning-analytics/GrammarCorrector').to(torch_device)

def correct_grammar(input_text, num_return_sequences):
    batch = tokenizer([input_text], truncation=True, padding='max_length', max_length=len(input_text), return_tensors="pt").to(torch_device)
    results = model.generate(**batch, max_length=len(input_text), num_beams=2, num_return_sequences=num_return_sequences, temperature=1.5)
    return results

# Prompts
results = correct_grammar(sent, num_return_sequences)

# Decode generated sequences
generated_sequences = [tokenizer.decode(generated_sequence, clean_up_tokenization_spaces=True, skip_special_tokens=True) for generated_sequence in results]

# Add "Check Now" button
if st.button("Check Now"):
    st.write("### Results:")

    # Check correctness and display in green or red
    for generated_sequence in generated_sequences:
        is_correct = generated_sequence == sent
        color = "green" if is_correct else "red"
        st.warning(f"**Generated Sentence:**", generated_sequence, f" (Matches original: {is_correct})", unsafe_allow_html=True)

        # If incorrect, display correct grammar sentence in a box
        # if not is_correct:
        #     st.warning(f"**Correct Grammar:** {sent}")

# Display original input
st.write("### Original Input:")
st.write(sent)