|
|
import os |
|
|
import json |
|
|
import gradio as gr |
|
|
import spaces |
|
|
import torch |
|
|
import random |
|
|
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForSequenceClassification |
|
|
from sentence_splitter import SentenceSplitter |
|
|
from itertools import product |
|
|
|
|
|
hf_token = os.getenv('HF_TOKEN') |
|
|
cuda_available = torch.cuda.is_available() |
|
|
device = torch.device("cuda" if cuda_available else "cpu") |
|
|
print(f"Using device: {device}") |
|
|
|
|
|
paraphraser_model_name = "facebook/bart-large-cnn" |
|
|
paraphraser_tokenizer = AutoTokenizer.from_pretrained(paraphraser_model_name) |
|
|
paraphraser_model = AutoModelForSeq2SeqLM.from_pretrained(paraphraser_model_name).to(device) |
|
|
|
|
|
classifier_model_name = "andreas122001/roberta-mixed-detector" |
|
|
classifier_tokenizer = AutoTokenizer.from_pretrained(classifier_model_name) |
|
|
classifier_model = AutoModelForSequenceClassification.from_pretrained(classifier_model_name).to(device) |
|
|
|
|
|
splitter = SentenceSplitter(language='en') |
|
|
|
|
|
def classify_text(text): |
|
|
inputs = classifier_tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device) |
|
|
with torch.no_grad(): |
|
|
outputs = classifier_model(**inputs) |
|
|
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1) |
|
|
predicted_class = torch.argmax(probabilities, dim=-1).item() |
|
|
main_label = classifier_model.config.id2label[predicted_class] |
|
|
main_score = probabilities[0][predicted_class].item() |
|
|
return main_label, main_score |
|
|
|
|
|
def introduce_errors(text): |
|
|
words = text.split() |
|
|
if len(words) > 3: |
|
|
i = random.randint(0, len(words) - 1) |
|
|
words[i] = words[i].lower() if words[i][0].isupper() else words[i].capitalize() |
|
|
return ' '.join(words) |
|
|
|
|
|
@spaces.GPU |
|
|
def generate_paraphrases(text, setting, output_format): |
|
|
sentences = splitter.split(text) |
|
|
all_sentence_paraphrases = [] |
|
|
|
|
|
if setting == 1: temperature, top_p, top_k = 0.7, 0.9, 50 |
|
|
elif setting == 2: temperature, top_p, top_k = 0.8, 0.85, 40 |
|
|
elif setting == 3: temperature, top_p, top_k = 0.9, 0.8, 30 |
|
|
elif setting == 4: temperature, top_p, top_k = 1.0, 0.75, 20 |
|
|
else: temperature, top_p, top_k = 1.1, 0.7, 10 |
|
|
|
|
|
num_return_sequences = 5 |
|
|
max_length = 128 |
|
|
|
|
|
formatted_output = f"Original text:\n{text}\n\nParaphrased versions:\n" |
|
|
json_output = {"original_text": text, "paraphrased_versions": [], "combined_versions": [], "human_like_versions": []} |
|
|
|
|
|
for i, sentence in enumerate(sentences): |
|
|
inputs = paraphraser_tokenizer(sentence, return_tensors="pt", max_length=max_length, truncation=True).to(device) |
|
|
|
|
|
outputs = paraphraser_model.generate( |
|
|
**inputs, |
|
|
do_sample=True, |
|
|
max_length=max_length, |
|
|
top_p=top_p, |
|
|
top_k=top_k, |
|
|
temperature=temperature, |
|
|
num_return_sequences=num_return_sequences, |
|
|
repetition_penalty=1.2, |
|
|
no_repeat_ngram_size=2 |
|
|
) |
|
|
|
|
|
paraphrases = paraphraser_tokenizer.batch_decode(outputs, skip_special_tokens=True) |
|
|
paraphrases = [introduce_errors(p) for p in paraphrases] |
|
|
|
|
|
formatted_output += f"Original sentence {i+1}: {sentence}\n" |
|
|
for j, paraphrase in enumerate(paraphrases, 1): |
|
|
formatted_output += f" Paraphrase {j}: {paraphrase}\n" |
|
|
|
|
|
json_output["paraphrased_versions"].append({f"original_sentence_{i+1}": sentence, "paraphrases": paraphrases}) |
|
|
all_sentence_paraphrases.append(paraphrases) |
|
|
formatted_output += "\n" |
|
|
|
|
|
all_combinations = list(product(*all_sentence_paraphrases)) |
|
|
random.shuffle(all_combinations) |
|
|
|
|
|
formatted_output += "\nCombined paraphrased versions:\n" |
|
|
combined_versions = [] |
|
|
for i, combination in enumerate(all_combinations[:50], 1): |
|
|
combined_paraphrase = " ".join(combination) |
|
|
combined_versions.append(combined_paraphrase) |
|
|
|
|
|
json_output["combined_versions"] = combined_versions |
|
|
|
|
|
human_versions = [] |
|
|
for i, version in enumerate(combined_versions, 1): |
|
|
label, score = classify_text(version) |
|
|
formatted_output += f"Version {i}:\n{version}\n" |
|
|
formatted_output += f"Classification: {label} (confidence: {score:.2%})\n\n" |
|
|
if label == "human-produced" or (label == "machine-generated" and score < 0.9): |
|
|
human_versions.append((version, label, score)) |
|
|
|
|
|
formatted_output += "\nHuman-like or Less Confident Machine-generated versions:\n" |
|
|
for i, (version, label, score) in enumerate(human_versions, 1): |
|
|
formatted_output += f"Version {i}:\n{version}\n" |
|
|
formatted_output += f"Classification: {label} (confidence: {score:.2%})\n\n" |
|
|
|
|
|
json_output["human_like_versions"] = [{"version": v, "label": l, "confidence_score": s} for v, l, s in human_versions] |
|
|
|
|
|
if not human_versions: |
|
|
human_versions = sorted([(v, l, s) for v, l, s in zip(combined_versions, [classify_text(v)[0] for v in combined_versions], [classify_text(v)[1] for v in combined_versions])], key=lambda x: x[2])[:5] |
|
|
formatted_output += "\nNo human-like versions found. Showing top 5 least confident machine-generated versions:\n" |
|
|
for i, (version, label, score) in enumerate(human_versions, 1): |
|
|
formatted_output += f"Version {i}:\n{version}\n" |
|
|
formatted_output += f"Classification: {label} (confidence: {score:.2%})\n\n" |
|
|
|
|
|
return (formatted_output, "\n\n".join([v[0] for v in human_versions])) if output_format == "text" else (json.dumps(json_output, indent=2), "\n\n".join([v[0] for v in human_versions])) |
|
|
|
|
|
iface = gr.Interface( |
|
|
fn=generate_paraphrases, |
|
|
inputs=[ |
|
|
gr.Textbox(lines=5, label="Input Text"), |
|
|
gr.Slider(minimum=1, maximum=5, step=1, label="Diversity Setting"), |
|
|
gr.Radio(["text", "json"], label="Output Format") |
|
|
], |
|
|
outputs=[ |
|
|
gr.Textbox(lines=20, label="Detailed Paraphrases and Classifications"), |
|
|
gr.Textbox(lines=10, label="Human-like or Less Confident Machine-generated Paraphrases") |
|
|
], |
|
|
title="Advanced Diverse Paraphraser with Human-like Filter", |
|
|
description="Enter a text, select a diversity setting, and choose the output format to generate diverse paraphrased versions. Combined versions are classified, and those detected as human-produced or less confidently machine-generated are presented in the final output." |
|
|
) |
|
|
|
|
|
iface.launch() |