{ "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": [] } ] }