NangTherr commited on
Commit
f5de3c3
·
verified ·
1 Parent(s): 157e6ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -23
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
- # Initialize FastAPI app
8
- app = FastAPI()
 
9
 
10
- # Load model and scaler
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([[data.soil_moisture, data.temperature, data.air_humidity, data.light_intensity]])
24
-
25
  # Scale the data
26
  std_data = scaler.transform(input_data)
27
-
28
  # Make prediction
29
  prediction = model.predict(std_data)
30
-
31
- return {"prediction": int(prediction[0])} # Convert prediction to int (0 or 1)
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  if __name__ == "__main__":
34
- uvicorn.run(app, host="0.0.0.0", port=8000)
 
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()