Spaces:
Sleeping
Sleeping
File size: 3,203 Bytes
0f2a23a e4351cc 0f2a23a db75140 0f2a23a bf4908b 7c41997 e4351cc 0f2a23a e4351cc f18ad55 bf4908b 7c41997 0f2a23a bf4908b 0f2a23a 486bbd6 f18ad55 486bbd6 7c41997 486bbd6 f18ad55 486bbd6 f18ad55 486bbd6 f18ad55 486bbd6 f18ad55 486bbd6 7c41997 486bbd6 f18ad55 486bbd6 f18ad55 486bbd6 f18ad55 0f2a23a f18ad55 a1d4c88 7c41997 f18ad55 7c41997 f18ad55 a1d4c88 7c41997 bf4908b a1d4c88 7c41997 f18ad55 bf4908b 0f2a23a 7c41997 |
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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()
|