taha092 commited on
Commit
3f14bc0
Β·
verified Β·
1 Parent(s): 8930b66

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -38
app.py CHANGED
@@ -1,56 +1,57 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
  from sentence_transformers import SentenceTransformer, util
4
- import random
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 modifier prompts
20
  tone_prompts = {
21
- "Academic": "Rewrite this in a scholarly and formal way:",
22
- "Casual": "Rewrite this casually like a real student would talk:",
23
- "Friendly": "Make this sound friendly and human-like:",
24
- "Stealth (AI Detection Bypass)": "Paraphrase to avoid sounding like AI-generated content:"
25
  }
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  def humanize_text(input_text, tone):
28
  if not input_text.strip():
29
  return "Please enter some text.", "", ""
30
 
31
- style_prompt = tone_prompts.get(tone, "Paraphrase:")
32
- result = paraphraser(f"{style_prompt} {input_text}", max_length=80, num_return_sequences=1)
33
- output_text = generate_paraphrase(input_text)
34
-
35
 
36
- # Semantic similarity score
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
- # Gradio Interface
46
- with gr.Blocks(theme=gr.themes.Base(primary_hue="blue", secondary_hue="blue")) as demo:
47
- gr.Markdown("""
48
- <h1 style='text-align: center; color: #0f172a;'>🧠 Taha's AI Humanizer Tool</h1>
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="πŸ”€ Enter Your AI-Sounding Text")
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
- btn = gr.Button("πŸš€ Humanize It")
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()