import gradio as gr
from gramformer import Gramformer
from textblob import TextBlob
from language_tool_python import LanguageTool
from langdetect import detect
import warnings
import difflib
import re
# Disable future warnings
warnings.filterwarnings("ignore", category=FutureWarning)
# Initialize Gramformer for grammar correction
gf = Gramformer(models=1, use_gpu=True)
tool = LanguageTool('en-US') # Initialize LanguageTool for additional grammar checks
# Function to correct grammar, highlight mistakes, and provide suggestions
def correct_and_suggest(essay):
corrected_sentences = []
highlighted_essay = []
mistakes_count = 0
suggestions = [] # To store suggestions for spelling, grammar, and tense
# Split essay into sentences
#sentences = essay.split('. ')
sentences = re.split(r'(?{word[2:]}")
mistakes_count += 1
else:
highlighted_sentence.append(word[2:])
elif word.startswith('+ '): # Indicates an added word
highlighted_sentence.append(f"{word[2:]}")
elif not word.startswith('? '): # Ignore metadata lines
highlighted_sentence.append(word[2:])
# Additional suggestions using TextBlob and LanguageTool
blob = TextBlob(sentence)
tool_matches = tool.check(sentence)
tense_suggestions = []
grammar_suggestions = []
spelling_suggestions = []
if len(tool_matches) > 0:
grammar_suggestions.append(f"Grammar issues in: '{sentence}'")
# Detect language using langdetect
if detect(sentence) != 'en':
tense_suggestions.append(f"Possible tense issue in: '{sentence}'")
# Append all suggestions
suggestions.extend(spelling_suggestions + grammar_suggestions + tense_suggestions)
# Append the results to the respective lists
highlighted_essay.append(' '.join(highlighted_sentence))
corrected_sentences.append(corrected_sentence)
# Simple score formula based on mistakes
total_words = len(re.findall(r'\w+', essay)) # Total number of words
score = max((100-mistakes_count), 0)
# Video suggestions
videos = [
"Essay Tips Video 1",
"Essay Tips Video 2",
"Essay Tips Video 3",
"Essay Tips Video 4"
]
return ' '.join(highlighted_essay), ' '.join(corrected_sentences), f"Score: {round(score, 2)}", \
'\n'.join(suggestions), '
'.join(videos), f"Word count: {total_words}"
# 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"), gr.Textbox(label="Suggestions"), gr.HTML(label="Helpful Videos"),
gr.Textbox(label="Word Count", interactive=False)]
# Create Gradio interface
interface = gr.Interface(
fn=correct_and_suggest,
inputs=app_inputs,
outputs=app_outputs,
title="Essay Checker"
)
# Launch the Gradio app with a public link
interface.launch(share=True)