Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,139 @@
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import
|
| 4 |
-
AutoTokenizer,
|
| 5 |
-
AutoModelForSequenceClassification
|
| 6 |
-
)
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
MODEL_NAME = "duclo90/results"
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 11 |
model.eval()
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def classify_text(text):
|
| 14 |
if not text.strip():
|
| 15 |
return """
|
| 16 |
-
<div style="text-align:
|
| 17 |
-
|
| 18 |
-
<
|
|
|
|
| 19 |
</div>
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
inputs = tokenizer(
|
| 23 |
text,
|
| 24 |
return_tensors="pt",
|
| 25 |
truncation=True,
|
| 26 |
max_length=512
|
| 27 |
)
|
|
|
|
| 28 |
with torch.no_grad():
|
| 29 |
outputs = model(**inputs)
|
| 30 |
probs = torch.softmax(outputs.logits, dim=-1)
|
| 31 |
score, pred = torch.max(probs, dim=-1)
|
| 32 |
-
|
| 33 |
label = model.config.id2label[pred.item()]
|
| 34 |
-
|
| 35 |
-
# Create attractive HTML output
|
| 36 |
if label.lower() == "human":
|
| 37 |
-
|
| 38 |
-
<div style="text-align:
|
| 39 |
-
|
| 40 |
-
<
|
| 41 |
-
<
|
| 42 |
-
<
|
| 43 |
-
|
| 44 |
-
</
|
| 45 |
</div>
|
| 46 |
-
<style>
|
| 47 |
-
@keyframes slideIn {
|
| 48 |
-
from { opacity: 0; transform: translateY(20px); }
|
| 49 |
-
to { opacity: 1; transform: translateY(0); }
|
| 50 |
-
}
|
| 51 |
-
@keyframes bounce {
|
| 52 |
-
0%, 100% { transform: translateY(0); }
|
| 53 |
-
50% { transform: translateY(-10px); }
|
| 54 |
-
}
|
| 55 |
-
</style>
|
| 56 |
"""
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
<div style="text-align:
|
| 60 |
-
|
| 61 |
-
<
|
| 62 |
-
<
|
| 63 |
-
<
|
| 64 |
-
|
| 65 |
-
</
|
| 66 |
</div>
|
| 67 |
-
<style>
|
| 68 |
-
@keyframes slideIn {
|
| 69 |
-
from { opacity: 0; transform: translateY(20px); }
|
| 70 |
-
to { opacity: 1; transform: translateY(0); }
|
| 71 |
-
}
|
| 72 |
-
@keyframes bounce {
|
| 73 |
-
0%, 100% { transform: translateY(0); }
|
| 74 |
-
50% { transform: translateY(-10px); }
|
| 75 |
-
}
|
| 76 |
-
</style>
|
| 77 |
"""
|
| 78 |
-
|
| 79 |
-
return result
|
| 80 |
|
| 81 |
-
#
|
|
|
|
|
|
|
| 82 |
custom_css = """
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
| 92 |
}
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
font-
|
| 99 |
-
margin-bottom: 10px;
|
| 100 |
-
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 101 |
}
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
}
|
| 109 |
|
| 110 |
-
|
|
|
|
| 111 |
border-radius: 15px !important;
|
| 112 |
-
|
| 113 |
-
background: rgba(255,255,255,0.95) !important;
|
| 114 |
-
box-shadow: 0 8px 32px rgba(0,0,0,0.1) !important;
|
| 115 |
}
|
| 116 |
|
|
|
|
| 117 |
button {
|
| 118 |
-
background: linear-gradient(135deg,
|
| 119 |
-
border:
|
| 120 |
-
border-radius: 10px !important;
|
| 121 |
-
padding: 12px 30px !important;
|
| 122 |
font-weight: 600 !important;
|
| 123 |
-
font-size: 16px !important;
|
| 124 |
-
box-shadow: 0 4px 15px rgba(0,0,0,0.2) !important;
|
| 125 |
-
transition: transform 0.2s !important;
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
button:hover {
|
| 129 |
-
transform: translateY(-2px) !important;
|
| 130 |
-
box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
|
| 131 |
-
}
|
| 132 |
-
|
| 133 |
-
.footer {
|
| 134 |
-
display: none !important;
|
| 135 |
}
|
| 136 |
"""
|
| 137 |
|
| 138 |
-
#
|
|
|
|
|
|
|
| 139 |
iface = gr.Interface(
|
| 140 |
fn=classify_text,
|
| 141 |
inputs=gr.Textbox(
|
| 142 |
-
lines=8,
|
| 143 |
-
placeholder="
|
| 144 |
-
label="
|
| 145 |
-
elem_id="input-text"
|
| 146 |
),
|
| 147 |
-
outputs=gr.HTML(label="
|
| 148 |
-
title="
|
| 149 |
-
description="
|
| 150 |
flagging_mode="never"
|
| 151 |
)
|
| 152 |
|
|
|
|
|
|
|
|
|
|
| 153 |
iface.launch(
|
| 154 |
-
theme=gr.themes.Soft(
|
| 155 |
-
primary_hue="purple",
|
| 156 |
-
secondary_hue="pink",
|
| 157 |
-
neutral_hue="slate",
|
| 158 |
-
font=gr.themes.GoogleFont("Inter")
|
| 159 |
-
),
|
| 160 |
css=custom_css,
|
| 161 |
-
share=False
|
| 162 |
-
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# =====================
|
| 6 |
+
# Model loading
|
| 7 |
+
# =====================
|
| 8 |
MODEL_NAME = "duclo90/results"
|
| 9 |
+
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 12 |
model.eval()
|
| 13 |
|
| 14 |
+
# =====================
|
| 15 |
+
# Inference function
|
| 16 |
+
# =====================
|
| 17 |
def classify_text(text):
|
| 18 |
if not text.strip():
|
| 19 |
return """
|
| 20 |
+
<div style="text-align:center; padding:40px; background:linear-gradient(135deg,#f093fb,#f5576c);
|
| 21 |
+
border-radius:20px; color:white;">
|
| 22 |
+
<h2>Oops!</h2>
|
| 23 |
+
<p>Please enter some text to analyze.</p>
|
| 24 |
</div>
|
| 25 |
"""
|
| 26 |
+
|
| 27 |
inputs = tokenizer(
|
| 28 |
text,
|
| 29 |
return_tensors="pt",
|
| 30 |
truncation=True,
|
| 31 |
max_length=512
|
| 32 |
)
|
| 33 |
+
|
| 34 |
with torch.no_grad():
|
| 35 |
outputs = model(**inputs)
|
| 36 |
probs = torch.softmax(outputs.logits, dim=-1)
|
| 37 |
score, pred = torch.max(probs, dim=-1)
|
| 38 |
+
|
| 39 |
label = model.config.id2label[pred.item()]
|
| 40 |
+
|
|
|
|
| 41 |
if label.lower() == "human":
|
| 42 |
+
return """
|
| 43 |
+
<div style="text-align:center; padding:50px; background:linear-gradient(135deg,#667eea,#764ba2);
|
| 44 |
+
border-radius:25px; box-shadow:0 20px 60px rgba(0,0,0,.3);">
|
| 45 |
+
<div style="font-size:80px;">👤</div>
|
| 46 |
+
<h1 style="color:white;">Human-Written</h1>
|
| 47 |
+
<p style="color:rgba(255,255,255,.9); font-size:18px;">
|
| 48 |
+
This text appears to be written by a human.
|
| 49 |
+
</p>
|
| 50 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
"""
|
| 52 |
else:
|
| 53 |
+
return """
|
| 54 |
+
<div style="text-align:center; padding:50px; background:linear-gradient(135deg,#f093fb,#f5576c);
|
| 55 |
+
border-radius:25px; box-shadow:0 20px 60px rgba(0,0,0,.3);">
|
| 56 |
+
<div style="font-size:80px;">🤖</div>
|
| 57 |
+
<h1 style="color:white;">AI-Generated</h1>
|
| 58 |
+
<p style="color:rgba(255,255,255,.9); font-size:18px;">
|
| 59 |
+
This text appears to be machine-generated.
|
| 60 |
+
</p>
|
| 61 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"""
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
# =====================
|
| 65 |
+
# Custom CSS (NO header / NO footer)
|
| 66 |
+
# =====================
|
| 67 |
custom_css = """
|
| 68 |
+
/* Remove Gradio top toolbar */
|
| 69 |
+
header,
|
| 70 |
+
footer,
|
| 71 |
+
.gradio-header,
|
| 72 |
+
.gradio-footer,
|
| 73 |
+
.gradio-toolbar,
|
| 74 |
+
#top-bar,
|
| 75 |
+
.svelte-1ipelgc,
|
| 76 |
+
.svelte-1xw6x5s {
|
| 77 |
+
display: none !important;
|
| 78 |
+
height: 0 !important;
|
| 79 |
+
visibility: hidden !important;
|
| 80 |
}
|
| 81 |
|
| 82 |
+
/* Remove reserved spacing */
|
| 83 |
+
body {
|
| 84 |
+
margin: 0 !important;
|
| 85 |
+
padding: 0 !important;
|
| 86 |
}
|
| 87 |
|
| 88 |
+
/* App container */
|
| 89 |
+
.gradio-container {
|
| 90 |
+
min-height: 100vh !important;
|
| 91 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 92 |
+
font-family: Inter, sans-serif;
|
|
|
|
|
|
|
| 93 |
}
|
| 94 |
|
| 95 |
+
/* Center content */
|
| 96 |
+
#component-0 {
|
| 97 |
+
max-width: 900px;
|
| 98 |
+
margin: auto;
|
| 99 |
+
padding: 30px;
|
| 100 |
}
|
| 101 |
|
| 102 |
+
/* Inputs & outputs */
|
| 103 |
+
textarea, .output-markdown {
|
| 104 |
border-radius: 15px !important;
|
| 105 |
+
background: rgba(255,255,255,.95) !important;
|
|
|
|
|
|
|
| 106 |
}
|
| 107 |
|
| 108 |
+
/* Buttons */
|
| 109 |
button {
|
| 110 |
+
background: linear-gradient(135deg,#f093fb,#f5576c) !important;
|
| 111 |
+
border-radius: 12px !important;
|
|
|
|
|
|
|
| 112 |
font-weight: 600 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
}
|
| 114 |
"""
|
| 115 |
|
| 116 |
+
# =====================
|
| 117 |
+
# Interface
|
| 118 |
+
# =====================
|
| 119 |
iface = gr.Interface(
|
| 120 |
fn=classify_text,
|
| 121 |
inputs=gr.Textbox(
|
| 122 |
+
lines=8,
|
| 123 |
+
placeholder="Paste or type text here…",
|
| 124 |
+
label="Input Text"
|
|
|
|
| 125 |
),
|
| 126 |
+
outputs=gr.HTML(label="Result"),
|
| 127 |
+
title="AI Text Classifier",
|
| 128 |
+
description="Detect whether text is human-written or AI-generated.",
|
| 129 |
flagging_mode="never"
|
| 130 |
)
|
| 131 |
|
| 132 |
+
# =====================
|
| 133 |
+
# Launch
|
| 134 |
+
# =====================
|
| 135 |
iface.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
css=custom_css,
|
| 137 |
+
share=False,
|
| 138 |
+
show_footer=False
|
| 139 |
+
)
|