Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
-
import
|
| 5 |
|
| 6 |
# Load models
|
| 7 |
-
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 8 |
-
|
| 9 |
tokenizer = T5Tokenizer.from_pretrained("Vamsi/T5_Paraphrase_Paws", use_fast=False)
|
| 10 |
model = T5ForConditionalGeneration.from_pretrained("Vamsi/T5_Paraphrase_Paws")
|
| 11 |
-
|
| 12 |
-
def generate_paraphrase(text):
|
| 13 |
-
input_ids = tokenizer.encode("paraphrase: " + text, return_tensors="pt", max_length=256, truncation=True)
|
| 14 |
-
output_ids = model.generate(input_ids, max_length=80, num_return_sequences=1, do_sample=True, top_k=120, top_p=0.98)
|
| 15 |
-
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 16 |
-
|
| 17 |
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 18 |
|
| 19 |
-
# Tone
|
| 20 |
tone_prompts = {
|
| 21 |
-
"Academic": "Rewrite this in a
|
| 22 |
-
"Casual": "Rewrite this
|
| 23 |
-
"Friendly": "Make this sound friendly
|
| 24 |
-
"Stealth (AI Detection Bypass)": "
|
| 25 |
}
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
def humanize_text(input_text, tone):
|
| 28 |
if not input_text.strip():
|
| 29 |
return "Please enter some text.", "", ""
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
output_text = generate_paraphrase(input_text)
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
| 37 |
emb1 = similarity_model.encode(input_text, convert_to_tensor=True)
|
| 38 |
emb2 = similarity_model.encode(output_text, convert_to_tensor=True)
|
| 39 |
similarity_score = util.pytorch_cos_sim(emb1, emb2).item()
|
| 40 |
-
|
| 41 |
-
score_description = "βοΈ Humanized (Safe)" if similarity_score < 0.92 else "β οΈ Still Close to AI Tone"
|
| 42 |
|
| 43 |
return output_text, f"{similarity_score:.2f}", score_description
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
with gr.Blocks(theme=gr.themes.
|
| 47 |
-
gr.Markdown(""
|
| 48 |
-
|
| 49 |
-
<p style='text-align: center; color: #334155;'>Rewriting AI-generated text to sound real, authentic, and undetectable β made by Taha.</p>
|
| 50 |
-
""")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
-
input_text = gr.Textbox(lines=6, label="
|
| 54 |
output_text = gr.Textbox(lines=6, label="β
Humanized Output")
|
| 55 |
|
| 56 |
tone = gr.Radio(["Academic", "Casual", "Friendly", "Stealth (AI Detection Bypass)"], label="π― Select Tone", value="Stealth (AI Detection Bypass)")
|
|
@@ -59,13 +60,6 @@ with gr.Blocks(theme=gr.themes.Base(primary_hue="blue", secondary_hue="blue")) a
|
|
| 59 |
similarity = gr.Textbox(label="π Semantic Similarity Score")
|
| 60 |
score_label = gr.Textbox(label="π§ Humanization Check")
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
btn.click(fn=humanize_text, inputs=[input_text, tone], outputs=[output_text, similarity, score_label])
|
| 65 |
-
|
| 66 |
-
gr.Markdown("""
|
| 67 |
-
<hr>
|
| 68 |
-
<p style='text-align: center; font-size: 14px; color: #64748b;'>β¨ Created with love by <strong>Taha</strong> β helping students stay original.</p>
|
| 69 |
-
""")
|
| 70 |
|
| 71 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 3 |
from sentence_transformers import SentenceTransformer, util
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
# Load models
|
|
|
|
|
|
|
| 7 |
tokenizer = T5Tokenizer.from_pretrained("Vamsi/T5_Paraphrase_Paws", use_fast=False)
|
| 8 |
model = T5ForConditionalGeneration.from_pretrained("Vamsi/T5_Paraphrase_Paws")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 10 |
|
| 11 |
+
# Tone prompt variations
|
| 12 |
tone_prompts = {
|
| 13 |
+
"Academic": "Rewrite this in a formal and academic way:",
|
| 14 |
+
"Casual": "Rewrite this in a casual and relaxed way:",
|
| 15 |
+
"Friendly": "Make this sound like a friendly human wrote it:",
|
| 16 |
+
"Stealth (AI Detection Bypass)": "Reword this to avoid AI detection and sound natural:"
|
| 17 |
}
|
| 18 |
|
| 19 |
+
def generate_paraphrase(text, tone):
|
| 20 |
+
prompt = tone_prompts.get(tone, "Paraphrase:")
|
| 21 |
+
input_text = f"{prompt} {text.strip()}"
|
| 22 |
+
input_ids = tokenizer.encode(input_text, return_tensors="pt", max_length=256, truncation=True)
|
| 23 |
+
output_ids = model.generate(
|
| 24 |
+
input_ids,
|
| 25 |
+
max_length=80,
|
| 26 |
+
num_return_sequences=1,
|
| 27 |
+
do_sample=True,
|
| 28 |
+
top_k=120,
|
| 29 |
+
top_p=0.95
|
| 30 |
+
)
|
| 31 |
+
return tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 32 |
+
|
| 33 |
def humanize_text(input_text, tone):
|
| 34 |
if not input_text.strip():
|
| 35 |
return "Please enter some text.", "", ""
|
| 36 |
|
| 37 |
+
# Generate output
|
| 38 |
+
output_text = generate_paraphrase(input_text, tone)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# Compute semantic similarity
|
| 41 |
emb1 = similarity_model.encode(input_text, convert_to_tensor=True)
|
| 42 |
emb2 = similarity_model.encode(output_text, convert_to_tensor=True)
|
| 43 |
similarity_score = util.pytorch_cos_sim(emb1, emb2).item()
|
| 44 |
+
score_description = "β
Very Human-Like" if similarity_score < 0.9 else "β οΈ May Still Sound AI-Generated"
|
|
|
|
| 45 |
|
| 46 |
return output_text, f"{similarity_score:.2f}", score_description
|
| 47 |
|
| 48 |
+
# UI
|
| 49 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 50 |
+
gr.Markdown("## π§ Taha's AI Humanizer Tool")
|
| 51 |
+
gr.Markdown("*Rewriting AI-generated text to sound real, authentic, and undetectable β made by Taha.*")
|
|
|
|
|
|
|
| 52 |
|
| 53 |
with gr.Row():
|
| 54 |
+
input_text = gr.Textbox(lines=6, label="π Enter Your AI-Sounding Text")
|
| 55 |
output_text = gr.Textbox(lines=6, label="β
Humanized Output")
|
| 56 |
|
| 57 |
tone = gr.Radio(["Academic", "Casual", "Friendly", "Stealth (AI Detection Bypass)"], label="π― Select Tone", value="Stealth (AI Detection Bypass)")
|
|
|
|
| 60 |
similarity = gr.Textbox(label="π Semantic Similarity Score")
|
| 61 |
score_label = gr.Textbox(label="π§ Humanization Check")
|
| 62 |
|
| 63 |
+
gr.Button("π Humanize It").click(fn=humanize_text, inputs=[input_text, tone], outputs=[output_text, similarity, score_label])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
demo.launch()
|