rblueeyes commited on
Commit
eb3e6ed
Β·
verified Β·
1 Parent(s): 7ce5e15

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -34
app.py CHANGED
@@ -16,7 +16,7 @@ def log(msg):
16
  print(msg, flush=True)
17
 
18
  # ======================
19
- # MODEL 1 β€” RATING & KATEGORI (ASLI, TIDAK DIUBAH)
20
  # ======================
21
  classifier = pipeline(
22
  "zero-shot-classification",
@@ -40,7 +40,7 @@ abusive_model.to(device)
40
  id2label = {0: "Tidak Abusif", 1: "Abusif"}
41
 
42
  # ======================
43
- # CONFIG (ASLI β€” TIDAK DIUBAH)
44
  # ======================
45
  SEMANTIC_EVENT = {
46
  "sadisme": [
@@ -99,6 +99,22 @@ def split_kalimat(text):
99
  if len(k.strip()) >= 20
100
  ]
101
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  def rating_usia(kategori):
103
  if "sadisme" in kategori:
104
  return 21
@@ -109,13 +125,21 @@ def rating_usia(kategori):
109
  return 0
110
 
111
  # ======================
112
- # MODE 1 β€” RATING & KATEGORI (ASLI, TIDAK DIUBAH)
113
  # ======================
114
  def analyze_text(judul, isi):
115
  kalimat = split_kalimat(isi)
 
 
116
  detected = set()
117
 
118
- for w in kalimat:
 
 
 
 
 
 
119
  for kategori, desc in SEMANTIC_EVENT.items():
120
  res = classifier(
121
  w,
@@ -123,14 +147,26 @@ def analyze_text(judul, isi):
123
  hypothesis_template="Teks ini menggambarkan {}."
124
  )
125
  score = max(res["scores"])
126
- if score >= THRESHOLD[kategori]:
127
- detected.add(kategori)
 
 
 
 
 
 
 
 
128
 
129
  usia = rating_usia(detected)
 
 
 
 
130
  return usia, ", ".join(sorted(detected))
131
 
132
  # ======================
133
- # MODE 2 β€” FILTER ABUSIVE (BARU, TERPISAH)
134
  # ======================
135
  def filter_abusive(isi):
136
  paragraphs = re.split(r'\n+', isi)
@@ -170,9 +206,9 @@ def filter_abusive(isi):
170
  return "\n".join(output)
171
 
172
  # ======================
173
- # ROUTER (KHUSUS RAILWAY)
174
  # ======================
175
- def api_router(judul, isi, mode):
176
  if mode == "rating":
177
  usia, kategori = analyze_text(judul, isi)
178
  return {
@@ -190,46 +226,26 @@ def api_router(judul, isi, mode):
190
  }
191
 
192
  # ======================
193
- # UI ASLI (TIDAK DIUBAH)
194
  # ======================
195
  demo = gr.Interface(
196
- fn=analyze_text,
197
- inputs=[
198
- gr.Textbox(label="Judul"),
199
- gr.Textbox(label="Isi Cerita", lines=10)
200
- ],
201
- outputs=[
202
- gr.Number(label="Rating Usia"),
203
- gr.Textbox(label="Kategori")
204
- ],
205
- title="READEAR",
206
- api_name="analyze"
207
- )
208
-
209
- # ======================
210
- # API UNTUK RAILWAY
211
- # ======================
212
- api = gr.Interface(
213
- fn=api_router,
214
  inputs=[
215
  gr.Textbox(),
216
  gr.Textbox(),
217
  gr.Radio(["rating", "abusive"])
218
  ],
219
  outputs=gr.JSON(),
220
- api_name="predict"
221
  )
222
 
223
  # ======================
224
  # ENTRY POINT
225
  # ======================
226
  if __name__ == "__main__":
227
- gr.TabbedInterface(
228
- [demo, api],
229
- ["UI", "API"]
230
- ).launch(
231
  server_name="0.0.0.0",
232
  server_port=7860,
233
  ssr_mode=False,
234
  show_error=True
235
- )
 
16
  print(msg, flush=True)
17
 
18
  # ======================
19
+ # MODEL 1 β€” RATING & KATEGORI (ASLI)
20
  # ======================
21
  classifier = pipeline(
22
  "zero-shot-classification",
 
40
  id2label = {0: "Tidak Abusif", 1: "Abusif"}
41
 
42
  # ======================
43
+ # CONFIG (ASLI)
44
  # ======================
45
  SEMANTIC_EVENT = {
46
  "sadisme": [
 
99
  if len(k.strip()) >= 20
100
  ]
101
 
102
+ def sliding_window(kalimat, window=4):
103
+ if len(kalimat) < window:
104
+ return kalimat
105
+ return [
106
+ " ".join(kalimat[i:i+window])
107
+ for i in range(len(kalimat) - window + 1)
108
+ ]
109
+
110
+ def event_completeness(text):
111
+ text = text.lower()
112
+ return sum(any(w in text for w in words) for words in EVENT_UNITS.values())
113
+
114
+ def is_metaphor(text):
115
+ text = text.lower()
116
+ return any(a in text and b in text for a, b in METAPHOR_PAIRS)
117
+
118
  def rating_usia(kategori):
119
  if "sadisme" in kategori:
120
  return 21
 
125
  return 0
126
 
127
  # ======================
128
+ # CORE ANALYSIS + DEBUG (JANGAN DIUBAH)
129
  # ======================
130
  def analyze_text(judul, isi):
131
  kalimat = split_kalimat(isi)
132
+ windows = sliding_window(kalimat)
133
+
134
  detected = set()
135
 
136
+ log("\n" + "=" * 80)
137
+ log(f"JUDUL: {judul}")
138
+
139
+ for w in windows:
140
+ log("\n[WINDOW]")
141
+ log(w)
142
+
143
  for kategori, desc in SEMANTIC_EVENT.items():
144
  res = classifier(
145
  w,
 
147
  hypothesis_template="Teks ini menggambarkan {}."
148
  )
149
  score = max(res["scores"])
150
+ threshold = THRESHOLD[kategori]
151
+
152
+ log(f" - {kategori} | score={score:.3f} | threshold={threshold}")
153
+
154
+ if score < threshold:
155
+ log(" ❌ FAIL: threshold")
156
+ continue
157
+
158
+ log(" βœ… ACCEPTED")
159
+ detected.add(kategori)
160
 
161
  usia = rating_usia(detected)
162
+ log(f"\nFINAL KATEGORI: {list(detected)}")
163
+ log(f"RATING USIA: {usia}")
164
+ log("=" * 80)
165
+
166
  return usia, ", ".join(sorted(detected))
167
 
168
  # ======================
169
+ # FILTER ABUSIVE (TERPISAH, AMAN)
170
  # ======================
171
  def filter_abusive(isi):
172
  paragraphs = re.split(r'\n+', isi)
 
206
  return "\n".join(output)
207
 
208
  # ======================
209
+ # ROUTER /ANALYZE (MODE-BASED)
210
  # ======================
211
+ def analyze_router(judul, isi, mode):
212
  if mode == "rating":
213
  usia, kategori = analyze_text(judul, isi)
214
  return {
 
226
  }
227
 
228
  # ======================
229
+ # GRADIO API (ROUTE TETAP /analyze)
230
  # ======================
231
  demo = gr.Interface(
232
+ fn=analyze_router,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
233
  inputs=[
234
  gr.Textbox(),
235
  gr.Textbox(),
236
  gr.Radio(["rating", "abusive"])
237
  ],
238
  outputs=gr.JSON(),
239
+ api_name="analyze"
240
  )
241
 
242
  # ======================
243
  # ENTRY POINT
244
  # ======================
245
  if __name__ == "__main__":
246
+ demo.launch(
 
 
 
247
  server_name="0.0.0.0",
248
  server_port=7860,
249
  ssr_mode=False,
250
  show_error=True
251
+ )