File size: 2,729 Bytes
8fc0265
 
 
 
 
 
b78c3b6
8fc0265
 
c9fdbee
8fc0265
 
 
 
 
b78c3b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8fc0265
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import json
import numpy as np
import joblib
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
import uvicorn
import streamlit as st

# Load model from the local storage (ensure the model file is in the same directory)
model_path = "model.pkl"
gb_model_loaded = joblib.load(model_path)

# Create FastAPI app
app = FastAPI()

# Set the title of the app
st.title("Medical Prediction Model")

# Instruction text
st.write("Enter 32 features for prediction:")

# Create 32 input fields for user input
inputs = []
for i in range(32):
    value = st.number_input(f"Feature {i + 1}", min_value=0, step=1)
    inputs.append(value)

# Button to make prediction
if st.button("Predict"):
    # Prepare the data for the request
    input_data = {"features": inputs}
    
    # Set the URL for your Hugging Face Space
    url = "https://phoner45-mediguide-api.hf.space/predict"  # Replace with your actual Space URL

    # Make a POST request
    response = requests.post(url, json=inputs)
    
    # Check the response status code
    if response.status_code == 200:
        # Get the JSON response
        prediction = response.json()
        # Display the prediction results
        st.success("Prediction Results:")
        st.json(prediction)
    else:
        st.error(f"Error: {response.status_code} - {response.text}")
        
# Define class labels
class_names = [
    'Emergency & Accident Unit', 'Heart Clinic',
    'Neuro Med Center', 'OPD:EYE', 'Dental',
    'OPD:MED', 'OPD:ENT', 'OPD:OBG',
    'OPD:Surgery + Uro.', 'Orthopedic Surgery',
    'GI Clinic', 'Breast Clinic', 'Skin & Dermatology'
]

# Define the input format for FastAPI using Pydantic BaseModel
class InputData(BaseModel):
    features: list[float]  # List of 32 feature inputs

@app.post("/predict")
def predict(data: InputData):
    try:
        # Validate input length
        if len(data.features) != 32:
            raise HTTPException(status_code=400, detail=f"Expected 32 features, but got {len(data.features)}")

        # Convert list to numpy array and reshape
        input_array = np.array(data.features).reshape(1, -1)

        # Get predictions
        prediction = gb_model_loaded.predict_proba(input_array)

        # Convert probabilities to percentage and format
        probabilities = (prediction[0] * 100).round(2)
        result_pro = {class_name: f"{prob:.2f}%" for class_name, prob in zip(class_names, probabilities)}

        # Return result as JSON
        return {'result': result_pro}

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

# Run the application with the following command if needed
# if __name__ == "__main__":
#     uvicorn.run(app, host="0.0.0.0", port=8501)