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"{word}" 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)