Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
-
🪶 Council Matters Classifier – PT
|
| 5 |
-
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
|
@@ -12,6 +12,7 @@ import re
|
|
| 12 |
from pathlib import Path
|
| 13 |
from scipy.sparse import hstack, csr_matrix
|
| 14 |
|
|
|
|
| 15 |
try:
|
| 16 |
import torch
|
| 17 |
from transformers import AutoTokenizer, AutoModel
|
|
@@ -127,8 +128,10 @@ class PortugueseClassifier:
|
|
| 127 |
return predicted_labels
|
| 128 |
|
| 129 |
|
|
|
|
| 130 |
classifier = PortugueseClassifier()
|
| 131 |
|
|
|
|
| 132 |
suggestions = [
|
| 133 |
"A Câmara Municipal aprovou o novo orçamento para 2025, com foco em sustentabilidade.",
|
| 134 |
"Foi decidido avançar com o projeto de requalificação do centro histórico.",
|
|
@@ -141,89 +144,17 @@ suggestions = [
|
|
| 141 |
"A Câmara apoiará as associações juvenis em atividades culturais.",
|
| 142 |
"O plano estratégico inclui medidas para atrair investimento privado.",
|
| 143 |
]
|
|
|
|
| 144 |
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
color: #f1f1f1;
|
| 150 |
-
font-family: 'Inter', sans-serif;
|
| 151 |
-
}
|
| 152 |
-
.gradio-container { background-color: #0c0c0c; color: #f1f1f1; }
|
| 153 |
-
h2, h3 { text-align: center; color: #00b4ff; font-weight: 600; }
|
| 154 |
-
textarea {
|
| 155 |
-
background-color: #181818 !important;
|
| 156 |
-
color: #fff !important;
|
| 157 |
-
border-radius: 10px !important;
|
| 158 |
-
border: 1px solid #333 !important;
|
| 159 |
-
}
|
| 160 |
-
button {
|
| 161 |
-
background-color: #007aff !important;
|
| 162 |
-
color: white !important;
|
| 163 |
-
font-weight: 600 !important;
|
| 164 |
-
border-radius: 8px !important;
|
| 165 |
-
border: none !important;
|
| 166 |
-
}
|
| 167 |
-
button:hover { background-color: #00aaff !important; }
|
| 168 |
-
.output-chip {
|
| 169 |
-
background-color: #1a1a1a;
|
| 170 |
-
color: #00c3ff;
|
| 171 |
-
padding: 5px 12px;
|
| 172 |
-
border-radius: 8px;
|
| 173 |
-
border: 1px solid #007aff33;
|
| 174 |
-
font-weight: 500;
|
| 175 |
-
}
|
| 176 |
-
.suggestion-box {
|
| 177 |
-
background-color: #111;
|
| 178 |
-
border-radius: 10px;
|
| 179 |
-
border: 1px solid #222;
|
| 180 |
-
padding: 10px;
|
| 181 |
-
display: flex;
|
| 182 |
-
align-items: center;
|
| 183 |
-
justify-content: space-between;
|
| 184 |
-
color: #aaa;
|
| 185 |
-
margin-top: 25px;
|
| 186 |
-
}
|
| 187 |
-
.arrow-btn {
|
| 188 |
-
background: none;
|
| 189 |
-
border: none;
|
| 190 |
-
color: #00c3ff;
|
| 191 |
-
font-size: 22px;
|
| 192 |
-
cursor: pointer;
|
| 193 |
-
}
|
| 194 |
-
.arrow-btn:hover { color: #00e0ff; transform: scale(1.15); }
|
| 195 |
-
"""
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
function updateSuggestionText() {{
|
| 202 |
-
const el = document.getElementById('suggestion-text');
|
| 203 |
-
if (!el) return;
|
| 204 |
-
el.style.opacity = 0;
|
| 205 |
-
setTimeout(() => {{
|
| 206 |
-
el.innerText = examples[index];
|
| 207 |
-
document.getElementById('input-text').value = examples[index];
|
| 208 |
-
el.style.opacity = 1;
|
| 209 |
-
}}, 150);
|
| 210 |
-
}}
|
| 211 |
-
|
| 212 |
-
window.addEventListener('DOMContentLoaded', () => {{
|
| 213 |
-
const prev = document.getElementById('prev-btn');
|
| 214 |
-
const next = document.getElementById('next-btn');
|
| 215 |
-
if (prev && next) {{
|
| 216 |
-
prev.addEventListener('click', () => {{
|
| 217 |
-
index = (index - 1 + examples.length) % examples.length;
|
| 218 |
-
updateSuggestionText();
|
| 219 |
-
}});
|
| 220 |
-
next.addEventListener('click', () => {{
|
| 221 |
-
index = (index + 1) % examples.length;
|
| 222 |
-
updateSuggestionText();
|
| 223 |
-
}});
|
| 224 |
-
}}
|
| 225 |
-
}});
|
| 226 |
-
"""
|
| 227 |
|
| 228 |
def classify_display(text):
|
| 229 |
preds = classifier.predict(text)
|
|
@@ -235,34 +166,46 @@ def classify_display(text):
|
|
| 235 |
prob = p["probability"]
|
| 236 |
conf = p["confidence"]
|
| 237 |
color = {"high": "#00ff88", "medium": "#ffd966", "low": "#ff6666"}[conf]
|
| 238 |
-
chips += f"<span class='output-chip' style='color:{color}'>{label} ({prob:.0%})</span>"
|
| 239 |
return f"<div style='display:flex;flex-wrap:wrap;gap:10px;justify-content:center;margin-top:10px'>{chips}</div>"
|
| 240 |
|
| 241 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
gr.Markdown("## 🏛️ **Council Matters Classifier – PT**")
|
| 243 |
gr.Markdown("### Insira texto administrativo em português:")
|
| 244 |
|
| 245 |
input_text = gr.Textbox(
|
| 246 |
-
label="",
|
| 247 |
-
placeholder="Escreva aqui o texto em português...",
|
| 248 |
-
lines=6,
|
| 249 |
-
elem_id="input-text"
|
| 250 |
)
|
| 251 |
|
| 252 |
classify_btn = gr.Button("Classificar")
|
| 253 |
output = gr.HTML(label="Tópicos previstos")
|
| 254 |
classify_btn.click(fn=classify_display, inputs=input_text, outputs=output)
|
| 255 |
|
| 256 |
-
|
| 257 |
-
gr.
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
""")
|
| 266 |
|
|
|
|
| 267 |
if __name__ == "__main__":
|
| 268 |
demo.launch()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
# -*- coding: utf-8 -*-
|
| 3 |
"""
|
| 4 |
+
🪶 Council Matters Classifier – PT
|
| 5 |
+
Dark modern Gradio interface with working suggestions carousel.
|
| 6 |
"""
|
| 7 |
|
| 8 |
import gradio as gr
|
|
|
|
| 12 |
from pathlib import Path
|
| 13 |
from scipy.sparse import hstack, csr_matrix
|
| 14 |
|
| 15 |
+
# Optional PyTorch
|
| 16 |
try:
|
| 17 |
import torch
|
| 18 |
from transformers import AutoTokenizer, AutoModel
|
|
|
|
| 128 |
return predicted_labels
|
| 129 |
|
| 130 |
|
| 131 |
+
# ---------------- Load Classifier ----------------
|
| 132 |
classifier = PortugueseClassifier()
|
| 133 |
|
| 134 |
+
# ---------------- Suggestions ----------------
|
| 135 |
suggestions = [
|
| 136 |
"A Câmara Municipal aprovou o novo orçamento para 2025, com foco em sustentabilidade.",
|
| 137 |
"Foi decidido avançar com o projeto de requalificação do centro histórico.",
|
|
|
|
| 144 |
"A Câmara apoiará as associações juvenis em atividades culturais.",
|
| 145 |
"O plano estratégico inclui medidas para atrair investimento privado.",
|
| 146 |
]
|
| 147 |
+
example_idx = 0
|
| 148 |
|
| 149 |
+
def next_example():
|
| 150 |
+
global example_idx
|
| 151 |
+
example_idx = (example_idx + 1) % len(suggestions)
|
| 152 |
+
return suggestions[example_idx], suggestions[example_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
def prev_example():
|
| 155 |
+
global example_idx
|
| 156 |
+
example_idx = (example_idx - 1 + len(suggestions)) % len(suggestions)
|
| 157 |
+
return suggestions[example_idx], suggestions[example_idx]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
def classify_display(text):
|
| 160 |
preds = classifier.predict(text)
|
|
|
|
| 166 |
prob = p["probability"]
|
| 167 |
conf = p["confidence"]
|
| 168 |
color = {"high": "#00ff88", "medium": "#ffd966", "low": "#ff6666"}[conf]
|
| 169 |
+
chips += f"<span class='output-chip' style='border-color:{color}80;color:{color}'>{label} ({prob:.0%})</span>"
|
| 170 |
return f"<div style='display:flex;flex-wrap:wrap;gap:10px;justify-content:center;margin-top:10px'>{chips}</div>"
|
| 171 |
|
| 172 |
+
# ---------------- CSS ----------------
|
| 173 |
+
custom_css = """
|
| 174 |
+
body { background-color: #0c0c0c; color: #f1f1f1; font-family: 'Inter', sans-serif; }
|
| 175 |
+
.gradio-container { background-color: #0c0c0c; color: #f1f1f1; }
|
| 176 |
+
h2, h3 { text-align: center; color: #00b4ff; font-weight: 600; }
|
| 177 |
+
textarea { background-color: #181818 !important; color: #fff !important; border-radius: 10px !important; border: 1px solid #333 !important; }
|
| 178 |
+
button { background-color: #007aff !important; color: white !important; font-weight: 600 !important; border-radius: 8px !important; border: none !important; }
|
| 179 |
+
button:hover { background-color: #00aaff !important; }
|
| 180 |
+
.output-chip { background-color: #1a1a1a; padding: 5px 12px; border-radius: 8px; font-weight: 500; border: 1px solid #007aff33; }
|
| 181 |
+
.suggestion-box { background-color: #111; border-radius: 10px; border: 1px solid #222; padding: 10px; display: flex; align-items: center; justify-content: space-between; color: #aaa; margin-top: 25px; }
|
| 182 |
+
.arrow-btn { background: none; border: none; color: #00c3ff; font-size: 22px; cursor: pointer; }
|
| 183 |
+
.arrow-btn:hover { color: #00e0ff; transform: scale(1.15); }
|
| 184 |
+
"""
|
| 185 |
+
|
| 186 |
+
# ---------------- Gradio UI ----------------
|
| 187 |
+
with gr.Blocks(css=custom_css, theme="gradio/soft") as demo:
|
| 188 |
gr.Markdown("## 🏛️ **Council Matters Classifier – PT**")
|
| 189 |
gr.Markdown("### Insira texto administrativo em português:")
|
| 190 |
|
| 191 |
input_text = gr.Textbox(
|
| 192 |
+
label="", placeholder="Escreva aqui o texto em português...", lines=6, elem_id="input-text"
|
|
|
|
|
|
|
|
|
|
| 193 |
)
|
| 194 |
|
| 195 |
classify_btn = gr.Button("Classificar")
|
| 196 |
output = gr.HTML(label="Tópicos previstos")
|
| 197 |
classify_btn.click(fn=classify_display, inputs=input_text, outputs=output)
|
| 198 |
|
| 199 |
+
# Sugestões
|
| 200 |
+
prev_btn = gr.Button("⟨")
|
| 201 |
+
suggestion_display = gr.Textbox(value=suggestions[0], interactive=False)
|
| 202 |
+
next_btn = gr.Button("⟩")
|
| 203 |
+
|
| 204 |
+
prev_btn.click(fn=prev_example, outputs=[input_text, suggestion_display])
|
| 205 |
+
next_btn.click(fn=next_example, outputs=[input_text, suggestion_display])
|
| 206 |
+
|
| 207 |
+
gr.Row([prev_btn, suggestion_display, next_btn], elem_id="suggestion-box")
|
|
|
|
| 208 |
|
| 209 |
+
# ---------------- Launch ----------------
|
| 210 |
if __name__ == "__main__":
|
| 211 |
demo.launch()
|