Spaces:
Sleeping
Sleeping
Uday Chitragar commited on
Commit ·
1cfefea
1
Parent(s): 9ad74e8
stable
Browse files- Makefile +3 -3
- app.py +43 -0
- iris_train.py +27 -0
- requirements.txt +4 -0
- test_app.py +18 -0
Makefile
CHANGED
|
@@ -3,12 +3,12 @@ install:
|
|
| 3 |
pip install -r requirements.txt
|
| 4 |
|
| 5 |
test:
|
| 6 |
-
python -m pytest -vv
|
| 7 |
|
| 8 |
format:
|
| 9 |
black *.py
|
| 10 |
|
| 11 |
lint:
|
| 12 |
-
pylint --disable=R,C
|
| 13 |
|
| 14 |
-
all: install
|
|
|
|
| 3 |
pip install -r requirements.txt
|
| 4 |
|
| 5 |
test:
|
| 6 |
+
python -m pytest -vv test_app.py
|
| 7 |
|
| 8 |
format:
|
| 9 |
black *.py
|
| 10 |
|
| 11 |
lint:
|
| 12 |
+
pylint --disable=R,C iris_train.py
|
| 13 |
|
| 14 |
+
all: install test
|
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sklearn.datasets import load_iris
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.preprocessing import StandardScaler
|
| 5 |
+
from sklearn.pipeline import make_pipeline
|
| 6 |
+
from sklearn.linear_model import LogisticRegression
|
| 7 |
+
from sklearn.metrics import accuracy_score
|
| 8 |
+
|
| 9 |
+
# Load Iris dataset
|
| 10 |
+
iris = load_iris()
|
| 11 |
+
X = iris.data
|
| 12 |
+
y = iris.target
|
| 13 |
+
|
| 14 |
+
# Split dataset into training and testing sets
|
| 15 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 16 |
+
|
| 17 |
+
# Define model
|
| 18 |
+
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
|
| 19 |
+
|
| 20 |
+
# Train model
|
| 21 |
+
model.fit(X_train, y_train)
|
| 22 |
+
|
| 23 |
+
# Define a function for prediction
|
| 24 |
+
def predict_iris(sepal_length, sepal_width, petal_length, petal_width):
|
| 25 |
+
features = [[sepal_length, sepal_width, petal_length, petal_width]]
|
| 26 |
+
prediction = model.predict(features)
|
| 27 |
+
return iris.target_names[prediction[0]]
|
| 28 |
+
|
| 29 |
+
# Create Gradio interface
|
| 30 |
+
iris_interface = gr.Interface(
|
| 31 |
+
fn=predict_iris,
|
| 32 |
+
inputs=["number", "number", "number", "number"],
|
| 33 |
+
outputs="text",
|
| 34 |
+
title="Iris clarrification model",
|
| 35 |
+
description="Measurements --> species prediction.",
|
| 36 |
+
examples=[
|
| 37 |
+
[5.1, 3.5, 1.4, 0.2],
|
| 38 |
+
[6.9, 3.1, 5.4, 2.1]
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Launch the interface
|
| 43 |
+
iris_interface.launch()
|
iris_train.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.datasets import load_iris
|
| 2 |
+
from sklearn.model_selection import train_test_split
|
| 3 |
+
from sklearn.preprocessing import StandardScaler
|
| 4 |
+
from sklearn.pipeline import make_pipeline
|
| 5 |
+
from sklearn.linear_model import LogisticRegression
|
| 6 |
+
from sklearn.metrics import accuracy_score
|
| 7 |
+
|
| 8 |
+
# Load Iris dataset
|
| 9 |
+
iris = load_iris()
|
| 10 |
+
X = iris.data
|
| 11 |
+
y = iris.target
|
| 12 |
+
|
| 13 |
+
# Split dataset into training and testing sets
|
| 14 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 15 |
+
|
| 16 |
+
# Define model
|
| 17 |
+
model = make_pipeline(StandardScaler(), LogisticRegression(max_iter=1000))
|
| 18 |
+
|
| 19 |
+
# Train model
|
| 20 |
+
model.fit(X_train, y_train)
|
| 21 |
+
|
| 22 |
+
# Predict
|
| 23 |
+
y_pred = model.predict(X_test)
|
| 24 |
+
|
| 25 |
+
# Evaluate model
|
| 26 |
+
accuracy = accuracy_score(y_test, y_pred)
|
| 27 |
+
print("Accuracy:", accuracy)
|
requirements.txt
CHANGED
|
@@ -1 +1,5 @@
|
|
| 1 |
pytest
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
pytest
|
| 2 |
+
numpy
|
| 3 |
+
scipy
|
| 4 |
+
scikit-learn
|
| 5 |
+
gradio
|
test_app.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from app import predict_iris
|
| 3 |
+
|
| 4 |
+
def test_predict_iris():
|
| 5 |
+
# Test with known input values and expected output
|
| 6 |
+
assert predict_iris(5.1, 3.5, 1.4, 0.2) == "setosa"
|
| 7 |
+
assert predict_iris(6.9, 3.1, 5.4, 2.1) == "virginica"
|
| 8 |
+
|
| 9 |
+
# Test with out-of-range input values
|
| 10 |
+
assert predict_iris(0, 0, 0, 0) == "setosa"
|
| 11 |
+
assert predict_iris(10, 10, 10, 10) == "virginica"
|
| 12 |
+
|
| 13 |
+
# Test with random input values
|
| 14 |
+
assert predict_iris(5.8, 2.7, 3.9, 1.2) in ["versicolor", "virginica", "setosa"]
|
| 15 |
+
|
| 16 |
+
if __name__ == "__main__":
|
| 17 |
+
test_predict_iris()
|
| 18 |
+
print("All tests passed!")
|