Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import numpy as np | |
| from joblib import load | |
| from tensorflow.keras.models import load_model | |
| import tensorflow as tf | |
| import requests | |
| # Load models from Hugging Face (update with your model URLs) | |
| def load_huggingface_model(model_url): | |
| response = requests.get(model_url) | |
| with open("model_temp.joblib" if model_url.endswith(".joblib") else "model_temp.h5", "wb") as f: | |
| f.write(response.content) | |
| return load("model_temp.joblib") if model_url.endswith(".joblib") else load_model("model_temp.h5") | |
| # URLs of your models on Hugging Face | |
| MODEL_URLS = { | |
| 'Deep Learning': 'https://huggingface.co/your-username/best_DeepLearning_model/resolve/main/best_DeepLearning_model.h5', | |
| 'Support Vector Regression': 'https://huggingface.co/your-username/best_SVR_model/resolve/main/best_SVR_model.joblib', | |
| 'Random Forest Regression': 'https://huggingface.co/your-username/best_RandomForestRegressor_model/resolve/main/best_RandomForestRegressor_model.joblib', | |
| 'Decision Tree Regression': 'https://huggingface.co/your-username/best_DecisionTreeRegressor_model/resolve/main/best_DecisionTreeRegressor_model.joblib' | |
| } | |
| # Prediction function | |
| def multi_inputs(input1, input2, input3, input4, input5, input6, input7, input8, input9, input10, input11, input12): | |
| try: | |
| model = load_huggingface_model(MODEL_URLS[input12]) | |
| # Convert numeric inputs to float | |
| input4, input5, input9 = float(input4), float(input5), float(input9) | |
| # Create input list with proper types | |
| inputs_to_transform = [ | |
| str(input1), str(input2), str(input3), input4, input5, | |
| float(input6), float(input7), float(input8), input9, | |
| str(input10), str(input11) | |
| ] | |
| # Reshape input for prediction | |
| input_data = np.array([inputs_to_transform], dtype=np.float32) | |
| if input12 == 'Deep Learning': | |
| tensor_input = tf.convert_to_tensor(input_data) | |
| prediction = model.predict(tensor_input, verbose=0) | |
| return float(prediction[0][0]) | |
| else: | |
| prediction = model.predict(input_data) | |
| return float(prediction[0]) | |
| except Exception as e: | |
| return f"Error in prediction: {str(e)}" | |
| # Gradio Interface | |
| interface = gr.Interface( | |
| fn=multi_inputs, | |
| inputs=[ | |
| gr.Radio(['male', 'female'], label='Gender'), | |
| gr.Dropdown(['Race1', 'Race2'], label='Race'), | |
| gr.Dropdown([20, 30, 40], label='Age'), | |
| gr.Number(label='Height (cm)'), | |
| gr.Number(label='Weight (kg)'), | |
| gr.Radio([0.0, 1.0], label='Diabetes'), | |
| gr.Radio([0.0, 1.0], label='Simvastatin'), | |
| gr.Radio([0.0, 1.0], label='Amiodarone'), | |
| gr.Number(label='INR'), | |
| gr.Dropdown(['Cyp2C9_A', 'Cyp2C9_B'], label='Cyp2C9 genotypes'), | |
| gr.Dropdown(['VKORC1_A', 'VKORC1_B'], label='VKORC1 genotypes'), | |
| gr.Dropdown(['Deep Learning', 'Support Vector Regression', 'Random Forest Regression', 'Decision Tree Regression'], label='Model Selection') | |
| ], | |
| outputs="number", | |
| live=False | |
| ) | |
| if __name__ == '__main__': | |
| interface.launch(debug=True) | |