import os import json import re import csv import io from flask import Flask, request, jsonify, render_template_string from transformers import AutoTokenizer, AutoModelForSequenceClassification import torch app = Flask(__name__) MODEL_PATH = "./hate_speech_model" tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH) model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH) threshold = 0.5 if os.path.exists(os.path.join(MODEL_PATH, "training_config.json")): try: with open(os.path.join(MODEL_PATH, "training_config.json"), "r") as f: config = json.load(f) threshold = config.get("optimal_threshold", 0.5) print(f"Loaded optimal threshold from config: {threshold:.4f}") except Exception as e: print(f"Gagal memuat training_config.json: {e}. Menggunakan threshold default 0.5") dataset_cache = {"data": None, "loaded": False} def preprocess_text(text): text = re.sub(r'@[^\s]+', '@USER', text) text = re.sub(r'https?://[^\s]+', 'HTTPURL', text) text = re.sub(r'\s+', ' ', text).strip() return text WEB_UI_HTML = r"""
Klasifikasi teks ujaran kebencian
Memuat dataset IndoDiscourse dari HuggingFace...