taha092 commited on
Commit
23866f0
·
verified ·
1 Parent(s): 40f369c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -145
app.py CHANGED
@@ -1,146 +1,142 @@
1
- import gradio as gr
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
- from transformers.pipelines import pipeline
5
- from sentence_transformers import SentenceTransformer, util
6
- import numpy as np
7
- import gradio.themes as grthemes
8
-
9
- # Paraphrasing model: humarin/chatgpt_paraphraser_on_T5_base
10
- PARAPHRASE_MODEL_NAME = "humarin/chatgpt_paraphraser_on_T5_base"
11
- paraphrase_tokenizer = AutoTokenizer.from_pretrained(PARAPHRASE_MODEL_NAME)
12
- paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained(PARAPHRASE_MODEL_NAME)
13
- device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
- paraphrase_model = paraphrase_model.to(device)
15
-
16
- # AI Detector: roberta-base-openai-detector
17
- ai_detector = pipeline("text-classification", model="roberta-base-openai-detector", device=0 if torch.cuda.is_available() else -1)
18
-
19
- # Semantic similarity model
20
- similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
21
-
22
- tone_templates = {
23
- "Academic": "Paraphrase the following text in a formal, academic tone:",
24
- "Casual": "Paraphrase the following text in a casual, conversational tone:",
25
- "Friendly": "Paraphrase the following text in a friendly, approachable tone:",
26
- "Stealth": "Paraphrase the following text to bypass AI detectors and sound as human as possible:",
27
- }
28
-
29
- # Paraphrasing function
30
- def paraphrase(text, tone):
31
- prompt = tone_templates[tone] + " " + text
32
- input_ids = paraphrase_tokenizer(
33
- f'paraphrase: {prompt}',
34
- return_tensors="pt", padding="longest",
35
- max_length=256, truncation=True
36
- ).input_ids.to(device)
37
- outputs = paraphrase_model.generate(
38
- input_ids,
39
- temperature=0.7,
40
- repetition_penalty=1.2,
41
- num_return_sequences=1,
42
- no_repeat_ngram_size=2,
43
- max_length=256,
44
- diversity_penalty=3.0,
45
- num_beams=5,
46
- num_beam_groups=5
47
- )
48
- res = paraphrase_tokenizer.batch_decode(outputs, skip_special_tokens=True)
49
- return res[0] if res else ""
50
-
51
- def semantic_similarity(text1, text2):
52
- emb1 = similarity_model.encode(text1, convert_to_tensor=True)
53
- emb2 = similarity_model.encode(text2, convert_to_tensor=True)
54
- sim = util.pytorch_cos_sim(emb1, emb2).item()
55
- return sim
56
-
57
- def ai_detect(text):
58
- # Returns probability of being AI-generated (label 'Fake')
59
- result = ai_detector(text)
60
- for r in result:
61
- if r['label'] == 'Fake':
62
- return r['score']
63
- elif r['label'] == 'Real':
64
- return 1.0 - r['score']
65
- return 0.5 # fallback
66
-
67
- def humanization_score(sim, ai_prob):
68
- # Lower similarity and lower AI probability = more human
69
- score = (1.0 - sim) * 0.5 + (1.0 - ai_prob) * 0.5
70
- return score
71
-
72
- def humanization_rating(score):
73
- if score < 0.7:
74
- return f"⚠️ Still robotic ({score:.2f})"
75
- elif score < 0.85:
76
- return f"👍 Acceptable ({score:.2f})"
77
- else:
78
- return f"✅ Highly Human ({score:.2f})"
79
-
80
- def process(text, tone):
81
- if not text.strip():
82
- return "", 0.0, 0.0, 0.0, "", 0.0, ""
83
- # Pre-humanization AI detection
84
- pre_ai_prob = ai_detect(text)
85
- # Paraphrase
86
- paraphrased = paraphrase(text, tone)
87
- # Post-humanization AI detection
88
- post_ai_prob = ai_detect(paraphrased)
89
- # Semantic similarity
90
- sim = semantic_similarity(text, paraphrased)
91
- # Humanization score
92
- score = humanization_score(sim, post_ai_prob)
93
- rating = humanization_rating(score)
94
- return (
95
- paraphrased,
96
- pre_ai_prob * 100,
97
- post_ai_prob * 100,
98
- sim,
99
- rating,
100
- score * 100,
101
- f"Pre: {pre_ai_prob*100:.1f}% | Post: {post_ai_prob*100:.1f}%"
102
- )
103
-
104
- # Custom dark theme using gradio.themes.Base
105
- custom_theme = grthemes.Base(
106
- primary_hue="blue",
107
- secondary_hue="blue",
108
- neutral_hue="slate"
109
- )
110
-
111
- with gr.Blocks(theme=custom_theme) as demo:
112
- gr.Markdown("""
113
- # 🧠 AI Humanizer
114
- <div style='display:flex;justify-content:space-between;align-items:center;'>
115
- <span style='font-size:1.2em;color:#7bb1ff;'>Rewrite AI text to sound 100% human</span>
116
- <span style='font-weight:bold;color:#7bb1ff;'>Made by Taha</span>
117
- </div>
118
- """, elem_id="header")
119
- with gr.Row():
120
- with gr.Column():
121
- text_in = gr.Textbox(label="Paste AI-generated text here", lines=8, placeholder="Paste your text...")
122
- tone = gr.Radio(["Academic", "Casual", "Friendly", "Stealth"], value="Stealth", label="Tone Selector")
123
- btn = gr.Button("Humanize", elem_id="humanize-btn")
124
- with gr.Column():
125
- text_out = gr.Textbox(label="Humanized Output", lines=8, interactive=False)
126
- rating = gr.Markdown("", elem_id="rating")
127
- gr.Markdown("<hr/>")
128
- gr.Markdown("<b>AI Probability Scores</b>")
129
- ai_scores = gr.Markdown("", elem_id="ai-scores")
130
- gr.Markdown("<b>Semantic Similarity</b>")
131
- sim_score = gr.Number(label="Similarity (0=very different, 1=very similar)", interactive=False)
132
- gr.Markdown("<b>Humanization %</b>")
133
- human_score = gr.Number(label="Humanization Score (%)", interactive=False)
134
- btn.click(
135
- process,
136
- inputs=[text_in, tone],
137
- outputs=[text_out, ai_scores, ai_scores, sim_score, rating, human_score, ai_scores],
138
- api_name="humanize"
139
- )
140
- gr.Markdown("""
141
- <div style='text-align:center;color:#7bb1ff;margin-top:2em;'>
142
- <b>Made by Taha</b> | Free for unlimited use | Optimized for students
143
- </div>
144
- """, elem_id="footer")
145
-
146
  demo.launch()
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ from transformers.pipelines import pipeline
5
+ from sentence_transformers import SentenceTransformer, util
6
+ import numpy as np
7
+ import gradio.themes as grthemes
8
+
9
+ # Paraphrasing model: humarin/chatgpt_paraphraser_on_T5_base
10
+ PARAPHRASE_MODEL_NAME = "humarin/chatgpt_paraphraser_on_T5_base"
11
+ paraphrase_tokenizer = AutoTokenizer.from_pretrained(PARAPHRASE_MODEL_NAME)
12
+ paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained(PARAPHRASE_MODEL_NAME)
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+ paraphrase_model = paraphrase_model.to(device)
15
+
16
+ # AI Detector: roberta-base-openai-detector
17
+ ai_detector = pipeline("text-classification", model="roberta-base-openai-detector", device=0 if torch.cuda.is_available() else -1)
18
+
19
+ # Semantic similarity model
20
+ similarity_model = SentenceTransformer('all-MiniLM-L6-v2')
21
+
22
+ tone_templates = {
23
+ "Academic": "Paraphrase the following text in a formal, academic tone:",
24
+ "Casual": "Paraphrase the following text in a casual, conversational tone:",
25
+ "Friendly": "Paraphrase the following text in a friendly, approachable tone:",
26
+ "Stealth": "Paraphrase the following text to bypass AI detectors and sound as human as possible:",
27
+ }
28
+
29
+ # Paraphrasing function
30
+ def paraphrase(text, tone):
31
+ prompt = tone_templates[tone] + " " + text
32
+ input_ids = paraphrase_tokenizer(
33
+ f'paraphrase: {prompt}',
34
+ return_tensors="pt", padding="longest",
35
+ max_length=256, truncation=True
36
+ ).input_ids.to(device)
37
+ outputs = paraphrase_model.generate(
38
+ input_ids,
39
+ temperature=0.7,
40
+ repetition_penalty=1.2,
41
+ num_return_sequences=1,
42
+ no_repeat_ngram_size=2,
43
+ max_length=256,
44
+ diversity_penalty=3.0,
45
+ num_beams=5,
46
+ num_beam_groups=5,
47
+ trust_remote_code=True
48
+ )
49
+ res = paraphrase_tokenizer.batch_decode(outputs, skip_special_tokens=True)
50
+ return res[0] if res else ""
51
+
52
+ def semantic_similarity(text1, text2):
53
+ emb1 = similarity_model.encode(text1, convert_to_tensor=True)
54
+ emb2 = similarity_model.encode(text2, convert_to_tensor=True)
55
+ sim = util.pytorch_cos_sim(emb1, emb2).item()
56
+ return sim
57
+
58
+ def ai_detect(text):
59
+ # Returns probability of being AI-generated (label 'Fake')
60
+ result = ai_detector(text)
61
+ for r in result:
62
+ if r['label'] == 'Fake':
63
+ return r['score']
64
+ elif r['label'] == 'Real':
65
+ return 1.0 - r['score']
66
+ return 0.5 # fallback
67
+
68
+ def humanization_score(sim, ai_prob):
69
+ # Lower similarity and lower AI probability = more human
70
+ score = (1.0 - sim) * 0.5 + (1.0 - ai_prob) * 0.5
71
+ return score
72
+
73
+ def humanization_rating(score):
74
+ if score < 0.7:
75
+ return f"⚠️ Still robotic ({score:.2f})"
76
+ elif score < 0.85:
77
+ return f"👍 Acceptable ({score:.2f})"
78
+ else:
79
+ return f"✅ Highly Human ({score:.2f})"
80
+
81
+ def process(text, tone):
82
+ if not text.strip():
83
+ return "", "", 0.0, "", 0.0
84
+ # Pre-humanization AI detection
85
+ pre_ai_prob = ai_detect(text)
86
+ # Paraphrase
87
+ paraphrased = paraphrase(text, tone)
88
+ # Post-humanization AI detection
89
+ post_ai_prob = ai_detect(paraphrased)
90
+ # Semantic similarity
91
+ sim = semantic_similarity(text, paraphrased)
92
+ # Humanization score
93
+ score = humanization_score(sim, post_ai_prob)
94
+ rating = humanization_rating(score)
95
+ ai_score_str = f"Pre: {pre_ai_prob*100:.1f}% | Post: {post_ai_prob*100:.1f}%"
96
+ return (
97
+ paraphrased, # gr.Textbox (string)
98
+ ai_score_str, # gr.Markdown (string)
99
+ sim, # gr.Number (float)
100
+ rating, # gr.Markdown (string)
101
+ score * 100 # gr.Number (float)
102
+ )
103
+
104
+ # Custom dark theme using gradio.themes.Base
105
+ custom_theme = grthemes.Base(
106
+ primary_hue="blue",
107
+ secondary_hue="blue",
108
+ neutral_hue="slate"
109
+ )
110
+
111
+ with gr.Blocks(theme=custom_theme) as demo:
112
+ gr.Markdown("""
113
+ # 🧠 AI Humanizer
114
+ <div style='display:flex;justify-content:space-between;align-items:center;'>
115
+ <span style='font-size:1.2em;color:#7bb1ff;'>Rewrite AI text to sound 100% human</span>
116
+ <span style='font-weight:bold;color:#7bb1ff;'>Made by Taha</span>
117
+ </div>
118
+ """, elem_id="header")
119
+ with gr.Row():
120
+ with gr.Column():
121
+ text_in = gr.Textbox(label="Paste AI-generated text here", lines=8, placeholder="Paste your text...")
122
+ tone = gr.Radio(["Academic", "Casual", "Friendly", "Stealth"], value="Stealth", label="Tone Selector")
123
+ btn = gr.Button("Humanize", elem_id="humanize-btn")
124
+ with gr.Column():
125
+ text_out = gr.Textbox(label="Humanized Output", lines=8, interactive=False)
126
+ ai_scores = gr.Markdown("", elem_id="ai-scores")
127
+ sim_score = gr.Number(label="Similarity (0=very different, 1=very similar)", interactive=False)
128
+ rating = gr.Markdown("", elem_id="rating")
129
+ human_score = gr.Number(label="Humanization Score (%)", interactive=False)
130
+ btn.click(
131
+ process,
132
+ inputs=[text_in, tone],
133
+ outputs=[text_out, ai_scores, sim_score, rating, human_score],
134
+ api_name="humanize"
135
+ )
136
+ gr.Markdown("""
137
+ <div style='text-align:center;color:#7bb1ff;margin-top:2em;'>
138
+ <b>Made by Taha</b> | Free for unlimited use | Optimized for students
139
+ </div>
140
+ """, elem_id="footer")
141
+
 
 
 
 
142
  demo.launch()