Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import torch
|
|
| 6 |
from transformers import RobertaTokenizerFast, RobertaForSequenceClassification
|
| 7 |
|
| 8 |
# ---------------------------
|
| 9 |
-
# Load your model
|
| 10 |
# ---------------------------
|
| 11 |
MODEL_NAME = "Alifjo123/robertaBase_messaging_100k"
|
| 12 |
tokenizer = RobertaTokenizerFast.from_pretrained(MODEL_NAME)
|
|
@@ -25,27 +25,18 @@ def predict_label(text: str):
|
|
| 25 |
outputs = model(**inputs)
|
| 26 |
logits = outputs.logits
|
| 27 |
probs = torch.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 28 |
-
|
| 29 |
-
# Two-class model: 0=Safe, 1=Unsafe
|
| 30 |
-
pred = int(probs[1] > probs[0])
|
| 31 |
return pred, probs[0] * 100.0, probs[1] * 100.0
|
| 32 |
|
| 33 |
# ---------------------------
|
| 34 |
-
# Render chat HTML from state
|
| 35 |
# ---------------------------
|
| 36 |
def render_chat(messages):
|
| 37 |
html_parts = ['<div class="chat">']
|
| 38 |
for m in messages:
|
| 39 |
side = "right" if m["role"] == "User A" else "left"
|
| 40 |
bubble_class = "bubble-a" if side == "right" else "bubble-b"
|
| 41 |
-
|
| 42 |
-
if m["pred"] == 0:
|
| 43 |
-
label = "Safe ✅"
|
| 44 |
-
chip_class = "chip-safe"
|
| 45 |
-
else:
|
| 46 |
-
label = "Unsafe ❌"
|
| 47 |
-
chip_class = "chip-unsafe"
|
| 48 |
-
|
| 49 |
safe = f'{m["safe"]:.1f}%'
|
| 50 |
unsafe = f'{m["unsafe"]:.1f}%'
|
| 51 |
ts = datetime.fromtimestamp(m["ts"]).strftime("%H:%M")
|
|
@@ -59,7 +50,7 @@ def render_chat(messages):
|
|
| 59 |
</div>
|
| 60 |
<div class="text">{html.escape(m["text"])}</div>
|
| 61 |
<div class="badges">
|
| 62 |
-
<span class="chip {
|
| 63 |
<span class="probs">Safe {safe} · Unsafe {unsafe}</span>
|
| 64 |
</div>
|
| 65 |
</div>
|
|
@@ -76,7 +67,7 @@ def send_message(role, message, messages):
|
|
| 76 |
messages = []
|
| 77 |
message = message.strip()
|
| 78 |
if not message:
|
| 79 |
-
return gr.update(), "", messages
|
| 80 |
|
| 81 |
pred, safe_pct, unsafe_pct = predict_label(message)
|
| 82 |
messages.append({
|
|
@@ -93,14 +84,14 @@ def clear_chat():
|
|
| 93 |
return render_chat([]), []
|
| 94 |
|
| 95 |
# ---------------------------
|
| 96 |
-
# Custom CSS (Pink + Blue ONLY)
|
| 97 |
# ---------------------------
|
| 98 |
CSS = """
|
| 99 |
* { box-sizing: border-box; }
|
| 100 |
:root {
|
| 101 |
-
--bg-gradient: linear-gradient(135deg, #ff99cc, #66ccff);
|
| 102 |
-
--bubble-a: #ff66b2;
|
| 103 |
-
--bubble-b: #3399ff;
|
| 104 |
--text-light: #f9f9f9;
|
| 105 |
--chip-safe: #00e676;
|
| 106 |
--chip-unsafe: #ff5252;
|
|
@@ -175,22 +166,30 @@ body {
|
|
| 175 |
font-size: 12px; text-align:center; margin-top: 8px; opacity:.8; color:#eee;
|
| 176 |
}
|
| 177 |
|
| 178 |
-
@keyframes fadeIn { from
|
| 179 |
"""
|
| 180 |
|
| 181 |
# ---------------------------
|
| 182 |
-
#
|
| 183 |
# ---------------------------
|
| 184 |
with gr.Blocks(css=CSS) as demo:
|
| 185 |
-
gr.
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from transformers import RobertaTokenizerFast, RobertaForSequenceClassification
|
| 7 |
|
| 8 |
# ---------------------------
|
| 9 |
+
# Load your model directly (NO API, just from HuggingFace hub)
|
| 10 |
# ---------------------------
|
| 11 |
MODEL_NAME = "Alifjo123/robertaBase_messaging_100k"
|
| 12 |
tokenizer = RobertaTokenizerFast.from_pretrained(MODEL_NAME)
|
|
|
|
| 25 |
outputs = model(**inputs)
|
| 26 |
logits = outputs.logits
|
| 27 |
probs = torch.softmax(logits, dim=-1).cpu().numpy()[0]
|
| 28 |
+
pred = int(probs[1] > probs[0]) # 1=unsafe, 0=safe
|
|
|
|
|
|
|
| 29 |
return pred, probs[0] * 100.0, probs[1] * 100.0
|
| 30 |
|
| 31 |
# ---------------------------
|
| 32 |
+
# Render chat (HTML) from state
|
| 33 |
# ---------------------------
|
| 34 |
def render_chat(messages):
|
| 35 |
html_parts = ['<div class="chat">']
|
| 36 |
for m in messages:
|
| 37 |
side = "right" if m["role"] == "User A" else "left"
|
| 38 |
bubble_class = "bubble-a" if side == "right" else "bubble-b"
|
| 39 |
+
label = "Unsafe ❌" if m["pred"] == 1 else "Safe ✅"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
safe = f'{m["safe"]:.1f}%'
|
| 41 |
unsafe = f'{m["unsafe"]:.1f}%'
|
| 42 |
ts = datetime.fromtimestamp(m["ts"]).strftime("%H:%M")
|
|
|
|
| 50 |
</div>
|
| 51 |
<div class="text">{html.escape(m["text"])}</div>
|
| 52 |
<div class="badges">
|
| 53 |
+
<span class="chip {'chip-unsafe' if m['pred']==1 else 'chip-safe'}">{label}</span>
|
| 54 |
<span class="probs">Safe {safe} · Unsafe {unsafe}</span>
|
| 55 |
</div>
|
| 56 |
</div>
|
|
|
|
| 67 |
messages = []
|
| 68 |
message = message.strip()
|
| 69 |
if not message:
|
| 70 |
+
return gr.update(), "", messages # ignore empty
|
| 71 |
|
| 72 |
pred, safe_pct, unsafe_pct = predict_label(message)
|
| 73 |
messages.append({
|
|
|
|
| 84 |
return render_chat([]), []
|
| 85 |
|
| 86 |
# ---------------------------
|
| 87 |
+
# Custom CSS (Pink + Blue ONLY, NO WHITE)
|
| 88 |
# ---------------------------
|
| 89 |
CSS = """
|
| 90 |
* { box-sizing: border-box; }
|
| 91 |
:root {
|
| 92 |
+
--bg-gradient: linear-gradient(135deg, #ff99cc, #66ccff); /* pink to blue */
|
| 93 |
+
--bubble-a: #ff66b2; /* darker pink */
|
| 94 |
+
--bubble-b: #3399ff; /* vibrant blue */
|
| 95 |
--text-light: #f9f9f9;
|
| 96 |
--chip-safe: #00e676;
|
| 97 |
--chip-unsafe: #ff5252;
|
|
|
|
| 166 |
font-size: 12px; text-align:center; margin-top: 8px; opacity:.8; color:#eee;
|
| 167 |
}
|
| 168 |
|
| 169 |
+
@keyframes fadeIn { from{opacity:0;transform:translateY(6px);} to{opacity:1;transform:translateY(0);} }
|
| 170 |
"""
|
| 171 |
|
| 172 |
# ---------------------------
|
| 173 |
+
# Build UI
|
| 174 |
# ---------------------------
|
| 175 |
with gr.Blocks(css=CSS) as demo:
|
| 176 |
+
with gr.Group(elem_classes="panel"):
|
| 177 |
+
gr.HTML('<div class="header">💬 Let\'s Chat</div>')
|
| 178 |
+
|
| 179 |
+
chat_html = gr.HTML(render_chat([]), elem_id="chat")
|
| 180 |
+
messages_state = gr.State([])
|
| 181 |
+
|
| 182 |
+
with gr.Row(elem_classes="controls"):
|
| 183 |
+
role = gr.Dropdown(["User A", "User B"], value="User A", label="Role")
|
| 184 |
+
msg = gr.Textbox(placeholder="Type a message…", label=None, lines=2, elem_classes="textbox")
|
| 185 |
+
send = gr.Button("Send", variant="primary")
|
| 186 |
+
clear = gr.Button("Clear", variant="secondary")
|
| 187 |
+
|
| 188 |
+
send.click(send_message, inputs=[role, msg, messages_state], outputs=[chat_html, msg, messages_state])
|
| 189 |
+
msg.submit(send_message, inputs=[role, msg, messages_state], outputs=[chat_html, msg, messages_state])
|
| 190 |
+
clear.click(clear_chat, outputs=[chat_html, messages_state])
|
| 191 |
+
|
| 192 |
+
gr.Markdown('<div class="footer-note">Model: <code>Alifjo123/robertaBase_messaging_100k</code></div>')
|
| 193 |
+
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
demo.launch()
|