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)