File size: 4,960 Bytes
dd1a135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "M8NNCivsv-38"
      },
      "outputs": [],
      "source": [
        "!pip install -q huggingface-hub joblib pandas scikit-learn matplotlib seaborn"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "from huggingface_hub import hf_hub_download\n",
        "import joblib, os, pandas as pd\n",
        "\n",
        "REPO_ID = \"dr-rakshith-truth-zeeker/truth-zeeker-ai-demo\"\n",
        "FNAME = \"model.joblib\"\n",
        "\n",
        "path = hf_hub_download(repo_id=REPO_ID, filename=FNAME)\n",
        "print(\"✅ Downloaded model from Hugging Face:\", path)\n",
        "\n",
        "model = joblib.load(path)\n",
        "print(\"Model loaded successfully:\", type(model))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4cRLizbOwmYH",
        "outputId": "e8c03cc1-4573-4d17-95aa-73955dbafc76"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "✅ Downloaded model from Hugging Face: /root/.cache/huggingface/hub/models--dr-rakshith-truth-zeeker--truth-zeeker-ai-demo/snapshots/c53c401c56ba50cac5bb805104dfe5ecf380d022/model.joblib\n",
            "Model loaded successfully: <class 'tuple'>\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "csv_url = \"https://raw.githubusercontent.com/dr-rakshith-truth-zeeker/Truth-Zeeker-AI/refs/heads/release/demo_data/sample_features.csv?token=GHSAT0AAAAAADNHI2OE4ZDLAJ26R6E2CJZY2HP242Q\"\n",
        "\n",
        "df = pd.read_csv(csv_url)\n",
        "print(\"Loaded demo data shape:\", df.shape)\n",
        "print(\"\\nFirst 10 rows:\\n\")\n",
        "print(df.head(10).to_string(index=False))\n",
        "\n",
        "# Run inference (handle tuple (scaler, model) or plain model)\n",
        "if isinstance(model, tuple) and len(model) == 2:\n",
        "    scaler, clf = model\n",
        "    Xs = scaler.transform(df[['duration','orig_bytes','resp_bytes']])\n",
        "    preds = clf.predict(Xs)\n",
        "else:\n",
        "    clf = model\n",
        "    preds = clf.predict(df[['duration','orig_bytes','resp_bytes']])\n",
        "\n",
        "df['anomaly_flag'] = preds\n",
        "print(\"\\nResults with anomaly_flag:\\n\")\n",
        "print(df.to_string(index=False))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4NqEdftzx-dN",
        "outputId": "dcff4174-28dc-4ea5-b900-18575399a712"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Loaded demo data shape: (10, 3)\n",
            "\n",
            "First 10 rows:\n",
            "\n",
            " duration  orig_bytes  resp_bytes\n",
            "     0.12         456         789\n",
            "     0.08         123          80\n",
            "     0.30        2000         150\n",
            "     0.05          60           0\n",
            "     0.90        4000        1200\n",
            "     0.02          50          10\n",
            "     0.20         300         400\n",
            "     0.25        1500        1200\n",
            "     0.15         800         200\n",
            "     0.07          90          70\n",
            "\n",
            "Results with anomaly_flag:\n",
            "\n",
            " duration  orig_bytes  resp_bytes  anomaly_flag\n",
            "     0.12         456         789             1\n",
            "     0.08         123          80             1\n",
            "     0.30        2000         150             1\n",
            "     0.05          60           0             1\n",
            "     0.90        4000        1200            -1\n",
            "     0.02          50          10             1\n",
            "     0.20         300         400             1\n",
            "     0.25        1500        1200            -1\n",
            "     0.15         800         200             1\n",
            "     0.07          90          70             1\n"
          ]
        },
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.12/dist-packages/sklearn/utils/validation.py:2732: UserWarning: X has feature names, but StandardScaler was fitted without feature names\n",
            "  warnings.warn(\n"
          ]
        }
      ]
    }
  ]
}