sashtech's picture
Update app.py
f18ad55 verified
raw
history blame
3.2 kB
import os
import gradio as gr
from transformers import pipeline
import spacy
import subprocess
import nltk
from nltk.corpus import wordnet
from spellchecker import SpellChecker
import re
from inflect import engine # For pluralization
# Initialize the English text classification pipeline for AI detection
pipeline_en = pipeline(task="text-classification", model="Hello-SimpleAI/chatgpt-detector-roberta")
# Initialize the spell checker and inflect engine
spell = SpellChecker()
inflect_engine = engine()
# Ensure necessary NLTK data is downloaded
nltk.download('wordnet')
nltk.download('omw-1.4')
# Ensure the SpaCy model is installed
try:
nlp = spacy.load("en_core_web_sm")
except OSError:
subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
nlp = spacy.load("en_core_web_sm")
# Function to predict AI detection
def predict_en(text):
res = pipeline_en(text)[0]
return res['label'], res['score']
# Function to get synonyms using NLTK WordNet
def get_synonyms_nltk(word, pos):
synsets = wordnet.synsets(word, pos=pos)
if synsets:
lemmas = synsets[0].lemmas()
return [lemma.name() for lemma in lemmas if lemma.name() != word] # Avoid original word
return []
# Function to remove redundant words
def remove_redundant_words(text):
meaningless_words = {"actually", "basically", "literally", "really", "very", "just"}
return ' '.join(word for word in text.split() if word.lower() not in meaningless_words)
# Function to correct spelling errors
def correct_spelling(text):
words = text.split()
corrected_words = [spell.correction(word) for word in words]
return ' '.join(corrected_words)
# Function to rephrase text with synonyms
def rephrase_with_synonyms(text):
doc = nlp(text)
rephrased_text = []
for token in doc:
pos_tag = {
"NOUN": wordnet.NOUN,
"VERB": wordnet.VERB,
"ADJ": wordnet.ADJ,
"ADV": wordnet.ADV
}.get(token.pos_, None)
if pos_tag:
synonyms = get_synonyms_nltk(token.lemma_, pos_tag)
synonym = synonyms[0] if synonyms else token.text
rephrased_text.append(synonym)
else:
rephrased_text.append(token.text)
return ' '.join(rephrased_text)
# Function to paraphrase and correct grammar
def paraphrase_and_correct(text):
cleaned_text = remove_redundant_words(text)
cleaned_text = correct_spelling(cleaned_text)
return rephrase_with_synonyms(cleaned_text)
# Function to handle user input
def process_text(input_text):
ai_label, ai_score = predict_en(input_text)
if ai_label == "HUMAN":
corrected_text = paraphrase_and_correct(input_text)
return corrected_text
else:
return "The text seems to be AI-generated; no correction applied."
# Gradio interface
iface = gr.Interface(
fn=process_text,
inputs=gr.Textbox(lines=10, placeholder="Enter your text here..."),
outputs=gr.Textbox(label="Corrected Text"),
title="Text Correction and Rephrasing",
description="This app corrects and rephrases text while detecting AI-generated content."
)
# Launch the interface
iface.launch()