Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,32 @@
|
|
|
|
1 |
import joblib
|
2 |
import numpy as np
|
3 |
-
from fastapi import FastAPI
|
4 |
-
from pydantic import BaseModel
|
5 |
-
import uvicorn
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
9 |
|
10 |
-
|
11 |
-
model = joblib.load('conme.pkl')
|
12 |
-
scaler = joblib.load('scaler.joblib')
|
13 |
-
|
14 |
-
class InputData(BaseModel):
|
15 |
-
soil_moisture: float
|
16 |
-
temperature: float
|
17 |
-
air_humidity: float
|
18 |
-
light_intensity: float
|
19 |
-
|
20 |
-
@app.post("/predict")
|
21 |
-
def predict(data: InputData):
|
22 |
# Prepare input data
|
23 |
-
input_data = np.array([[
|
24 |
-
|
25 |
# Scale the data
|
26 |
std_data = scaler.transform(input_data)
|
27 |
-
|
28 |
# Make prediction
|
29 |
prediction = model.predict(std_data)
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
if __name__ == "__main__":
|
34 |
-
|
|
|
1 |
+
import gradio as gr
|
2 |
import joblib
|
3 |
import numpy as np
|
|
|
|
|
|
|
4 |
|
5 |
+
# Load the model and scaler
|
6 |
+
model = joblib.load("conme.pkl") # Đảm bảo tên tệp phù hợp
|
7 |
+
scaler = joblib.load("scaler.joblib")
|
8 |
|
9 |
+
def predict(soil_moisture, temperature, air_humidity, light_intensity):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
# Prepare input data
|
11 |
+
input_data = np.array([[soil_moisture, temperature, air_humidity, light_intensity]])
|
|
|
12 |
# Scale the data
|
13 |
std_data = scaler.transform(input_data)
|
|
|
14 |
# Make prediction
|
15 |
prediction = model.predict(std_data)
|
16 |
+
return int(prediction[0]) # Convert prediction to int (0 or 1)
|
17 |
+
|
18 |
+
# Set up Gradio interface
|
19 |
+
iface = gr.Interface(
|
20 |
+
fn=predict,
|
21 |
+
inputs=[
|
22 |
+
gr.inputs.Number(label="Soil Moisture"),
|
23 |
+
gr.inputs.Number(label="Temperature"),
|
24 |
+
gr.inputs.Number(label="Air Humidity"),
|
25 |
+
gr.inputs.Number(label="Light Intensity"),
|
26 |
+
],
|
27 |
+
outputs="text",
|
28 |
+
title="Plant Watering Prediction"
|
29 |
+
)
|
30 |
|
31 |
if __name__ == "__main__":
|
32 |
+
iface.launch()
|