File size: 6,326 Bytes
8cb40c4
 
c458b34
 
 
 
8cb40c4
32dbf0f
 
 
8cb40c4
 
 
 
 
32dbf0f
 
 
8cb40c4
387f42d
9ef3372
32dbf0f
 
c458b34
 
9ef3372
 
32dbf0f
8cb40c4
 
 
 
 
 
32dbf0f
8cb40c4
 
 
 
32dbf0f
8cb40c4
32dbf0f
9ef3372
32dbf0f
 
 
 
 
c458b34
32dbf0f
 
 
 
 
 
c458b34
 
9ef3372
c458b34
 
 
 
 
 
 
 
 
 
9ef3372
 
32dbf0f
 
 
 
 
c458b34
32dbf0f
 
 
 
 
 
c458b34
 
9ef3372
c458b34
 
 
 
 
 
 
 
 
 
9ef3372
387f42d
32dbf0f
 
 
387f42d
32dbf0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c458b34
 
 
 
387f42d
 
454c122
 
c458b34
32dbf0f
387f42d
 
c458b34
 
 
 
 
 
 
 
 
 
 
 
 
387f42d
 
c458b34
387f42d
c458b34
 
387f42d
 
 
c458b34
 
387f42d
 
 
8cb40c4
32dbf0f
 
 
8cb40c4
 
387f42d
32dbf0f
c458b34
32dbf0f
387f42d
c458b34
 
32dbf0f
 
bf335e8
 
 
32dbf0f
 
 
bf335e8
c458b34
 
 
 
 
 
387f42d
c458b34
32dbf0f
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import torch
import gradio as gr
from transformers import (
    AutoTokenizer,
    AutoModelForSequenceClassification
)

# =========================
# Model loading (UNCHANGED)
# =========================
MODEL_NAME = "duclo90/results"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
model.eval()

# =========================
# Inference logic (UNCHANGED)
# =========================
def classify_text(text):
    if not text.strip():
        return """
        <div style="text-align: center; padding: 40px; background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
                    border-radius: 20px; color: white;">
            <h2 style="margin: 0;">⚠️ Oops!</h2>
            <p style="margin-top: 10px; font-size: 18px;">Please enter some text to analyze</p>
        </div>
        """

    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        max_length=512
    )

    with torch.no_grad():
        outputs = model(**inputs)
        probs = torch.softmax(outputs.logits, dim=-1)
        score, pred = torch.max(probs, dim=-1)

    label = model.config.id2label[pred.item()]

    if label.lower() == "human":
        return """
        <div style="text-align: center; padding: 50px 30px;
                    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
                    border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
                    animation: slideIn 0.5s ease-out;">
            <div style="font-size: 80px; margin-bottom: 20px; animation: bounce 1s ease;">👤</div>
            <h1 style="color: white; font-size: 42px; margin: 0; font-weight: 700;">Human-Written</h1>
            <p style="color: rgba(255,255,255,0.9); font-size: 20px; margin-top: 15px;">
                This text appears to be written by a human
            </p>
            <div style="margin-top: 30px; padding: 15px 30px; background: rgba(255,255,255,0.2);
                        border-radius: 15px; display: inline-block;">
                <span style="color: white; font-size: 18px; font-weight: 600;">✨ Natural & Authentic</span>
            </div>
        </div>
        <style>
            @keyframes slideIn {
                from { opacity: 0; transform: translateY(20px); }
                to { opacity: 1; transform: translateY(0); }
            }
            @keyframes bounce {
                0%, 100% { transform: translateY(0); }
                50% { transform: translateY(-10px); }
            }
        </style>
        """
    else:
        return """
        <div style="text-align: center; padding: 50px 30px;
                    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
                    border-radius: 25px; box-shadow: 0 20px 60px rgba(0,0,0,0.3);
                    animation: slideIn 0.5s ease-out;">
            <div style="font-size: 80px; margin-bottom: 20px; animation: bounce 1s ease;">🤖</div>
            <h1 style="color: white; font-size: 42px; margin: 0; font-weight: 700;">AI-Generated</h1>
            <p style="color: rgba(255,255,255,0.9); font-size: 20px; margin-top: 15px;">
                This text appears to be machine-generated
            </p>
            <div style="margin-top: 30px; padding: 15px 30px; background: rgba(255,255,255,0.2);
                        border-radius: 15px; display: inline-block;">
                <span style="color: white; font-size: 18px; font-weight: 600;">⚡ AI-Powered Content</span>
            </div>
        </div>
        <style>
            @keyframes slideIn {
                from { opacity: 0; transform: translateY(20px); }
                to { opacity: 1; transform: translateY(0); }
            }
            @keyframes bounce {
                0%, 100% { transform: translateY(0); }
                50% { transform: translateY(-10px); }
            }
        </style>
        """

# =========================
# CSS (ONLY UI REMOVALS)
# =========================
custom_css = """
/* Remove Gradio top bar, API button, settings, footer */
header,
footer,
.gradio-header,
.gradio-footer,
.gradio-toolbar,
#top-bar,
[class*="header"],
[class*="toolbar"],
[class*="footer"],
.svelte-1ipelgc,
.svelte-1xw6x5s {
    display: none !important;
    height: 0 !important;
    min-height: 0 !important;
    visibility: hidden !important;
    padding: 0 !important;
    margin: 0 !important;
}

/* Remove leftover spacing */
body {
    margin: 0 !important;
    padding: 0 !important;
}

/* Your existing styles */
#component-0 {
    max-width: 900px;
    margin: auto;
    padding: 20px;
}

.gradio-container {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
    font-family: 'Inter', sans-serif;
    min-height: 100vh !important;
}

#title {
    text-align: center;
    color: white;
    font-size: 2.5em;
    font-weight: 700;
    margin-bottom: 10px;
}

#description {
    text-align: center;
    color: rgba(255,255,255,0.9);
    font-size: 1.1em;
    margin-bottom: 30px;
}

.input-textarea, .output-markdown {
    border-radius: 15px !important;
    border: 2px solid rgba(255,255,255,0.3) !important;
    background: rgba(255,255,255,0.95) !important;
}

button {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
    border-radius: 10px !important;
    font-weight: 600 !important;
}
"""

# =========================
# Interface (UNCHANGED)
# =========================
iface = gr.Interface(
    fn=classify_text,
    inputs=gr.Textbox(
        lines=8,
        placeholder="✍️ Paste or type text here to analyze...",
        label="Enter Text"
    ),
    outputs=gr.HTML(label="Analysis Result"),
    title="<div id='title'>🔍 AI Text Classifier</div>",
    description="<div id='description'>Detect whether text is human-written or AI-generated using advanced NLP<br>"
                "<span style='font-size: 0.85em; opacity: 0.7;'>Model by Dergaoui Ayoub</span></div>",
    flagging_mode="never"
)

# =========================
# Launch
# =========================
iface.launch(
    theme=gr.themes.Soft(
        primary_hue="purple",
        secondary_hue="pink",
        neutral_hue="slate",
        font=gr.themes.GoogleFont("Inter")
    ),
    css=custom_css,
    share=False
)