Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,37 +6,128 @@ from transformers import (
|
|
| 6 |
)
|
| 7 |
|
| 8 |
MODEL_NAME = "duclo90/results"
|
| 9 |
-
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 11 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_NAME)
|
| 12 |
-
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
def classify_text(text):
|
|
|
|
|
|
|
|
|
|
| 16 |
inputs = tokenizer(
|
| 17 |
text,
|
| 18 |
return_tensors="pt",
|
| 19 |
truncation=True,
|
| 20 |
max_length=512
|
| 21 |
)
|
| 22 |
-
|
| 23 |
with torch.no_grad():
|
| 24 |
outputs = model(**inputs)
|
| 25 |
probs = torch.softmax(outputs.logits, dim=-1)
|
| 26 |
score, pred = torch.max(probs, dim=-1)
|
| 27 |
-
|
| 28 |
label = model.config.id2label[pred.item()]
|
| 29 |
-
confidence = round(score.item(),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
|
|
|
| 34 |
iface = gr.Interface(
|
| 35 |
fn=classify_text,
|
| 36 |
-
inputs=gr.Textbox(
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
-
iface.launch()
|
|
|
|
| 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 "โ ๏ธ Please enter some text to analyze."
|
| 16 |
+
|
| 17 |
inputs = tokenizer(
|
| 18 |
text,
|
| 19 |
return_tensors="pt",
|
| 20 |
truncation=True,
|
| 21 |
max_length=512
|
| 22 |
)
|
|
|
|
| 23 |
with torch.no_grad():
|
| 24 |
outputs = model(**inputs)
|
| 25 |
probs = torch.softmax(outputs.logits, dim=-1)
|
| 26 |
score, pred = torch.max(probs, dim=-1)
|
| 27 |
+
|
| 28 |
label = model.config.id2label[pred.item()]
|
| 29 |
+
confidence = round(score.item() * 100, 1)
|
| 30 |
+
|
| 31 |
+
# Format output with icons and styling
|
| 32 |
+
icon = "๐ค" if label.lower() == "human" else "๐ค"
|
| 33 |
+
|
| 34 |
+
result = f"""
|
| 35 |
+
### {icon} Classification Result
|
| 36 |
+
|
| 37 |
+
**Prediction:** {label}-Written Text
|
| 38 |
+
|
| 39 |
+
**Confidence:** {confidence}%
|
| 40 |
+
|
| 41 |
+
{'๐ฆ' * int(confidence // 10)} {confidence}%
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
return result
|
| 45 |
+
|
| 46 |
+
# Custom CSS for modern look
|
| 47 |
+
custom_css = """
|
| 48 |
+
#component-0 {
|
| 49 |
+
max-width: 900px;
|
| 50 |
+
margin: auto;
|
| 51 |
+
padding: 20px;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
.gradio-container {
|
| 55 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !important;
|
| 56 |
+
font-family: 'Inter', sans-serif;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
#title {
|
| 60 |
+
text-align: center;
|
| 61 |
+
color: white;
|
| 62 |
+
font-size: 2.5em;
|
| 63 |
+
font-weight: 700;
|
| 64 |
+
margin-bottom: 10px;
|
| 65 |
+
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
#description {
|
| 69 |
+
text-align: center;
|
| 70 |
+
color: rgba(255,255,255,0.9);
|
| 71 |
+
font-size: 1.1em;
|
| 72 |
+
margin-bottom: 30px;
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
.input-textarea, .output-markdown {
|
| 76 |
+
border-radius: 15px !important;
|
| 77 |
+
border: 2px solid rgba(255,255,255,0.3) !important;
|
| 78 |
+
background: rgba(255,255,255,0.95) !important;
|
| 79 |
+
box-shadow: 0 8px 32px rgba(0,0,0,0.1) !important;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
button {
|
| 83 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%) !important;
|
| 84 |
+
border: none !important;
|
| 85 |
+
border-radius: 10px !important;
|
| 86 |
+
padding: 12px 30px !important;
|
| 87 |
+
font-weight: 600 !important;
|
| 88 |
+
font-size: 16px !important;
|
| 89 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.2) !important;
|
| 90 |
+
transition: transform 0.2s !important;
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
button:hover {
|
| 94 |
+
transform: translateY(-2px) !important;
|
| 95 |
+
box-shadow: 0 6px 20px rgba(0,0,0,0.3) !important;
|
| 96 |
+
}
|
| 97 |
|
| 98 |
+
.footer {
|
| 99 |
+
display: none !important;
|
| 100 |
+
}
|
| 101 |
+
"""
|
| 102 |
|
| 103 |
+
# Example texts
|
| 104 |
+
examples = [
|
| 105 |
+
["The sun set over the horizon, painting the sky in brilliant shades of orange and pink. I sat there, mesmerized by nature's beauty."],
|
| 106 |
+
["According to recent data analysis, the implementation of artificial intelligence systems has increased operational productivity by approximately 40 percent across various industries."],
|
| 107 |
+
["Can't believe how amazing that concert was last night! The energy in the crowd was absolutely electric ๐ธโจ"],
|
| 108 |
+
]
|
| 109 |
|
| 110 |
+
# Create interface with modern theme
|
| 111 |
iface = gr.Interface(
|
| 112 |
fn=classify_text,
|
| 113 |
+
inputs=gr.Textbox(
|
| 114 |
+
lines=8,
|
| 115 |
+
placeholder="โ๏ธ Paste or type text here to analyze...",
|
| 116 |
+
label="Enter Text",
|
| 117 |
+
elem_id="input-text"
|
| 118 |
+
),
|
| 119 |
+
outputs=gr.Markdown(label="Analysis Result"),
|
| 120 |
+
title="<div id='title'>๐ AI Text Classifier</div>",
|
| 121 |
+
description="<div id='description'>Detect whether text is human-written or AI-generated using advanced NLP</div>",
|
| 122 |
+
examples=examples,
|
| 123 |
+
theme=gr.themes.Soft(
|
| 124 |
+
primary_hue="purple",
|
| 125 |
+
secondary_hue="pink",
|
| 126 |
+
neutral_hue="slate",
|
| 127 |
+
font=gr.themes.GoogleFont("Inter")
|
| 128 |
+
),
|
| 129 |
+
css=custom_css,
|
| 130 |
+
flagging_mode="never"
|
| 131 |
)
|
| 132 |
|
| 133 |
+
iface.launch()
|