File size: 5,456 Bytes
23866f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40f369c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from transformers.pipelines import pipeline
from sentence_transformers import SentenceTransformer, util
import numpy as np
import gradio.themes as grthemes

# Paraphrasing model: humarin/chatgpt_paraphraser_on_T5_base
PARAPHRASE_MODEL_NAME = "humarin/chatgpt_paraphraser_on_T5_base"
paraphrase_tokenizer = AutoTokenizer.from_pretrained(PARAPHRASE_MODEL_NAME)
paraphrase_model = AutoModelForSeq2SeqLM.from_pretrained(PARAPHRASE_MODEL_NAME)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
paraphrase_model = paraphrase_model.to(device)

# AI Detector: roberta-base-openai-detector
ai_detector = pipeline("text-classification", model="roberta-base-openai-detector", device=0 if torch.cuda.is_available() else -1)

# Semantic similarity model
similarity_model = SentenceTransformer('all-MiniLM-L6-v2')

tone_templates = {
    "Academic": "Paraphrase the following text in a formal, academic tone:",
    "Casual": "Paraphrase the following text in a casual, conversational tone:",
    "Friendly": "Paraphrase the following text in a friendly, approachable tone:",
    "Stealth": "Paraphrase the following text to bypass AI detectors and sound as human as possible:",
}

# Paraphrasing function
def paraphrase(text, tone):
    prompt = tone_templates[tone] + " " + text
    input_ids = paraphrase_tokenizer(
        f'paraphrase: {prompt}',
        return_tensors="pt", padding="longest",
        max_length=256, truncation=True
    ).input_ids.to(device)
    outputs = paraphrase_model.generate(
        input_ids,
        temperature=0.7,
        repetition_penalty=1.2,
        num_return_sequences=1,
        no_repeat_ngram_size=2,
        max_length=256,
        diversity_penalty=3.0,
        num_beams=5,
        num_beam_groups=5,
        trust_remote_code=True
    )
    res = paraphrase_tokenizer.batch_decode(outputs, skip_special_tokens=True)
    return res[0] if res else ""

def semantic_similarity(text1, text2):
    emb1 = similarity_model.encode(text1, convert_to_tensor=True)
    emb2 = similarity_model.encode(text2, convert_to_tensor=True)
    sim = util.pytorch_cos_sim(emb1, emb2).item()
    return sim

def ai_detect(text):
    # Returns probability of being AI-generated (label 'Fake')
    result = ai_detector(text)
    for r in result:
        if r['label'] == 'Fake':
            return r['score']
        elif r['label'] == 'Real':
            return 1.0 - r['score']
    return 0.5  # fallback

def humanization_score(sim, ai_prob):
    # Lower similarity and lower AI probability = more human
    score = (1.0 - sim) * 0.5 + (1.0 - ai_prob) * 0.5
    return score

def humanization_rating(score):
    if score < 0.7:
        return f"⚠️ Still robotic ({score:.2f})"
    elif score < 0.85:
        return f"👍 Acceptable ({score:.2f})"
    else:
        return f"✅ Highly Human ({score:.2f})"

def process(text, tone):
    if not text.strip():
        return "", "", 0.0, "", 0.0
    # Pre-humanization AI detection
    pre_ai_prob = ai_detect(text)
    # Paraphrase
    paraphrased = paraphrase(text, tone)
    # Post-humanization AI detection
    post_ai_prob = ai_detect(paraphrased)
    # Semantic similarity
    sim = semantic_similarity(text, paraphrased)
    # Humanization score
    score = humanization_score(sim, post_ai_prob)
    rating = humanization_rating(score)
    ai_score_str = f"Pre: {pre_ai_prob*100:.1f}% | Post: {post_ai_prob*100:.1f}%"
    return (
        paraphrased,         # gr.Textbox (string)
        ai_score_str,        # gr.Markdown (string)
        sim,                # gr.Number (float)
        rating,             # gr.Markdown (string)
        score * 100         # gr.Number (float)
    )

# Custom dark theme using gradio.themes.Base
custom_theme = grthemes.Base(
    primary_hue="blue",
    secondary_hue="blue",
    neutral_hue="slate"
)

with gr.Blocks(theme=custom_theme) as demo:
    gr.Markdown("""
    # 🧠 AI Humanizer
    <div style='display:flex;justify-content:space-between;align-items:center;'>
        <span style='font-size:1.2em;color:#7bb1ff;'>Rewrite AI text to sound 100% human</span>
        <span style='font-weight:bold;color:#7bb1ff;'>Made by Taha</span>
    </div>
    """, elem_id="header")
    with gr.Row():
        with gr.Column():
            text_in = gr.Textbox(label="Paste AI-generated text here", lines=8, placeholder="Paste your text...")
            tone = gr.Radio(["Academic", "Casual", "Friendly", "Stealth"], value="Stealth", label="Tone Selector")
            btn = gr.Button("Humanize", elem_id="humanize-btn")
        with gr.Column():
            text_out = gr.Textbox(label="Humanized Output", lines=8, interactive=False)
            ai_scores = gr.Markdown("", elem_id="ai-scores")
            sim_score = gr.Number(label="Similarity (0=very different, 1=very similar)", interactive=False)
            rating = gr.Markdown("", elem_id="rating")
            human_score = gr.Number(label="Humanization Score (%)", interactive=False)
    btn.click(
        process,
        inputs=[text_in, tone],
        outputs=[text_out, ai_scores, sim_score, rating, human_score],
        api_name="humanize"
    )
    gr.Markdown("""
    <div style='text-align:center;color:#7bb1ff;margin-top:2em;'>
        <b>Made by Taha</b> | Free for unlimited use | Optimized for students
    </div>
    """, elem_id="footer")

demo.launch()