{ "cells": [ { "cell_type": "markdown", "id": "53e9feaa-53de-4377-8d45-aa1f7264ae3a", "metadata": {}, "source": [ "### First Neccesary libararies needs to be loaded." ] }, { "cell_type": "code", "execution_count": 32, "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 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": 13, "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": "code", "execution_count": 4, "id": "8b27be80-1a6c-4c6c-a3ac-fe3b6f1378ae", "metadata": {}, "outputs": [], "source": [ "import tempfile" ] }, { "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": 32, "id": "215fd411-623f-43ce-8775-1bbd2c130b56", "metadata": { "jupyter": { "source_hidden": true } }, "outputs": [], "source": [ "def classify_batch(file):\n", " \"\"\"Process uploaded CSV/TXT file with multiple emails\"\"\"\n", " results = []\n", " if file.name.endswith('.csv'): # Handling the emails in CSV files format\n", " df = pd.read_csv(file)\n", " emails = df['email'].tolist() # we assume there's a column named 'email'\n", " for idx, email in enumerate(emails):\n", " prediction = spam_classifier(email)[0]\n", " results.append({\n", " \"email\": email[:50] + \"...\", # Truncate for display\n", " \"label\": \"SPAM\" if prediction[\"label\"] == \"LABEL_1\" else \"HAM\",\n", " \"confidence\": prediction[\"score\"]\n", " })\n", "\n", " ### Now we almost do the same thing but for text files (one email per line)\n", " elif file.name.endswith('.txt'):\n", " with open(file.name, 'r') as f:\n", " emails = f.readlines()\n", " for email in emails:\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", " ### --------------- Here we implemnt some condition for our uploaded files __________\n", " try:\n", " results = []\n", " if not file.name:\n", " raise gr.Error(\"No file uploaded\")\n", " # Handle CSV files\n", " if file.name.endswith('.csv'):\n", " df = pd.read_csv(file)\n", " if 'email' not in df.columns:\n", " raise gr.Error(\"CSV file must contain 'email' column\")\n", " emails = df['email'].tolist()\n", " \n", " # Handle text files\n", " elif file.name.endswith('.txt'):\n", " with open(file.name, 'r') as f:\n", " emails = f.readlines()\n", " else:\n", " raise gr.Error(\"Unsupported file format. Only CSV/TXT accepted\")\n", " \n", " # Limit to 100 emails max for demo\n", " emails = emails[:100]\n", "\n", "\n", " except gr.Error as e:\n", " raise e # Re-raise Gradio errors to show pop-up\n", " except Exception as e:\n", " raise gr.Error(f\"An unexpected error occurred: {str(e)}\")\n", "\n", "\n", " return pd.DataFrame(results)" ] }, { "cell_type": "code", "execution_count": 27, "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": 28, "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": "code", "execution_count": 33, "id": "c428e83a-dbe6-4c91-8a05-5b550652c181", "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", "To disable this warning, you can either:\n", "\t- Avoid using `tokenizers` before the fork if possible\n", "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7867\n", "Caching examples at: '/Users/techgarage/Projects/spamedar/.gradio/cached_examples/318'\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "with gr.Blocks(title=\"Spam Classifier Pro\") as demo:\n", " gr.Markdown(\"# 📧 Spam Classification System\")\n", " \n", " with gr.Tab(\"Single Email\"):\n", " gr.Interface(\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", " [\"Meeting rescheduled to Friday 2 PM\"]\n", " ]\n", " )\n", " current_dir = os.getcwd()\n", " with gr.Tab(\"Batch Processing\"):\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\"\"\"\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]\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": "4559470b-1356-4f9d-b977-44bfbe117f3d", "metadata": {}, "source": [ "### using gradio we will make a simple interface for our program" ] }, { "cell_type": "code", "execution_count": 21, "id": "dfa7f58b-0ab8-445e-bfab-1396f2443033", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 17, "id": "67927628-4ca2-43ac-80c3-a1f9d4771d5d", "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", "To disable this warning, you can either:\n", "\t- Avoid using `tokenizers` before the fork if possible\n", "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "* Running on local URL: http://127.0.0.1:7863\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "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", " 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", " \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", " 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", " results_df = classify_batch(file)\n", " \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", " \n", " file_input.upload(\n", " fn=process_file,\n", " inputs=file_input,\n", " outputs=[output_table, download_btn] # Update both components\n", " )\n", " \n", " gr.Examples(\n", " examples=[\n", " [\"sample_emails.csv\"],\n", " [\"batch_emails.txt\"]\n", " ],\n", " inputs=file_input\n", " )\n", "if __name__ == \"__main__\":\n", " demo.launch()" ] }, { "cell_type": "code", "execution_count": 17, "id": "188e3c31-38ef-4191-8b24-5487724466bd", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true, "source_hidden": true } }, "outputs": [ { "ename": "FileNotFoundError", "evalue": "[Errno 2] No such file or directory: 'sample_emails.csv'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[17], line 31\u001b[0m\n\u001b[1;32m 23\u001b[0m download_btn \u001b[38;5;241m=\u001b[39m gr\u001b[38;5;241m.\u001b[39mFile(label\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mDownload Results\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 25\u001b[0m file_input\u001b[38;5;241m.\u001b[39mupload(\n\u001b[1;32m 26\u001b[0m fn\u001b[38;5;241m=\u001b[39mclassify_batch,\n\u001b[1;32m 27\u001b[0m inputs\u001b[38;5;241m=\u001b[39mfile_input,\n\u001b[1;32m 28\u001b[0m outputs\u001b[38;5;241m=\u001b[39moutput_table\n\u001b[1;32m 29\u001b[0m )\n\u001b[0;32m---> 31\u001b[0m \u001b[43mgr\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mExamples\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 32\u001b[0m \u001b[43m \u001b[49m\u001b[43mexamples\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m[\u001b[49m\n\u001b[1;32m 33\u001b[0m \u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43msample_emails.csv\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 34\u001b[0m \u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mbatch_emails.txt\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\n\u001b[1;32m 35\u001b[0m \u001b[43m \u001b[49m\u001b[43m]\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 36\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfile_input\u001b[49m\n\u001b[1;32m 37\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 39\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 40\u001b[0m demo\u001b[38;5;241m.\u001b[39mlaunch()\n", "File \u001b[0;32m~/anaconda3/envs/grad/lib/python3.10/site-packages/gradio/helpers.py:57\u001b[0m, in \u001b[0;36mcreate_examples\u001b[0;34m(examples, inputs, outputs, fn, cache_examples, cache_mode, examples_per_page, _api_mode, label, elem_id, run_on_click, preprocess, postprocess, api_name, batch, example_labels, visible)\u001b[0m\n\u001b[1;32m 36\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mcreate_examples\u001b[39m(\n\u001b[1;32m 37\u001b[0m examples: \u001b[38;5;28mlist\u001b[39m[Any] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mlist\u001b[39m[\u001b[38;5;28mlist\u001b[39m[Any]] \u001b[38;5;241m|\u001b[39m \u001b[38;5;28mstr\u001b[39m,\n\u001b[1;32m 38\u001b[0m inputs: Component \u001b[38;5;241m|\u001b[39m Sequence[Component],\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 54\u001b[0m visible: \u001b[38;5;28mbool\u001b[39m \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;01mTrue\u001b[39;00m,\n\u001b[1;32m 55\u001b[0m ):\n\u001b[1;32m 56\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"Top-level synchronous function that creates Examples. Provided for backwards compatibility, i.e. so that gr.Examples(...) can be used to create the Examples component.\"\"\"\u001b[39;00m\n\u001b[0;32m---> 57\u001b[0m examples_obj \u001b[38;5;241m=\u001b[39m \u001b[43mExamples\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 58\u001b[0m \u001b[43m \u001b[49m\u001b[43mexamples\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexamples\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 59\u001b[0m \u001b[43m \u001b[49m\u001b[43minputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43minputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 60\u001b[0m \u001b[43m \u001b[49m\u001b[43moutputs\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43moutputs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 61\u001b[0m \u001b[43m \u001b[49m\u001b[43mfn\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfn\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 62\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_examples\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_examples\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 63\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_mode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_mode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 64\u001b[0m \u001b[43m \u001b[49m\u001b[43mexamples_per_page\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexamples_per_page\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 65\u001b[0m \u001b[43m \u001b[49m\u001b[43m_api_mode\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m_api_mode\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 66\u001b[0m \u001b[43m \u001b[49m\u001b[43mlabel\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mlabel\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 67\u001b[0m \u001b[43m \u001b[49m\u001b[43melem_id\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43melem_id\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 68\u001b[0m \u001b[43m \u001b[49m\u001b[43mrun_on_click\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mrun_on_click\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 69\u001b[0m \u001b[43m \u001b[49m\u001b[43mpreprocess\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpreprocess\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 70\u001b[0m \u001b[43m \u001b[49m\u001b[43mpostprocess\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mpostprocess\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 71\u001b[0m \u001b[43m \u001b[49m\u001b[43mapi_name\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mapi_name\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 72\u001b[0m \u001b[43m \u001b[49m\u001b[43mbatch\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mbatch\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 73\u001b[0m \u001b[43m \u001b[49m\u001b[43mexample_labels\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mexample_labels\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 74\u001b[0m \u001b[43m \u001b[49m\u001b[43mvisible\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mvisible\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 75\u001b[0m \u001b[43m \u001b[49m\u001b[43m_initiated_directly\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mFalse\u001b[39;49;00m\u001b[43m,\u001b[49m\n\u001b[1;32m 76\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 77\u001b[0m examples_obj\u001b[38;5;241m.\u001b[39mcreate()\n\u001b[1;32m 78\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m examples_obj\n", "File \u001b[0;32m~/anaconda3/envs/grad/lib/python3.10/site-packages/gradio/helpers.py:294\u001b[0m, in \u001b[0;36mExamples.__init__\u001b[0;34m(self, examples, inputs, outputs, fn, cache_examples, cache_mode, examples_per_page, _api_mode, label, elem_id, run_on_click, preprocess, postprocess, api_name, batch, example_labels, visible, _initiated_directly)\u001b[0m\n\u001b[1;32m 291\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdataset\u001b[38;5;241m.\u001b[39msamples:\n\u001b[1;32m 292\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m index, example \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28menumerate\u001b[39m(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnon_none_examples):\n\u001b[1;32m 293\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mnon_none_processed_examples[\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdataset\u001b[38;5;241m.\u001b[39msamples[index]] \u001b[38;5;241m=\u001b[39m (\n\u001b[0;32m--> 294\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_get_processed_example\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexample\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 295\u001b[0m )\n\u001b[1;32m 297\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcache_examples \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlazy\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[1;32m 298\u001b[0m \u001b[38;5;28mprint\u001b[39m(\n\u001b[1;32m 299\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWill cache examples in \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m{\u001b[39;00mutils\u001b[38;5;241m.\u001b[39mabspath(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mcached_folder)\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m directory at first use.\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 300\u001b[0m end\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m\"\u001b[39m,\n\u001b[1;32m 301\u001b[0m )\n", "File \u001b[0;32m~/anaconda3/envs/grad/lib/python3.10/site-packages/gradio/helpers.py:328\u001b[0m, in \u001b[0;36mExamples._get_processed_example\u001b[0;34m(self, example)\u001b[0m\n\u001b[1;32m 324\u001b[0m sub \u001b[38;5;241m=\u001b[39m []\n\u001b[1;32m 325\u001b[0m \u001b[38;5;28;01mfor\u001b[39;00m component, sample \u001b[38;5;129;01min\u001b[39;00m \u001b[38;5;28mzip\u001b[39m(\n\u001b[1;32m 326\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39minputs_with_examples, example, strict\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mFalse\u001b[39;00m\n\u001b[1;32m 327\u001b[0m ):\n\u001b[0;32m--> 328\u001b[0m prediction_value \u001b[38;5;241m=\u001b[39m \u001b[43mcomponent\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mpostprocess\u001b[49m\u001b[43m(\u001b[49m\u001b[43msample\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 329\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(prediction_value, (GradioRootModel, GradioModel)):\n\u001b[1;32m 330\u001b[0m prediction_value \u001b[38;5;241m=\u001b[39m prediction_value\u001b[38;5;241m.\u001b[39mmodel_dump()\n", "File \u001b[0;32m~/anaconda3/envs/grad/lib/python3.10/site-packages/gradio/components/file.py:223\u001b[0m, in \u001b[0;36mFile.postprocess\u001b[0;34m(self, value)\u001b[0m\n\u001b[1;32m 209\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m ListFiles(\n\u001b[1;32m 210\u001b[0m root\u001b[38;5;241m=\u001b[39m[\n\u001b[1;32m 211\u001b[0m FileData(\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 217\u001b[0m ]\n\u001b[1;32m 218\u001b[0m )\n\u001b[1;32m 219\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 220\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m FileData(\n\u001b[1;32m 221\u001b[0m path\u001b[38;5;241m=\u001b[39mvalue,\n\u001b[1;32m 222\u001b[0m orig_name\u001b[38;5;241m=\u001b[39mPath(value)\u001b[38;5;241m.\u001b[39mname,\n\u001b[0;32m--> 223\u001b[0m size\u001b[38;5;241m=\u001b[39m\u001b[43mPath\u001b[49m\u001b[43m(\u001b[49m\u001b[43mvalue\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstat\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241m.\u001b[39mst_size,\n\u001b[1;32m 224\u001b[0m )\n", "File \u001b[0;32m~/anaconda3/envs/grad/lib/python3.10/pathlib.py:1097\u001b[0m, in \u001b[0;36mPath.stat\u001b[0;34m(self, follow_symlinks)\u001b[0m\n\u001b[1;32m 1092\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;21mstat\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;241m*\u001b[39m, follow_symlinks\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mTrue\u001b[39;00m):\n\u001b[1;32m 1093\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 1094\u001b[0m \u001b[38;5;124;03m Return the result of the stat() system call on this path, like\u001b[39;00m\n\u001b[1;32m 1095\u001b[0m \u001b[38;5;124;03m os.stat() does.\u001b[39;00m\n\u001b[1;32m 1096\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m-> 1097\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_accessor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mstat\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mfollow_symlinks\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mfollow_symlinks\u001b[49m\u001b[43m)\u001b[49m\n", "\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'sample_emails.csv'" ] } ], "source": [ "demo = gr.Interface(\n", " fn=classify_text,\n", " inputs=gr.Textbox(label=\"Email/Message\", placeholder=\"Enter text here...\"),\n", " outputs=gr.Label(label=\"Classification Results\"),\n", " title=\"Spamedar\",\n", " description=\"Copy your email to find out if it's a is Spam or Ham.👇\",\n", " examples=[\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", " ]\n", ")\n", "\n", "demo.launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "73be7578-bc18-4af7-8a00-52b6ee4b21e9", "metadata": {}, "outputs": [], "source": [] } ], "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 }