File size: 2,549 Bytes
16813ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
{
  "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": 23,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Ee3EGX_zl1Ei",
        "outputId": "13d12370-40e9-4fd6-a77c-6fc3721d2727"
      },
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Model saved as iris_knn.pkl\n"
          ]
        }
      ],
      "source": [
        "from sklearn.datasets import load_iris\n",
        "from sklearn.neighbors import KNeighborsClassifier\n",
        "import joblib\n",
        "\n",
        "# Load dataset\n",
        "iris = load_iris()\n",
        "X = iris.data\n",
        "y = iris.target\n",
        "\n",
        "# Train KNN\n",
        "model = KNeighborsClassifier(n_neighbors=5)\n",
        "model.fit(X, y)\n",
        "\n",
        "# Save model\n",
        "joblib.dump((model, iris.target_names), \"iris_knn.pkl\")\n",
        "\n",
        "print(\"Model saved as iris_knn.pkl\")\n"
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "import requests\n",
        "\n",
        "url = \"https://tofighi-iris-detector-api.hf.space/predict\"\n",
        "\n",
        "data = {\n",
        "    \"sepal_length\": 1.4,\n",
        "    \"sepal_width\": 1.3,\n",
        "    \"petal_length\": 2.4,\n",
        "    \"petal_width\": 1\n",
        "}\n",
        "\n",
        "resp = requests.post(url, json=data)\n",
        "print(resp.json())"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "wMe3AXJVt70u",
        "outputId": "9c01d308-679a-461f-f535-ef98851f944d"
      },
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "{'predicted_class': 'setosa', 'input': {'sepal_length': 1.4, 'sepal_width': 1.3, 'petal_length': 2.4, 'petal_width': 1.0}, 'class_probabilities': {'setosa': 0.8, 'versicolor': 0.2, 'virginica': 0.0}}\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "yYAcMc_Zyqpa"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}