Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import torch.nn.functional as F
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
# ======================
|
| 9 |
-
# ENV
|
| 10 |
# ======================
|
| 11 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 12 |
DEBUG = True
|
|
@@ -16,7 +16,7 @@ def log(msg):
|
|
| 16 |
print(msg, flush=True)
|
| 17 |
|
| 18 |
# ======================
|
| 19 |
-
# MODEL 1 β RATING & KATEGORI (
|
| 20 |
# ======================
|
| 21 |
classifier = pipeline(
|
| 22 |
"zero-shot-classification",
|
|
@@ -26,7 +26,7 @@ classifier = pipeline(
|
|
| 26 |
)
|
| 27 |
|
| 28 |
# ======================
|
| 29 |
-
# MODEL 2 β ABUSIVE (
|
| 30 |
# ======================
|
| 31 |
ABUSIVE_MODEL_PATH = "./indoBERT_abusive"
|
| 32 |
|
|
@@ -90,7 +90,7 @@ THRESHOLD = {
|
|
| 90 |
}
|
| 91 |
|
| 92 |
# ======================
|
| 93 |
-
# UTIL UMUM
|
| 94 |
# ======================
|
| 95 |
def split_kalimat(text):
|
| 96 |
return [
|
|
@@ -109,9 +109,9 @@ def rating_usia(kategori):
|
|
| 109 |
return 0
|
| 110 |
|
| 111 |
# ======================
|
| 112 |
-
# MODE
|
| 113 |
# ======================
|
| 114 |
-
def
|
| 115 |
kalimat = split_kalimat(isi)
|
| 116 |
detected = set()
|
| 117 |
|
|
@@ -131,9 +131,9 @@ def analyze_rating(judul, isi):
|
|
| 131 |
return usia, ", ".join(sorted(detected))
|
| 132 |
|
| 133 |
# ======================
|
| 134 |
-
# MODE
|
| 135 |
# ======================
|
| 136 |
-
def
|
| 137 |
paragraphs = re.split(r'\n+', isi)
|
| 138 |
output = []
|
| 139 |
|
|
@@ -143,7 +143,6 @@ def detect_abusive(isi):
|
|
| 143 |
continue
|
| 144 |
|
| 145 |
sentences = re.split(r'(?<=[.!?])\s+', para)
|
| 146 |
-
abusive_found = False
|
| 147 |
abusive_notes = []
|
| 148 |
|
| 149 |
for s in sentences:
|
|
@@ -162,57 +161,46 @@ def detect_abusive(isi):
|
|
| 162 |
pred = torch.argmax(probs, dim=-1).item()
|
| 163 |
|
| 164 |
if id2label[pred] == "Abusif":
|
| 165 |
-
abusive_found = True
|
| 166 |
abusive_notes.append(
|
| 167 |
f'Kalimat "{s}" mengandung kalimat abusif, tidak baik diucapkan'
|
| 168 |
)
|
| 169 |
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
output.extend(abusive_notes)
|
| 173 |
-
else:
|
| 174 |
-
output.append(para)
|
| 175 |
|
| 176 |
return "\n".join(output)
|
| 177 |
|
| 178 |
# ======================
|
| 179 |
-
# ROUTER MODE (
|
| 180 |
# ======================
|
| 181 |
def router(judul, isi, mode):
|
| 182 |
if mode == "rating":
|
| 183 |
-
return
|
| 184 |
|
| 185 |
if mode == "abusive":
|
| 186 |
-
return
|
| 187 |
|
| 188 |
return "Invalid mode"
|
| 189 |
|
| 190 |
# ======================
|
| 191 |
-
# GRADIO
|
| 192 |
# ======================
|
| 193 |
demo = gr.Interface(
|
| 194 |
fn=router,
|
| 195 |
inputs=[
|
| 196 |
gr.Textbox(label="Judul"),
|
| 197 |
gr.Textbox(label="Isi"),
|
| 198 |
-
gr.Radio(
|
| 199 |
-
["rating", "abusive"],
|
| 200 |
-
label="Mode",
|
| 201 |
-
value="rating"
|
| 202 |
-
)
|
| 203 |
],
|
| 204 |
outputs=gr.JSON(),
|
| 205 |
-
|
| 206 |
-
|
| 207 |
)
|
| 208 |
|
| 209 |
-
# ======================
|
| 210 |
-
# ENTRY POINT
|
| 211 |
-
# ======================
|
| 212 |
if __name__ == "__main__":
|
| 213 |
demo.launch(
|
| 214 |
server_name="0.0.0.0",
|
| 215 |
server_port=7860,
|
| 216 |
ssr_mode=False,
|
| 217 |
show_error=True
|
| 218 |
-
)
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
# ======================
|
| 9 |
+
# ENV (ASLI)
|
| 10 |
# ======================
|
| 11 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 12 |
DEBUG = True
|
|
|
|
| 16 |
print(msg, flush=True)
|
| 17 |
|
| 18 |
# ======================
|
| 19 |
+
# MODEL 1 β RATING & KATEGORI (ASLI, JANGAN DISENTUH)
|
| 20 |
# ======================
|
| 21 |
classifier = pipeline(
|
| 22 |
"zero-shot-classification",
|
|
|
|
| 26 |
)
|
| 27 |
|
| 28 |
# ======================
|
| 29 |
+
# TAMBAHAN MODEL 2 β ABUSIVE (BARU)
|
| 30 |
# ======================
|
| 31 |
ABUSIVE_MODEL_PATH = "./indoBERT_abusive"
|
| 32 |
|
|
|
|
| 90 |
}
|
| 91 |
|
| 92 |
# ======================
|
| 93 |
+
# UTIL UMUM (ASLI)
|
| 94 |
# ======================
|
| 95 |
def split_kalimat(text):
|
| 96 |
return [
|
|
|
|
| 109 |
return 0
|
| 110 |
|
| 111 |
# ======================
|
| 112 |
+
# MODE LAMA β RATING & KATEGORI (ASLI)
|
| 113 |
# ======================
|
| 114 |
+
def analyze_text(judul, isi):
|
| 115 |
kalimat = split_kalimat(isi)
|
| 116 |
detected = set()
|
| 117 |
|
|
|
|
| 131 |
return usia, ", ".join(sorted(detected))
|
| 132 |
|
| 133 |
# ======================
|
| 134 |
+
# MODE BARU β FILTER ABUSIVE (TAMBAHAN SAJA)
|
| 135 |
# ======================
|
| 136 |
+
def filter_abusive_text(isi):
|
| 137 |
paragraphs = re.split(r'\n+', isi)
|
| 138 |
output = []
|
| 139 |
|
|
|
|
| 143 |
continue
|
| 144 |
|
| 145 |
sentences = re.split(r'(?<=[.!?])\s+', para)
|
|
|
|
| 146 |
abusive_notes = []
|
| 147 |
|
| 148 |
for s in sentences:
|
|
|
|
| 161 |
pred = torch.argmax(probs, dim=-1).item()
|
| 162 |
|
| 163 |
if id2label[pred] == "Abusif":
|
|
|
|
| 164 |
abusive_notes.append(
|
| 165 |
f'Kalimat "{s}" mengandung kalimat abusif, tidak baik diucapkan'
|
| 166 |
)
|
| 167 |
|
| 168 |
+
output.append(para)
|
| 169 |
+
output.extend(abusive_notes)
|
|
|
|
|
|
|
|
|
|
| 170 |
|
| 171 |
return "\n".join(output)
|
| 172 |
|
| 173 |
# ======================
|
| 174 |
+
# ROUTER MODE (SATU API, MODE BEDA)
|
| 175 |
# ======================
|
| 176 |
def router(judul, isi, mode):
|
| 177 |
if mode == "rating":
|
| 178 |
+
return analyze_text(judul, isi)
|
| 179 |
|
| 180 |
if mode == "abusive":
|
| 181 |
+
return filter_abusive_text(isi)
|
| 182 |
|
| 183 |
return "Invalid mode"
|
| 184 |
|
| 185 |
# ======================
|
| 186 |
+
# GRADIO (API TETAP SATU)
|
| 187 |
# ======================
|
| 188 |
demo = gr.Interface(
|
| 189 |
fn=router,
|
| 190 |
inputs=[
|
| 191 |
gr.Textbox(label="Judul"),
|
| 192 |
gr.Textbox(label="Isi"),
|
| 193 |
+
gr.Radio(["rating", "abusive"], value="rating")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
],
|
| 195 |
outputs=gr.JSON(),
|
| 196 |
+
api_name="predict",
|
| 197 |
+
title="READEAR"
|
| 198 |
)
|
| 199 |
|
|
|
|
|
|
|
|
|
|
| 200 |
if __name__ == "__main__":
|
| 201 |
demo.launch(
|
| 202 |
server_name="0.0.0.0",
|
| 203 |
server_port=7860,
|
| 204 |
ssr_mode=False,
|
| 205 |
show_error=True
|
| 206 |
+
)
|