Views
No views yet

1git clone https://huggingface.co/ludyhasby/hoax_sahih_AI
2cd hoax_sahih_AI
3pip install -r requirements.txt1import pickle
2import tensorflow as tf
3from tensorflow.keras.preprocessing.sequence import pad_sequences
4from huggingface_hub import hf_hub_download
5from statics import slang_dict2, stop_words, emoji_dict, max_length
6import re1def load_important(token_hf):
2 # --- Step 1. Load Model ---
3 model_path = hf_hub_download(
4 repo_id="ludyhasby/hoax_sahih_AI",
5 filename="hoax_detection.h5",
6 token=token_hf # masukkan token Anda
7 )
8 model = tf.keras.models.load_model(model_path, compile=False)
9
10 # --- Step 2. Load Tokenizer ---
11 tokenizer_path = hf_hub_download(
12 repo_id="ludyhasby/hoax_sahih_AI",
13 filename="tokenizer_hoax.pickle"
14 )
15 with open(tokenizer_path, "rb") as handle:
16 tokenizer = pickle.load(handle)
17 return model, tokenizer
18
19# --- Step 3. Preprocessing Things ---
20def replace_emot(teks, emoji_dict):
21 for j, emoticon in (emoji_dict["Emoji"].items()):
22 tag = emoji_dict["tag_indo"][j]
23 teks = teks.replace(emoticon, f" {tag}")
24 return teks
25
26def normalize_slang(text, slang_dict):
27 list_word = text.split()
28 for word in list_word:
29 for j, original in slang_dict['original'].items():
30 if word == original:
31 text = text.replace(word, slang_dict['replacement'][j])
32 return text
33
34def teks_to_pad(teks):
35 teks_seq = tokenizer.texts_to_sequences(teks)
36 teks_pad = pad_sequences(teks_seq, maxlen=max_length, truncating=trunc_type, padding=pad_type)
37 return teks_pad
38
39def preprocessing(text, emot_f, slang_dict, STOP_PREP):
40 text = replace_emot(text, emot_f)
41 text = re.sub(r'[^\w\s]', ' ', text)
42 text = text.lower()
43 text = re.sub(r'username', '', text)
44 text = normalize_slang(text, slang_dict)
45 print(text)
46 # Mengubah string menjadi list kata
47 words = text.split()
48 filtered_words = [word for word in words if word.lower() not in STOP_PREP]
49 text = ' '.join(filtered_words)
50 text = re.sub(r"\d+", "", text)
51 text = re.sub(r'[ ]+', ' ', text)
52
53 # Tokenizer
54 padded = teks_to_pad([text])
55 return padded
56
57def decode_label(encode):
58 if encode == 0:
59 return "Berita Kemungkinan Benar"
60 elif encode == 1:
61 return "Berita Kemungkinan Salah [Hoax]"
62 return "Out of Bound !"
63
64def main_inference(teks, emoji_dict, slang_dict, STOP_PREP):
65 padnya = preprocessing(teks, emoji_dict, slang_dict2, STOP_PREP)
66 pred = model.predict(padnya)
67 bin_result = (pred >= 0.5).astype(int)
68 probability = pred[0][0] if bin_result==1 else 1-pred[0][0]
69 print(decode_label(bin_result))
70 print(probability)1berita = input("Silahkan masukkan berita Anda: ")
2main_inference(berita, emoji_dict, slang_dict2, stop_words).
├── hoax_detection.h5 # Model terlatih
├── tokenizer_hoax.pickle # Tokenizer
├── hoax_inference.py # Script Contoh Penggunaan Siap Gunakan (Lokal)
├── requirements.txt
├── statics.py # Modul Statis yang diperlukan saat proses inferensia
├── logo_sahihAI.png
└── README.md