File size: 3,215 Bytes
f0f5479
f8c1b24
 
 
e0a4ce0
 
0c05d31
700ece2
 
0c05d31
594b8b0
 
 
 
 
e0a4ce0
 
 
 
594b8b0
e0a4ce0
0c05d31
 
 
 
 
 
 
 
 
 
 
 
700ece2
0c05d31
700ece2
 
 
 
 
 
 
0c05d31
 
 
700ece2
0c05d31
700ece2
594b8b0
e0a4ce0
 
 
 
 
594b8b0
e0a4ce0
 
 
 
0c05d31
 
9f8b11b
 
 
e0a4ce0
 
 
 
700ece2
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# app.py
import os
os.system('python download.py')

from transformers import T5Tokenizer, T5ForConditionalGeneration
import gradio as gr
import nltk
from nltk.tokenize import sent_tokenize, word_tokenize
from difflib import SequenceMatcher

# Download necessary resources
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
nltk.download('wordnet')

# Load a pre-trained T5 model specifically fine-tuned for grammar correction
tokenizer = T5Tokenizer.from_pretrained("prithivida/grammar_error_correcter_v1")
model = T5ForConditionalGeneration.from_pretrained("prithivida/grammar_error_correcter_v1")

# Function to perform grammar correction
def grammar_check(text):
    sentences = sent_tokenize(text)
    corrected_sentences = []

    for sentence in sentences:
        input_text = f"gec: {sentence}"
        input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
        outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
        corrected_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
        corrected_sentences.append(corrected_sentence)

    # Function to underline and color revised parts
    def underline_and_color_revisions(original, corrected):
        diff = SequenceMatcher(None, original.split(), corrected.split())
        result = []
        for tag, i1, i2, j1, j2 in diff.get_opcodes():
            if tag == 'insert':
                result.append(f"<u style='color:red;'>{' '.join(corrected.split()[j1:j2])}</u>")
            elif tag == 'replace':
                result.append(f"<u style='color:red;'>{' '.join(corrected.split()[j1:j2])}</u>")
            elif tag == 'equal':
                result.append(' '.join(original.split()[i1:i2]))
        return " ".join(result)

    corrected_text = " ".join(
        underline_and_color_revisions(orig, corr) for orig, corr in zip(sentences, corrected_sentences)
    )

    return corrected_text

# Create Gradio interface with a writing prompt
interface = gr.Interface(
    fn=grammar_check,
    inputs="text",
    outputs="html",  # Output type is HTML
    title="Grammar Checker",
    description=(
        "Enter text to check for grammar mistakes.\n\n"
        "Writing Prompt:\n"
        "In the story, Alex and his friends discovered an ancient treasure in Whispering Hollow and decided to donate the artifacts to the local museum.\n\n"
        "In the past, did you have a similar experience where you found something valuable or interesting? Tell the story. Describe what you found, what you did with it, and how you felt about your decision.\n\n"
        "Remember to use past tense in your writing.\n\n"
        "<b>A student's sample answer:</b>\n"
        "<blockquote>When I was 10, I find an old coin in my backyard. I kept it for a while and shows it to my friends. They was impressed and say it might be valuable. Later, I take it to a local antique shop, and the owner told me it was very old. I decided to give it to the museum in my town. The museum was happy and put it on display. I feel proud of my decision.<br><br><i>Copy and paste to try.</i></blockquote>"
    )
)

# Launch the interface
interface.launch()