Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,12 @@
|
|
1 |
# app.py
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
# Load a pre-trained T5 model specifically fine-tuned for grammar correction
|
6 |
tokenizer = T5Tokenizer.from_pretrained("prithivida/grammar_error_correcter_v1")
|
@@ -8,28 +14,53 @@ model = T5ForConditionalGeneration.from_pretrained("prithivida/grammar_error_cor
|
|
8 |
|
9 |
# Function to perform grammar correction
|
10 |
def grammar_check(text):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
return corrected_text
|
16 |
|
17 |
# Create Gradio interface with a writing prompt
|
18 |
interface = gr.Interface(
|
19 |
fn=grammar_check,
|
20 |
inputs="text",
|
21 |
-
outputs="
|
22 |
title="Grammar Checker",
|
23 |
description=(
|
24 |
"Enter text to check for grammar mistakes.\n\n"
|
25 |
"Writing Prompt:\n"
|
26 |
-
"In the story, Alex and his friends decided to donate
|
27 |
-
"
|
28 |
-
"
|
29 |
-
"
|
30 |
-
"Explain your reasons with some relevant examples from your own experience and knowledge. "
|
31 |
)
|
32 |
)
|
33 |
|
34 |
# Launch the interface
|
35 |
-
interface.launch()
|
|
|
1 |
# app.py
|
2 |
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
3 |
import gradio as gr
|
4 |
+
import nltk
|
5 |
+
from nltk.tokenize import sent_tokenize
|
6 |
+
import difflib
|
7 |
+
|
8 |
+
# Download the punkt tokenizer for sentence splitting
|
9 |
+
nltk.download('punkt')
|
10 |
|
11 |
# Load a pre-trained T5 model specifically fine-tuned for grammar correction
|
12 |
tokenizer = T5Tokenizer.from_pretrained("prithivida/grammar_error_correcter_v1")
|
|
|
14 |
|
15 |
# Function to perform grammar correction
|
16 |
def grammar_check(text):
|
17 |
+
# Split the text into sentences
|
18 |
+
sentences = sent_tokenize(text)
|
19 |
+
corrected_sentences = []
|
20 |
+
original_sentences = []
|
21 |
+
|
22 |
+
for sentence in sentences:
|
23 |
+
original_sentences.append(sentence)
|
24 |
+
input_text = f"gec: {sentence}"
|
25 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=512, truncation=True)
|
26 |
+
outputs = model.generate(input_ids, max_length=512, num_beams=4, early_stopping=True)
|
27 |
+
corrected_sentence = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
28 |
+
corrected_sentences.append(corrected_sentence)
|
29 |
+
|
30 |
+
# Function to underline and color revised parts
|
31 |
+
def underline_and_color_revisions(original, corrected):
|
32 |
+
diff = difflib.ndiff(original.split(), corrected.split())
|
33 |
+
result = []
|
34 |
+
for word in diff:
|
35 |
+
if word.startswith("+ "):
|
36 |
+
result.append(f"<u style='color:red;'>{word[2:]}</u>")
|
37 |
+
elif word.startswith("- "):
|
38 |
+
continue
|
39 |
+
else:
|
40 |
+
result.append(word[2:])
|
41 |
+
return " ".join(result)
|
42 |
+
|
43 |
+
# Join the corrected sentences back into a single string
|
44 |
+
corrected_text = " ".join(
|
45 |
+
underline_and_color_revisions(orig, corr) for orig, corr in zip(original_sentences, corrected_sentences)
|
46 |
+
)
|
47 |
return corrected_text
|
48 |
|
49 |
# Create Gradio interface with a writing prompt
|
50 |
interface = gr.Interface(
|
51 |
fn=grammar_check,
|
52 |
inputs="text",
|
53 |
+
outputs="html", # Change output type to HTML
|
54 |
title="Grammar Checker",
|
55 |
description=(
|
56 |
"Enter text to check for grammar mistakes.\n\n"
|
57 |
"Writing Prompt:\n"
|
58 |
+
"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"
|
59 |
+
"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"
|
60 |
+
"Remember to use past tense in your writing.\n"
|
61 |
+
"Sample text for testing: 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."
|
|
|
62 |
)
|
63 |
)
|
64 |
|
65 |
# Launch the interface
|
66 |
+
interface.launch()
|