Upload app.ipynb
Browse files
app.ipynb
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"nbformat": 4,
|
| 3 |
+
"nbformat_minor": 0,
|
| 4 |
+
"metadata": {
|
| 5 |
+
"colab": {
|
| 6 |
+
"provenance": []
|
| 7 |
+
},
|
| 8 |
+
"kernelspec": {
|
| 9 |
+
"name": "python3",
|
| 10 |
+
"display_name": "Python 3"
|
| 11 |
+
},
|
| 12 |
+
"language_info": {
|
| 13 |
+
"name": "python"
|
| 14 |
+
}
|
| 15 |
+
},
|
| 16 |
+
"cells": [
|
| 17 |
+
{
|
| 18 |
+
"cell_type": "code",
|
| 19 |
+
"execution_count": 1,
|
| 20 |
+
"metadata": {
|
| 21 |
+
"colab": {
|
| 22 |
+
"base_uri": "https://localhost:8080/",
|
| 23 |
+
"height": 764
|
| 24 |
+
},
|
| 25 |
+
"id": "NfhpwnF_ZFT2",
|
| 26 |
+
"outputId": "4b1a8307-1484-4661-96de-747c277bd12d"
|
| 27 |
+
},
|
| 28 |
+
"outputs": [
|
| 29 |
+
{
|
| 30 |
+
"output_type": "stream",
|
| 31 |
+
"name": "stderr",
|
| 32 |
+
"text": [
|
| 33 |
+
"/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning: \n",
|
| 34 |
+
"The secret `HF_TOKEN` does not exist in your Colab secrets.\n",
|
| 35 |
+
"To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.\n",
|
| 36 |
+
"You will be able to reuse this secret in all of your notebooks.\n",
|
| 37 |
+
"Please note that authentication is recommended but still optional to access public models or datasets.\n",
|
| 38 |
+
" warnings.warn(\n"
|
| 39 |
+
]
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"output_type": "stream",
|
| 43 |
+
"name": "stdout",
|
| 44 |
+
"text": [
|
| 45 |
+
"nama-user-anda/nama-model-spam-anda is not a local folder and is not a valid model identifier listed on 'https://huggingface.co/models'\n",
|
| 46 |
+
"If this is a private repository, make sure to pass a token having permission to this repo either by logging in with `huggingface-cli login` or by passing `token=<your_token>`\n",
|
| 47 |
+
"It looks like you are running Gradio on a hosted a Jupyter notebook. For the Gradio app to work, sharing must be enabled. Automatically setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
|
| 50 |
+
"* Running on public URL: https://07f424aa3bc95e504a.gradio.live\n",
|
| 51 |
+
"\n",
|
| 52 |
+
"This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"output_type": "display_data",
|
| 57 |
+
"data": {
|
| 58 |
+
"text/plain": [
|
| 59 |
+
"<IPython.core.display.HTML object>"
|
| 60 |
+
],
|
| 61 |
+
"text/html": [
|
| 62 |
+
"<div><iframe src=\"https://07f424aa3bc95e504a.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
"metadata": {}
|
| 66 |
+
}
|
| 67 |
+
],
|
| 68 |
+
"source": [
|
| 69 |
+
"import gradio as gr\n",
|
| 70 |
+
"from transformers import pipeline\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"# 1. Muat pipeline klasifikasi teks dari model Anda di Hub\n",
|
| 73 |
+
"# Pastikan model Anda sudah publik atau Anda login di environment\n",
|
| 74 |
+
"try:\n",
|
| 75 |
+
" classifier = pipeline(\"text-classification\", model=\"nama-user-anda/nama-model-spam-anda\") # <-- GANTI INI\n",
|
| 76 |
+
"except Exception as e:\n",
|
| 77 |
+
" # Jika gagal, tampilkan error. Mungkin modelnya private atau salah nama.\n",
|
| 78 |
+
" print(e)\n",
|
| 79 |
+
" classifier = None\n",
|
| 80 |
+
"\n",
|
| 81 |
+
"# 2. Buat fungsi untuk prediksi\n",
|
| 82 |
+
"# Fungsi ini akan menerima input teks dari user dan mengembalikan output\n",
|
| 83 |
+
"def predict_spam(text):\n",
|
| 84 |
+
" if classifier is None:\n",
|
| 85 |
+
" return {\"Error\": \"Model tidak berhasil dimuat, periksa nama model di kode atau log di Space.\"}\n",
|
| 86 |
+
"\n",
|
| 87 |
+
" # Pipeline akan mengembalikan list berisi dictionary\n",
|
| 88 |
+
" # Contoh: [{'label': 'SPAM', 'score': 0.99...}]\n",
|
| 89 |
+
" prediction = classifier(text)\n",
|
| 90 |
+
"\n",
|
| 91 |
+
" # Kita format outputnya agar lebih mudah dibaca oleh Gradio\n",
|
| 92 |
+
" # Kita akan menggunakan komponen Label di Gradio, jadi kita return dictionary\n",
|
| 93 |
+
" return {p['label']: p['score'] for p in prediction}\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"# 3. Definisikan antarmuka Gradio\n",
|
| 96 |
+
"# Ini adalah bagian yang mengatur tampilan (input, output, judul, dll.)\n",
|
| 97 |
+
"demo = gr.Interface(\n",
|
| 98 |
+
" fn=predict_spam, # Fungsi yang akan dijalankan saat tombol \"Submit\" ditekan\n",
|
| 99 |
+
" inputs=gr.Textbox(lines=5, label=\"Text\", placeholder=\"Masukkan teks untuk diklasifikasikan...\"),\n",
|
| 100 |
+
" outputs=gr.Label(num_top_classes=2, label=\"Output\"), # gr.Label cocok untuk output klasifikasi\n",
|
| 101 |
+
" title=\"Klasifikasi Teks - Anti Spam Indonesia\",\n",
|
| 102 |
+
" description=\"Masukkan teks untuk diklasifikasikan oleh model hasil fine-tuning. Cocok untuk deteksi spam, penipuan, dll. Model oleh: [Nama Anda].\",\n",
|
| 103 |
+
" examples=[\n",
|
| 104 |
+
" [\"Selamat! Anda memenangkan undian hadiah 100 juta rupiah, klik link ini segera!\"],\n",
|
| 105 |
+
" [\"Pak, nanti sore jadi rapat ya di kantor jam 4.\"],\n",
|
| 106 |
+
" [\"Diskon besar! Beli 1 gratis 1 untuk semua produk elektronik. Kunjungi situs kami sekarang juga.\"]\n",
|
| 107 |
+
" ]\n",
|
| 108 |
+
")\n",
|
| 109 |
+
"\n",
|
| 110 |
+
"# 4. Luncurkan aplikasi\n",
|
| 111 |
+
"if __name__ == \"__main__\":\n",
|
| 112 |
+
" demo.launch()"
|
| 113 |
+
]
|
| 114 |
+
}
|
| 115 |
+
]
|
| 116 |
+
}
|