nihaluser commited on
Commit
f43c1fe
·
verified ·
1 Parent(s): 6e16222

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -47
app.py CHANGED
@@ -2,55 +2,41 @@ import spaces
2
  import gradio as gr
3
  import joblib
4
  import pandas as pd
5
- import uvicorn
6
- from fastapi import FastAPI
7
- from fastapi.middleware.cors import CORSMiddleware
8
- from pydantic import BaseModel, ConfigDict, Field
9
- from typing import Literal
10
 
11
  model = joblib.load("Machine_Failure_Model.pkl")
12
 
13
- api = FastAPI(title="Machine Failure Prediction API")
14
 
15
- api.add_middleware(
16
- CORSMiddleware,
17
- allow_origins=["*"],
18
- allow_methods=["*"],
19
- allow_headers=["*"],
20
- )
21
-
22
-
23
- class MachineData(BaseModel):
24
- model_config = ConfigDict(populate_by_name=True)
25
-
26
- Type: Literal["L", "M", "H"] = Field(..., description="Enter your machine type here")
27
- Air_temperature_K: float = Field(..., alias="Air temperature [K]")
28
- Process_temperature_K: float = Field(..., alias="Process temperature [K]")
29
- Rotational_speed_rpm: int = Field(..., alias="Rotational speed [rpm]")
30
- Torque_Nm: float = Field(..., alias="Torque [Nm]")
31
- Tool_wear_min: int = Field(..., alias="Tool wear [min]")
32
-
33
-
34
- class FailurePrediction(BaseModel):
35
- machine_failure_prediction: int = Field(..., description="0 = No Failure, 1 = Failure")
36
- failure_probability: float = Field(..., description="Probability of machine failure (0.0 to 1.0)")
37
-
38
-
39
- @api.get("/health")
40
- def health():
41
  return {"status": "online", "message": "nihal is here"}
42
 
43
 
44
- @api.post("/predict", response_model=FailurePrediction)
45
- def predict(data: MachineData):
46
- input_row = pd.DataFrame([data.model_dump(by_alias=True)])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  prediction = int(model.predict(input_row)[0])
48
  probability = round(float(model.predict_proba(input_row)[0][1]), 2)
49
 
50
- return FailurePrediction(
51
- machine_failure_prediction=prediction,
52
- failure_probability=probability,
53
- )
54
 
55
 
56
  @spaces.GPU
@@ -59,16 +45,13 @@ def warmup():
59
 
60
 
61
  with gr.Blocks() as demo:
62
- gr.Markdown("### Aegis CNC Predictive Maintenance API is Running\nUse `POST /predict` from the frontend.")
63
  btn = gr.Button("Check ZeroGPU Hook")
64
  out = gr.Textbox(label="Status")
65
  btn.click(fn=warmup, outputs=out)
66
 
 
 
 
67
 
68
- if __name__ == "__main__":
69
- # ssr_mode=False prevents Gradio 6 from binding Node SSR to port 7860
70
- demo.launch(server_name="127.0.0.1", server_port=7861, ssr_mode=False, prevent_thread_lock=True)
71
- demo.close()
72
-
73
- app = gr.mount_gradio_app(api, demo, path="/")
74
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
2
  import gradio as gr
3
  import joblib
4
  import pandas as pd
 
 
 
 
 
5
 
6
  model = joblib.load("Machine_Failure_Model.pkl")
7
 
 
8
 
9
+ def health_check() -> dict:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  return {"status": "online", "message": "nihal is here"}
11
 
12
 
13
+ def predict_failure(
14
+ machine_type: str,
15
+ air_temp: float,
16
+ process_temp: float,
17
+ rpm: int,
18
+ torque: float,
19
+ tool_wear: int,
20
+ ) -> dict:
21
+ input_row = pd.DataFrame(
22
+ [
23
+ {
24
+ "Type": machine_type,
25
+ "Air temperature [K]": float(air_temp),
26
+ "Process temperature [K]": float(process_temp),
27
+ "Rotational speed [rpm]": int(rpm),
28
+ "Torque [Nm]": float(torque),
29
+ "Tool wear [min]": int(tool_wear),
30
+ }
31
+ ]
32
+ )
33
  prediction = int(model.predict(input_row)[0])
34
  probability = round(float(model.predict_proba(input_row)[0][1]), 2)
35
 
36
+ return {
37
+ "machine_failure_prediction": prediction,
38
+ "failure_probability": probability,
39
+ }
40
 
41
 
42
  @spaces.GPU
 
45
 
46
 
47
  with gr.Blocks() as demo:
48
+ gr.Markdown("### Aegis CNC Predictive Maintenance API")
49
  btn = gr.Button("Check ZeroGPU Hook")
50
  out = gr.Textbox(label="Status")
51
  btn.click(fn=warmup, outputs=out)
52
 
53
+ # Native Gradio REST API endpoints
54
+ gr.api(health_check, api_name="health")
55
+ gr.api(predict_failure, api_name="predict")
56
 
57
+ demo.launch(server_name="0.0.0.0", server_port=7860)