Spaces:
Sleeping
Sleeping
Clement Vachet
commited on
Commit
·
8631332
1
Parent(s):
13eef60
Add user interface via gradio app
Browse files- .gitignore +3 -0
- app.py +111 -0
- models/model.pkl +0 -0
- requirements.txt +3 -3
.gitignore
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# Byte-compiled / optimized / DLL files
|
| 2 |
__pycache__/
|
| 3 |
*.py[cod]
|
|
|
|
| 1 |
+
# Environment file
|
| 2 |
+
config_api.env
|
| 3 |
+
|
| 4 |
# Byte-compiled / optimized / DLL files
|
| 5 |
__pycache__/
|
| 6 |
*.py[cod]
|
app.py
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from classification.classifier import Classifier
|
| 5 |
+
from dotenv import load_dotenv, find_dotenv
|
| 6 |
+
import json
|
| 7 |
+
|
| 8 |
+
# Initialize API URLs from env file or global settings
|
| 9 |
+
def retrieve_api():
|
| 10 |
+
|
| 11 |
+
env_path = find_dotenv('config_api.env')
|
| 12 |
+
if env_path:
|
| 13 |
+
load_dotenv(dotenv_path=env_path)
|
| 14 |
+
print("config_api.env file loaded successfully.")
|
| 15 |
+
else:
|
| 16 |
+
print("config_api.env file not found.")
|
| 17 |
+
|
| 18 |
+
# Use of AWS endpoint or local container by default
|
| 19 |
+
global AWS_API
|
| 20 |
+
AWS_API = os.getenv("AWS_API", default="http://localhost:8000")
|
| 21 |
+
|
| 22 |
+
def initialize_classifier():
|
| 23 |
+
global cls
|
| 24 |
+
cls = Classifier()
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def predict_class_local(sepl, sepw, petl, petw):
|
| 28 |
+
data = list(map(float, [sepl, sepw, petl, petw]))
|
| 29 |
+
results = cls.load_and_test(data)
|
| 30 |
+
return results
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def predict_class_aws(sepl, sepw, petl, petw):
|
| 34 |
+
if AWS_API == "http://localhost:8000":
|
| 35 |
+
API_endpoint = AWS_API + "/2015-03-31/functions/function/invocations"
|
| 36 |
+
else:
|
| 37 |
+
API_endpoint = AWS_API + "/test/classify"
|
| 38 |
+
|
| 39 |
+
data = list(map(float, [sepl, sepw, petl, petw]))
|
| 40 |
+
json_object = {
|
| 41 |
+
"features": [
|
| 42 |
+
data
|
| 43 |
+
]
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
response = requests.post(API_endpoint, json=json_object)
|
| 47 |
+
if response.status_code == 200:
|
| 48 |
+
# Process the response
|
| 49 |
+
response_json = response.json()
|
| 50 |
+
results_dict = json.loads(response_json["body"])
|
| 51 |
+
else:
|
| 52 |
+
results_dict = {"Error": response.status_code}
|
| 53 |
+
gr.Error(f"\t API Error: {response.status_code}")
|
| 54 |
+
return results_dict
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def predict(sepl, sepw, petl, petw, type):
|
| 58 |
+
print("type: ", type)
|
| 59 |
+
if type == "Local":
|
| 60 |
+
results = predict_class_local(sepl, sepw, petl, petw)
|
| 61 |
+
elif type == "AWS API":
|
| 62 |
+
results = predict_class_aws(sepl, sepw, petl, petw)
|
| 63 |
+
|
| 64 |
+
prediction = results["predictions"][0]
|
| 65 |
+
confidence = max(results["probabilities"][0])
|
| 66 |
+
|
| 67 |
+
return f"Prediction: {prediction} \t - \t Confidence: {confidence:.3f}"
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# Define the Gradio interface
|
| 71 |
+
def user_interface():
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.Markdown("# IRIS classification task - use of AWS Lambda")
|
| 74 |
+
gr.Markdown(
|
| 75 |
+
"""
|
| 76 |
+
Aims: Categorization of different species of iris flowers (Setosa, Versicolor, and Virginica)
|
| 77 |
+
based on measurements of physical characteristics (sepals and petals).
|
| 78 |
+
|
| 79 |
+
Notes: This web application uses two types of predictions:
|
| 80 |
+
- local prediction (direct source code)
|
| 81 |
+
- cloud prediction via an AWS API (i.e. use of ECR, Lambda function and API Gateway) to run the machine learning model.
|
| 82 |
+
|
| 83 |
+
"""
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
with gr.Row():
|
| 87 |
+
with gr.Column():
|
| 88 |
+
with gr.Group():
|
| 89 |
+
gr_sepl = gr.Slider(minimum=4.0, maximum=8.0, step=0.1, label="Sepal Length (in cm)")
|
| 90 |
+
gr_sepw = gr.Slider(minimum=2.0, maximum=5.0, step=0.1, label="Sepal Width (in cm)")
|
| 91 |
+
gr_petl = gr.Slider(minimum=1.0, maximum=7.0, step=0.1, label="Petal Length (in cm)")
|
| 92 |
+
gr_petw = gr.Slider(minimum=0.1, maximum=2.8, step=0.1, label="Petal Width (in cm)")
|
| 93 |
+
with gr.Column():
|
| 94 |
+
with gr.Row():
|
| 95 |
+
gr_type = gr.Radio(["Local", "AWS API"], value="Local", label="Prediction type")
|
| 96 |
+
with gr.Row():
|
| 97 |
+
gr_output = gr.Textbox(label="Prediction output")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
submit_btn = gr.Button("Submit")
|
| 101 |
+
clear_button = gr.ClearButton()
|
| 102 |
+
|
| 103 |
+
submit_btn.click(fn=predict, inputs=[gr_sepl, gr_sepw, gr_petl, gr_petw, gr_type], outputs=[gr_output])
|
| 104 |
+
clear_button.click(lambda: None, inputs=None, outputs=[gr_output], queue=False)
|
| 105 |
+
demo.queue().launch(debug=True)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
if __name__ == "__main__":
|
| 109 |
+
retrieve_api()
|
| 110 |
+
initialize_classifier()
|
| 111 |
+
user_interface()
|
models/model.pkl
CHANGED
|
Binary files a/models/model.pkl and b/models/model.pkl differ
|
|
|
requirements.txt
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
scikit-learn==1.6.0
|
| 2 |
pandas==2.2.3
|
| 3 |
numpy==2.2.0
|
| 4 |
-
|
| 5 |
-
# pyarrow==16.0.0
|
| 6 |
pytest==8.3.4
|
| 7 |
-
joblib==1.4.2
|
|
|
|
|
|
| 1 |
scikit-learn==1.6.0
|
| 2 |
pandas==2.2.3
|
| 3 |
numpy==2.2.0
|
| 4 |
+
gradio==5.5.0
|
|
|
|
| 5 |
pytest==8.3.4
|
| 6 |
+
joblib==1.4.2
|
| 7 |
+
python-dotenv==1.0.1
|