Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,73 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
model_name = "
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
def humanize_text(ai_text):
|
| 17 |
-
if not ai_text.strip():
|
| 18 |
-
return "⚠️ Please enter some text."
|
| 19 |
-
|
| 20 |
-
# Prompt for rewriting
|
| 21 |
-
prompt = (
|
| 22 |
-
"Rewrite the following AI-generated text so it sounds natural and human-like. "
|
| 23 |
-
"Avoid robotic tone, vary sentence length, and make it engaging:\n\n"
|
| 24 |
-
f"Text: {ai_text}\n\n"
|
| 25 |
-
"Humanized version:"
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
| 29 |
-
outputs = model.generate(
|
| 30 |
-
**inputs,
|
| 31 |
-
max_new_tokens=400,
|
| 32 |
-
temperature=1.0,
|
| 33 |
-
top_p=0.95
|
| 34 |
-
)
|
| 35 |
-
humanized = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 36 |
-
|
| 37 |
-
return humanized
|
| 38 |
-
|
| 39 |
-
# Gradio UI
|
| 40 |
-
with gr.Blocks() as demo:
|
| 41 |
-
gr.Markdown("## ✨ AI Text Humanizer")
|
| 42 |
-
gr.Markdown("Paste AI-generated text below, and get a natural, human-like rewrite.")
|
| 43 |
-
|
| 44 |
-
with gr.Row():
|
| 45 |
-
ai_input = gr.Textbox(label="AI-Generated Text", placeholder="Paste your AI text here...", lines=10)
|
| 46 |
-
|
| 47 |
-
with gr.Row():
|
| 48 |
-
humanized_output = gr.Textbox(label="✅ Humanized Text", lines=10)
|
| 49 |
-
|
| 50 |
-
run_button = gr.Button("🔄 Humanize Now")
|
| 51 |
-
|
| 52 |
-
run_button.click(
|
| 53 |
-
fn=humanize_text,
|
| 54 |
-
inputs=ai_input,
|
| 55 |
-
outputs=humanized_output
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
demo.launch()
|
|
|
|
| 1 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
import torch, gradio as gr
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
+
# --- Load Model (Option 1: FLAN-T5-Paraphraser) ---
|
| 6 |
+
model_name = "alykassem/FLAN-T5-Paraphraser"
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
model = model.to(device)
|
| 12 |
+
model.eval()
|
| 13 |
+
|
| 14 |
+
# --- Helpers ---
|
| 15 |
+
def split_sentences(text):
|
| 16 |
+
sentences = re.split(r'(?<=[.!?])\s+', text.strip())
|
| 17 |
+
return [s for s in sentences if s]
|
| 18 |
+
|
| 19 |
+
def clean_sentence(sent):
|
| 20 |
+
sent = re.sub(r'\s+', ' ', sent).strip()
|
| 21 |
+
if not sent.endswith(('.', '!', '?')):
|
| 22 |
+
sent += "."
|
| 23 |
+
return sent
|
| 24 |
+
|
| 25 |
+
# --- Main function ---
|
| 26 |
+
def paraphrase_fn(text, num_return_sequences=1, temperature=1.0, top_p=0.9):
|
| 27 |
+
if not text.strip():
|
| 28 |
+
return "⚠️ Please enter some text"
|
| 29 |
+
|
| 30 |
+
num_return_sequences = int(num_return_sequences)
|
| 31 |
+
sentences = split_sentences(text)
|
| 32 |
+
all_outputs = []
|
| 33 |
+
|
| 34 |
+
for sent in sentences:
|
| 35 |
+
input_text = "paraphrase: " + sent + " </s>"
|
| 36 |
+
inputs = tokenizer([input_text], return_tensors="pt", truncation=True, padding=True).to(device)
|
| 37 |
+
|
| 38 |
+
outputs = model.generate(
|
| 39 |
+
**inputs,
|
| 40 |
+
max_new_tokens=128,
|
| 41 |
+
num_return_sequences=num_return_sequences,
|
| 42 |
+
do_sample=True,
|
| 43 |
+
top_p=float(top_p),
|
| 44 |
+
temperature=float(temperature),
|
| 45 |
+
)
|
| 46 |
+
decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 47 |
+
|
| 48 |
+
seen, unique = set(), []
|
| 49 |
+
for d in decoded:
|
| 50 |
+
d = clean_sentence(d)
|
| 51 |
+
if d not in seen:
|
| 52 |
+
unique.append(d)
|
| 53 |
+
seen.add(d)
|
| 54 |
+
|
| 55 |
+
all_outputs.append(unique[0])
|
| 56 |
+
|
| 57 |
+
return " ".join(all_outputs).strip()
|
| 58 |
+
|
| 59 |
+
# --- Gradio Interface ---
|
| 60 |
+
iface = gr.Interface(
|
| 61 |
+
fn=paraphrase_fn,
|
| 62 |
+
inputs=[
|
| 63 |
+
gr.Textbox(lines=8, placeholder="Paste text here..."),
|
| 64 |
+
gr.Slider(1, 3, step=1, value=1, label="Variants"),
|
| 65 |
+
gr.Slider(0.5, 2.0, step=0.1, value=1.0, label="Temperature"),
|
| 66 |
+
gr.Slider(0.6, 1.0, step=0.01, value=0.9, label="Top-p"),
|
| 67 |
+
],
|
| 68 |
+
outputs=gr.Textbox(label="Output"),
|
| 69 |
+
title="📝 Writenix Paraphraser (FLAN-T5)",
|
| 70 |
+
description="Paraphrasing powered by FLAN-T5, fine-tuned on high-quality datasets."
|
| 71 |
)
|
| 72 |
|
| 73 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|