Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import joblib | |
| import numpy as np | |
| # Load trained model | |
| model = joblib.load("driving_score_model.pkl") | |
| # Define the prediction function | |
| def predict_driving_score(Acceleration_X, Acceleration_Y, Acceleration_Z, Gyroscope_X, Gyroscope_Y, Gyroscope_Z, Speed_kmh): | |
| try: | |
| # Prepare input as a numpy array | |
| sample_input = np.array([[Acceleration_X, Acceleration_Y, Acceleration_Z, Gyroscope_X, Gyroscope_Y, Gyroscope_Z, Speed_kmh]]) | |
| # Predict scores | |
| prediction = model.predict(sample_input) | |
| safety_score = prediction[0][0] | |
| eco_score = prediction[0][1] | |
| return round(safety_score, 2), round(eco_score, 2) | |
| except Exception as e: | |
| return f"Error: {str(e)}", f"Error: {str(e)}" | |
| # Gradio Interface (expose POST method) | |
| def gradio_interface(): | |
| return gr.Interface( | |
| fn=predict_driving_score, | |
| inputs=[ | |
| gr.Number(label="Acceleration_X"), | |
| gr.Number(label="Acceleration_Y"), | |
| gr.Number(label="Acceleration_Z"), | |
| gr.Number(label="Gyroscope_X"), | |
| gr.Number(label="Gyroscope_Y"), | |
| gr.Number(label="Gyroscope_Z"), | |
| gr.Number(label="Speed_kmh") | |
| ], | |
| outputs=[ | |
| gr.Number(label="Predicted Safety Score"), | |
| gr.Number(label="Predicted EcoScore") | |
| ], | |
| title="Driving Safety & Eco Score Predictor", | |
| description="Enter sensor values to predict Safety Score and EcoScore.", | |
| allow_flagging="never", # Optionally remove flagging for a more streamlined interface | |
| live=True # Automatically update results as inputs change | |
| ) | |
| # Launch the Gradio interface and expose a public link | |
| gradio_interface().launch(share=True) # share=True will provide a public link | |