Spaces:
Build error
Build error
Shujaat Ali
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,11 +3,16 @@ import gradio as gr
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, T5Tokenizer, T5ForConditionalGeneration
|
4 |
import torch
|
5 |
import nltk
|
|
|
|
|
6 |
|
7 |
# Download NLTK data (if not already downloaded)
|
8 |
nltk.download('punkt')
|
9 |
nltk.download('stopwords')
|
10 |
|
|
|
|
|
|
|
11 |
# Check for GPU and set the device accordingly
|
12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
|
@@ -19,6 +24,29 @@ model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-unca
|
|
19 |
paraphrase_tokenizer = T5Tokenizer.from_pretrained("SRDdev/Paraphrase")
|
20 |
paraphrase_model = T5ForConditionalGeneration.from_pretrained("SRDdev/Paraphrase").to(device)
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
# AI detection function using DistilBERT
|
23 |
def detect_ai_generated(text):
|
24 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
@@ -49,10 +77,14 @@ def humanize_text(AI_text):
|
|
49 |
|
50 |
# Main function to handle the overall process
|
51 |
def main_function(AI_text):
|
52 |
-
|
|
|
|
|
|
|
|
|
53 |
|
54 |
# Humanize AI text
|
55 |
-
humanized_text = humanize_text(
|
56 |
|
57 |
return f"AI-Generated Content: {ai_probability:.2f}%\n\nHumanized Text:\n{humanized_text}"
|
58 |
|
@@ -61,8 +93,8 @@ interface = gr.Interface(
|
|
61 |
fn=main_function,
|
62 |
inputs="textbox",
|
63 |
outputs="textbox",
|
64 |
-
title="AI Text Humanizer",
|
65 |
-
description="Enter AI-generated text and get a human-written version. This space uses models from Hugging Face directly."
|
66 |
)
|
67 |
|
68 |
# Launch the Gradio app
|
|
|
3 |
from transformers import AutoTokenizer, AutoModelForSequenceClassification, T5Tokenizer, T5ForConditionalGeneration
|
4 |
import torch
|
5 |
import nltk
|
6 |
+
import spacy
|
7 |
+
from nltk.corpus import wordnet
|
8 |
|
9 |
# Download NLTK data (if not already downloaded)
|
10 |
nltk.download('punkt')
|
11 |
nltk.download('stopwords')
|
12 |
|
13 |
+
# Load spaCy model for English
|
14 |
+
nlp = spacy.load("en_core_web_sm")
|
15 |
+
|
16 |
# Check for GPU and set the device accordingly
|
17 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
|
|
|
24 |
paraphrase_tokenizer = T5Tokenizer.from_pretrained("SRDdev/Paraphrase")
|
25 |
paraphrase_model = T5ForConditionalGeneration.from_pretrained("SRDdev/Paraphrase").to(device)
|
26 |
|
27 |
+
# Function to find synonyms using WordNet via NLTK
|
28 |
+
def get_synonyms(word):
|
29 |
+
synonyms = set()
|
30 |
+
for syn in wordnet.synsets(word):
|
31 |
+
for lemma in syn.lemmas():
|
32 |
+
synonyms.add(lemma.name())
|
33 |
+
return list(synonyms)
|
34 |
+
|
35 |
+
# Replace words with synonyms using spaCy and WordNet
|
36 |
+
def replace_with_synonyms(text):
|
37 |
+
doc = nlp(text)
|
38 |
+
processed_text = []
|
39 |
+
for token in doc:
|
40 |
+
synonyms = get_synonyms(token.text.lower())
|
41 |
+
if synonyms and token.pos_ in {"NOUN", "VERB", "ADJ", "ADV"}: # Only replace certain types of words
|
42 |
+
replacement = synonyms[0] # Replace with the first synonym
|
43 |
+
if token.is_title:
|
44 |
+
replacement = replacement.capitalize()
|
45 |
+
processed_text.append(replacement)
|
46 |
+
else:
|
47 |
+
processed_text.append(token.text)
|
48 |
+
return " ".join(processed_text)
|
49 |
+
|
50 |
# AI detection function using DistilBERT
|
51 |
def detect_ai_generated(text):
|
52 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
|
|
|
77 |
|
78 |
# Main function to handle the overall process
|
79 |
def main_function(AI_text):
|
80 |
+
# Replace words with synonyms
|
81 |
+
text_with_synonyms = replace_with_synonyms(AI_text)
|
82 |
+
|
83 |
+
# Detect AI-generated content
|
84 |
+
ai_probability = detect_ai_generated(text_with_synonyms)
|
85 |
|
86 |
# Humanize AI text
|
87 |
+
humanized_text = humanize_text(text_with_synonyms)
|
88 |
|
89 |
return f"AI-Generated Content: {ai_probability:.2f}%\n\nHumanized Text:\n{humanized_text}"
|
90 |
|
|
|
93 |
fn=main_function,
|
94 |
inputs="textbox",
|
95 |
outputs="textbox",
|
96 |
+
title="AI Text Humanizer with Synonym Replacement",
|
97 |
+
description="Enter AI-generated text and get a human-written version, with synonyms replaced for more natural output. This space uses models from Hugging Face directly."
|
98 |
)
|
99 |
|
100 |
# Launch the Gradio app
|