smoker-status / app.py
Rian Rachmanto
revised the code in line
fef344c
import uvicorn
from fastapi import FastAPI, UploadFile, File
import joblib
import numpy as np
import pandas as pd
import gradio as gr
import xgboost as xgb
# Create the app object
app = FastAPI()
# Load the saved model
model = joblib.load("model.pkl")
# Define the routes
@app.get('/')
def index():
return {'message': 'Hello, Everyone!'}
@app.get('/Welcome/{name}')
def get_name(name: str):
return {'Welcome to my ML model': name}
@app.post('/predict_csv')
def smoker_status(file: UploadFile = File(...)):
# Read the CSV file
df = pd.read_csv(file.name)
df.drop(['relaxation', 'LDL', 'waist(cm)'], axis=1, inplace=True)
# Make predictions for each row
predictions = []
for _, row in df.iterrows():
input_data = np.array([row.values])
prediction = model.predict(input_data)
if prediction == 0:
ispu = "NOT SMOKER"
else:
ispu = "SMOKER"
predictions.append(ispu)
return {"predictions": predictions}
# Define a Gradio interface for your model using gr.inputs.File
iface = gr.Interface(
fn=smoker_status,
inputs=gr.inputs.File(),
outputs=gr.outputs.Textbox(),
live=True,
title="CSV File Predictions",
description="Upload a CSV file to make predictions on its data.",
)
if __name__ == '__main__':
# Start Gradio alongside FastAPI without attempting to share
iface.launch()
# Start FastAPI
uvicorn.run("app:app", host='0.0.0.0', port=8080)