Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,21 +2,16 @@ import re
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
import os
|
| 5 |
-
import logging
|
| 6 |
|
| 7 |
# ======================
|
| 8 |
-
# ENV
|
| 9 |
# ======================
|
| 10 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 11 |
-
|
| 12 |
-
logging.basicConfig(
|
| 13 |
-
filename="debug.log",
|
| 14 |
-
level=logging.INFO,
|
| 15 |
-
format="%(asctime)s | %(message)s"
|
| 16 |
-
)
|
| 17 |
|
| 18 |
def log(msg):
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
# ======================
|
| 22 |
# MODEL (LOAD SEKALI)
|
|
@@ -106,7 +101,7 @@ def rating_usia(kategori):
|
|
| 106 |
return 0
|
| 107 |
|
| 108 |
# ======================
|
| 109 |
-
# CORE ANALYSIS + DEBUG
|
| 110 |
# ======================
|
| 111 |
def analyze_text(judul, isi):
|
| 112 |
kalimat = split_kalimat(isi)
|
|
@@ -114,55 +109,53 @@ def analyze_text(judul, isi):
|
|
| 114 |
|
| 115 |
detected = set()
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
f.write(f"JUDUL: {judul}\n")
|
| 120 |
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
|
| 133 |
-
|
| 134 |
-
f" - {kategori} | score={score:.3f} | threshold={threshold}\n"
|
| 135 |
-
)
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
|
| 145 |
-
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
|
| 162 |
return usia, ", ".join(sorted(detected))
|
| 163 |
|
| 164 |
# ======================
|
| 165 |
-
# GRADIO UI (
|
| 166 |
# ======================
|
| 167 |
demo = gr.Interface(
|
| 168 |
fn=analyze_text,
|
|
@@ -174,11 +167,11 @@ demo = gr.Interface(
|
|
| 174 |
gr.Number(label="Rating Usia"),
|
| 175 |
gr.Textbox(label="Kategori")
|
| 176 |
],
|
| 177 |
-
title="READEAR"
|
| 178 |
)
|
| 179 |
|
| 180 |
# ======================
|
| 181 |
-
# ENTRY POINT (
|
| 182 |
# ======================
|
| 183 |
if __name__ == "__main__":
|
| 184 |
demo.launch(
|
|
@@ -186,4 +179,4 @@ if __name__ == "__main__":
|
|
| 186 |
server_port=7860,
|
| 187 |
ssr_mode=False,
|
| 188 |
show_error=True
|
| 189 |
-
)
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# ======================
|
| 7 |
+
# ENV
|
| 8 |
# ======================
|
| 9 |
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
| 10 |
+
DEBUG = True # β MATIKAN kalau sudah selesai
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def log(msg):
|
| 13 |
+
if DEBUG:
|
| 14 |
+
print(msg, flush=True)
|
| 15 |
|
| 16 |
# ======================
|
| 17 |
# MODEL (LOAD SEKALI)
|
|
|
|
| 101 |
return 0
|
| 102 |
|
| 103 |
# ======================
|
| 104 |
+
# CORE ANALYSIS + DEBUG KE CONTAINER
|
| 105 |
# ======================
|
| 106 |
def analyze_text(judul, isi):
|
| 107 |
kalimat = split_kalimat(isi)
|
|
|
|
| 109 |
|
| 110 |
detected = set()
|
| 111 |
|
| 112 |
+
log("\n" + "=" * 80)
|
| 113 |
+
log(f"JUDUL: {judul}")
|
|
|
|
| 114 |
|
| 115 |
+
for w in windows:
|
| 116 |
+
log("\n[WINDOW]")
|
| 117 |
+
log(w)
|
| 118 |
|
| 119 |
+
for kategori, desc in SEMANTIC_EVENT.items():
|
| 120 |
+
res = classifier(
|
| 121 |
+
w,
|
| 122 |
+
desc,
|
| 123 |
+
hypothesis_template="Teks ini menggambarkan {}."
|
| 124 |
+
)
|
| 125 |
+
score = max(res["scores"])
|
| 126 |
+
threshold = THRESHOLD[kategori]
|
| 127 |
|
| 128 |
+
log(f" - {kategori} | score={score:.3f} | threshold={threshold}")
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
if score < threshold:
|
| 131 |
+
log(" β FAIL: threshold")
|
| 132 |
+
continue
|
| 133 |
|
| 134 |
+
if score < 0.60:
|
| 135 |
+
comp = event_completeness(w)
|
| 136 |
+
meta = is_metaphor(w)
|
| 137 |
|
| 138 |
+
log(f" completeness={comp}, metaphor={meta}")
|
| 139 |
|
| 140 |
+
if comp < 2:
|
| 141 |
+
log(" β FAIL: completeness")
|
| 142 |
+
continue
|
| 143 |
+
if meta:
|
| 144 |
+
log(" β FAIL: metaphor")
|
| 145 |
+
continue
|
| 146 |
|
| 147 |
+
log(" β
ACCEPTED")
|
| 148 |
+
detected.add(kategori)
|
| 149 |
|
| 150 |
+
usia = rating_usia(detected)
|
| 151 |
+
log(f"\nFINAL KATEGORI: {list(detected)}")
|
| 152 |
+
log(f"RATING USIA: {usia}")
|
| 153 |
+
log("=" * 80)
|
| 154 |
|
| 155 |
return usia, ", ".join(sorted(detected))
|
| 156 |
|
| 157 |
# ======================
|
| 158 |
+
# GRADIO UI (JANGAN DIUBAH)
|
| 159 |
# ======================
|
| 160 |
demo = gr.Interface(
|
| 161 |
fn=analyze_text,
|
|
|
|
| 167 |
gr.Number(label="Rating Usia"),
|
| 168 |
gr.Textbox(label="Kategori")
|
| 169 |
],
|
| 170 |
+
title="READEAR β Semantic Content Rating"
|
| 171 |
)
|
| 172 |
|
| 173 |
# ======================
|
| 174 |
+
# ENTRY POINT (ANTI-RESTART HF)
|
| 175 |
# ======================
|
| 176 |
if __name__ == "__main__":
|
| 177 |
demo.launch(
|
|
|
|
| 179 |
server_port=7860,
|
| 180 |
ssr_mode=False,
|
| 181 |
show_error=True
|
| 182 |
+
)
|