File size: 2,000 Bytes
72872ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gramformer import Gramformer
import warnings
import difflib

warnings.filterwarnings("ignore", category=FutureWarning)

# Initialize Gramformer model
gf = Gramformer(models=1, use_gpu=True)


# Function to correct grammar and highlight mistakes
def correct_and_highlight(essay):
    corrected_sentences = []
    highlighted_essay = []
    mistakes_count = 0

    # Split essay into sentences
    sentences = essay.split('. ')

    # Correct each sentence
    for sentence in sentences:
        correction_set = gf.correct(sentence)
        corrected_sentence = list(correction_set)[0] if correction_set else sentence

        # Highlight changes using difflib
        diff = difflib.ndiff(sentence.split(), corrected_sentence.split())
        highlighted_sentence = ' '.join(
            [f"<span style='color:red'>{word}</span>" if word.startswith('-') else word for word in diff if
             not word.startswith('?')]
        )

        # Count the mistakes
        mistakes_count += sum(1 for word in diff if word.startswith('-'))

        highlighted_essay.append(highlighted_sentence)
        corrected_sentences.append(corrected_sentence)

    # Score the essay (based on mistake count)
    total_words = len(essay.split())
    score = max(100 - (mistakes_count / total_words * 100), 0)  # Simple score formula

    return ' '.join(highlighted_essay), ' '.join(corrected_sentences), f"Score: {round(score, 2)}"


# Define Gradio inputs and outputs
app_inputs = gr.Textbox(lines=10, placeholder="Enter your full essay here...", label="Essay")
app_outputs = [gr.HTML(label="Highlighted Mistakes"), gr.Textbox(label="Corrected Essay"), gr.Textbox(label="Score")]

# Create the Gradio interface
interface = gr.Interface(
    fn=correct_and_highlight,
    inputs=app_inputs,
    outputs=app_outputs,
    title="Advanced Essay Checker"
)

# Launch the Gradio app with a public link
interface.launch(share=True)