Spaces:
Sleeping
Sleeping
| # Import dependencies | |
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForSequenceClassification, T5Tokenizer, T5ForConditionalGeneration | |
| import torch | |
| import nltk | |
| # Download NLTK data (if not already downloaded) | |
| nltk.download('punkt') | |
| nltk.download('stopwords') | |
| # Check for GPU and set the device accordingly | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| # Load AI Detector model and tokenizer from Hugging Face (DistilBERT) | |
| tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english") | |
| model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english").to(device) | |
| # Load SRDdev Paraphrase model and tokenizer for humanizing text | |
| paraphrase_tokenizer = T5Tokenizer.from_pretrained("SRDdev/Paraphrase") | |
| paraphrase_model = T5ForConditionalGeneration.from_pretrained("SRDdev/Paraphrase").to(device) | |
| # AI detection function using DistilBERT | |
| def detect_ai_generated(text): | |
| inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device) | |
| with torch.no_grad(): | |
| outputs = model(**inputs) | |
| probabilities = torch.softmax(outputs.logits, dim=1) | |
| ai_probability = probabilities[0][1].item() # Probability of being AI-generated | |
| return ai_probability | |
| # Humanize the AI-detected text using the SRDdev Paraphrase model | |
| def humanize_text(AI_text): | |
| paragraphs = AI_text.split("\n") | |
| paraphrased_paragraphs = [] | |
| for paragraph in paragraphs: | |
| if paragraph.strip(): | |
| inputs = paraphrase_tokenizer(paragraph, return_tensors="pt", max_length=512, truncation=True).to(device) | |
| paraphrased_ids = paraphrase_model.generate( | |
| inputs['input_ids'], | |
| max_length=inputs['input_ids'].shape[-1] + 20, # Slightly more than the original input length | |
| num_beams=4, | |
| early_stopping=True, | |
| length_penalty=1.0, | |
| no_repeat_ngram_size=3, | |
| ) | |
| paraphrased_text = paraphrase_tokenizer.decode(paraphrased_ids[0], skip_special_tokens=True) | |
| paraphrased_paragraphs.append(paraphrased_text) | |
| return "\n\n".join(paraphrased_paragraphs) | |
| # Main function to handle the overall process | |
| def main_function(AI_text): | |
| ai_probability = detect_ai_generated(AI_text) | |
| # Humanize AI text | |
| humanized_text = humanize_text(AI_text) | |
| return f"AI-Generated Content: {ai_probability:.2f}%\n\nHumanized Text:\n{humanized_text}" | |
| # Gradio interface definition | |
| interface = gr.Interface( | |
| fn=main_function, | |
| inputs="textbox", | |
| outputs="textbox", | |
| title="AI Text Humanizer", | |
| description="Enter AI-generated text and get a human-written version. This space uses models from Hugging Face directly." | |
| ) | |
| # Launch the Gradio app | |
| interface.launch(debug=True) | |