File size: 11,165 Bytes
477b130
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "53e9feaa-53de-4377-8d45-aa1f7264ae3a",
   "metadata": {},
   "source": [
    "### First Neccesary libararies needs to be loaded."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8864f53f-15d2-403c-a905-3da509cfb050",
   "metadata": {},
   "outputs": [],
   "source": [
    "import gradio as gr\n",
    "import transformers\n",
    "from transformers import pipeline\n",
    "import tf_keras as keras\n",
    "import pandas as pd\n",
    "import tempfile\n",
    "import os"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5bb130f0-d8c8-459d-918f-84025c93bc05",
   "metadata": {},
   "source": [
    "### Now we import our already pre-trained model from "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1c4e29a8-9b24-47d6-b14b-0e2a7e4d66a1",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of the PyTorch model were not used when initializing the TF 2.0 model TFBertForSequenceClassification: ['bert.embeddings.position_ids']\n",
      "- This IS expected if you are initializing TFBertForSequenceClassification from a PyTorch model trained on another task or with another architecture (e.g. initializing a TFBertForSequenceClassification model from a BertForPreTraining model).\n",
      "- This IS NOT expected if you are initializing TFBertForSequenceClassification from a PyTorch model that you expect to be exactly identical (e.g. initializing a TFBertForSequenceClassification model from a BertForSequenceClassification model).\n",
      "All the weights of TFBertForSequenceClassification were initialized from the PyTorch model.\n",
      "If your task is similar to the task the model of the checkpoint was trained on, you can already use TFBertForSequenceClassification for predictions without further training.\n",
      "Device set to use 0\n"
     ]
    }
   ],
   "source": [
    "# Load pre-trained spam classifier\n",
    "spam_classifier = pipeline(\n",
    "    \"text-classification\",\n",
    "    model=\"mrm8488/bert-tiny-finetuned-sms-spam-detection\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9cb4ab66-2833-40bd-87cb-4d712398e431",
   "metadata": {},
   "source": [
    "### Since single email check is hassle we will make a function for batch classication\n",
    "### we should assume certain file template or format so our program knows what to expect"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "b8ae7b3b-5273-4242-85db-b5cb622a4046",
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify_batch(file):\n",
    "    \"\"\"Process uploaded CSV/TXT file with multiple emails\"\"\"\n",
    "    try:\n",
    "        results = []\n",
    "        \n",
    "        # Check if file exists\n",
    "        if not file.name:\n",
    "            raise gr.Error(\"No file uploaded\")\n",
    "\n",
    "        # --- CSV File Handling ---\n",
    "        if file.name.endswith('.csv'):\n",
    "            df = pd.read_csv(file)\n",
    "            \n",
    "            # Check for required email column\n",
    "            if 'email' not in df.columns:\n",
    "                raise gr.Error(\"CSV file must contain a column named 'email'\")\n",
    "                \n",
    "            emails = df['email'].tolist()\n",
    "\n",
    "        # --- Text File Handling ---\n",
    "        elif file.name.endswith('.txt'):\n",
    "            with open(file.name, 'r') as f:\n",
    "                emails = f.readlines()\n",
    "        \n",
    "        # --- Unsupported Format ---\n",
    "        else:\n",
    "            raise gr.Error(\"Unsupported file format. Only CSV/TXT accepted\")\n",
    "\n",
    "        # Process emails (common for both formats)\n",
    "        emails = emails[:100]  # Limit to 100 emails\n",
    "        for email in emails:\n",
    "            # Handle empty lines in text files\n",
    "            if not email.strip():\n",
    "                continue\n",
    "                \n",
    "            prediction = spam_classifier(email.strip())[0]\n",
    "            results.append({\n",
    "                \"email\": email.strip()[:50] + \"...\",\n",
    "                \"label\": \"SPAM\" if prediction[\"label\"] == \"LABEL_1\" else \"HAM\",\n",
    "                \"confidence\": f\"{prediction['score']:.4f}\"\n",
    "            })\n",
    "\n",
    "        return pd.DataFrame(results)\n",
    "\n",
    "    except gr.Error as e:\n",
    "        raise e  # Show pop-up for expected errors\n",
    "    except Exception as e:\n",
    "        raise gr.Error(f\"Processing error: {str(e)}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6ccb5108-a5d4-4f61-b363-dc4c9d25b4fb",
   "metadata": {},
   "source": [
    "### We define simple function for classification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1336344b-54c3-431d-8d89-c351b0c24f80",
   "metadata": {},
   "outputs": [],
   "source": [
    "def classify_text(text):\n",
    "    result = spam_classifier(text)[0]\n",
    "    return {\n",
    "        \"Spam\": result[\"score\"] if result[\"label\"] == \"LABEL_1\" else 1 - result[\"score\"],\n",
    "        \"Ham\": result[\"score\"] if result[\"label\"] == \"LABEL_0\" else 1 - result[\"score\"]\n",
    "    }"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4559470b-1356-4f9d-b977-44bfbe117f3d",
   "metadata": {},
   "source": [
    "### using gradio we will make a simple interface for our program"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "67927628-4ca2-43ac-80c3-a1f9d4771d5d",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "* Running on local URL:  http://127.0.0.1:7868\n",
      "Caching examples at: '/Users/techgarage/Projects/spamedar/.gradio/cached_examples/143'\n",
      "\n",
      "To create a public link, set `share=True` in `launch()`.\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div><iframe src=\"http://127.0.0.1:7868/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with gr.Blocks(title=\"Spam Classifier Pro\") as demo:\n",
    "    gr.Markdown(\"# πŸ“§ Welcome to Spamedar!\")\n",
    "    \n",
    "    \n",
    "    with gr.Tab(\"βœ‰οΈ Single Email\"):\n",
    "        gr.Interface(\n",
    "            description=\"<h2>Copy your email to find out if it's a is Spam or HamπŸ‘‡<h2>\",\n",
    "            fn=classify_text,\n",
    "            inputs=gr.Textbox(label=\"Input Email\", lines=3),\n",
    "            outputs=gr.Label(label=\"Classification\"),\n",
    "            examples=[\n",
    "                [\"Urgent: Verify your account details now!\"],\n",
    "                [\"Hey, can we meet tomorrow to discuss the project?\"],\n",
    "                [\"WINNER! You've been selected for a $1000 Walmart Gift Card!\"],\n",
    "                [\"Your account needs verification. Click here to confirm your details.\"],\n",
    "                [\"Meeting rescheduled to Friday 2 PM\"]\n",
    "            ]\n",
    "        )\n",
    "    current_dir = os.getcwd()\n",
    "    with gr.Tab(\"πŸ“¨ Multiple Emails\"):\n",
    "        gr.Markdown(\"## Upload email batch (CSV or TXT)\")\n",
    "        file_input = gr.File(label=\"Upload File\", file_types=[\".csv\", \".txt\"])\n",
    "        clear_btn = gr.Button(\"Clear Selection\", variant=\"secondary\")\n",
    "        output_table = gr.Dataframe(\n",
    "            headers=[\"email\", \"label\", \"confidence\"],\n",
    "            datatype=[\"str\", \"str\", \"number\"],\n",
    "            interactive=False,\n",
    "            label=\"Classification Results\"\n",
    "        )\n",
    "        download_btn = gr.DownloadButton(label=\"Download Results\")\n",
    "\n",
    "        def process_file(file):\n",
    "            \"\"\"Process file and return (display_df, download_path)\"\"\"\n",
    "            try:\n",
    "                if file is None:\n",
    "                    return pd.DataFrame(), None\n",
    "\n",
    "                results_df = classify_batch(file)\n",
    "                with tempfile.NamedTemporaryFile(suffix=\".csv\", delete=False) as f:\n",
    "                    results_df.to_csv(f.name, index=False)\n",
    "                    return results_df, f.name\n",
    "            except Exception as e:\n",
    "                raise gr.Error(f\"Error processing file: {str(e)}\")\n",
    "\n",
    "        def clear_selection():\n",
    "            ###clear file input and results function\n",
    "            return None, pd.DataFrame(), None\n",
    "        \n",
    "        file_input.upload(\n",
    "            fn=process_file,\n",
    "            inputs=file_input,\n",
    "            outputs=[output_table, download_btn]  # Update both components\n",
    "        )\n",
    "\n",
    "        clear_btn.click(\n",
    "            fn=clear_selection,\n",
    "            outputs=[file_input, output_table, download_btn]\n",
    "        )\n",
    "\n",
    "        example_files= [\n",
    "            os.path.join(os.getcwd(), \"sample_emails.csv\"),\n",
    "            os.path.join(os.getcwd(), \"batch_emails.txt\"),\n",
    "        ]\n",
    "        if all(os.path.exists(f) for f in example_files):\n",
    "            gr.Examples(\n",
    "                examples=[[f] for f in example_files],\n",
    "                inputs=file_input,\n",
    "                outputs=[output_table, download_btn],\n",
    "                fn=process_file,\n",
    "                cache_examples=True,\n",
    "                label=\"Click any example below to test:\"\n",
    "            )\n",
    "            \n",
    "        else:\n",
    "            print(\"Warning: Example files missing. Place these in your project root:\")\n",
    "            print(\"- sample_emails.csv\")\n",
    "            print(\"- batch_emails.txt\")\n",
    "        \n",
    "if __name__ == \"__main__\":\n",
    "    demo.launch()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18c2a4bd-0404-46ec-87b1-4f47b5802150",
   "metadata": {},
   "source": [
    "### Thank you for following the guide until the end.πŸš€πŸ‘Ύ\n",
    "code: Raouf Jivad(with a lil help of GPT πŸ˜ƒ)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}