Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import pickle | |
| import gradio as gr | |
| # Try loading files directly first (for Hugging Face Spaces) | |
| try: | |
| model = pickle.load(open("xgb_model.pkl", "rb")) | |
| scaler = pickle.load(open("scaler.pkl", "rb")) | |
| except FileNotFoundError: | |
| # Fallback to HF Hub download if files not found locally | |
| from huggingface_hub import hf_hub_download | |
| try: | |
| model_path = hf_hub_download(repo_id="nitinnyadavvv/cricpred", filename="xgb_model.pkl") | |
| scaler_path = hf_hub_download(repo_id="nitinnyadavvv/cricpred", filename="scaler.pkl") | |
| model = pickle.load(open(model_path, "rb")) | |
| scaler = pickle.load(open(scaler_path, "rb")) | |
| except Exception as e: | |
| raise RuntimeError("Failed to load model files. Please ensure they exist in the repository.") from e | |
| def predict_run(over_number, ball_number, total_runs_till_now, total_wickets, current_run_rate, | |
| last_3_balls_runs, last_over_runs, partnership_runs, current_over_score): | |
| features = np.array([[over_number, ball_number, total_runs_till_now, total_wickets, | |
| current_run_rate, last_3_balls_runs, last_over_runs, | |
| partnership_runs, current_over_score]]) | |
| scaled_features = scaler.transform(features) | |
| prediction = model.predict(scaled_features) | |
| return round(float(prediction[0]), 2) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Cricket Run Predictor") | |
| gr.Markdown("Predict runs scored in the current over using match context.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| over_number = gr.Number(label="Over Number (0-50)", value=10) | |
| ball_number = gr.Number(label="Ball Number (1-6)", value=3) | |
| total_runs_till_now = gr.Number(label="Total Runs Till Now", value=80) | |
| total_wickets = gr.Number(label="Total Wickets (0-10)", value=2) | |
| current_run_rate = gr.Number(label="Current Run Rate", value=5.5) | |
| with gr.Column(): | |
| last_3_balls_runs = gr.Number(label="Last 3 Balls Runs", value=6) | |
| last_over_runs = gr.Number(label="Last Over Runs", value=9) | |
| partnership_runs = gr.Number(label="Partnership Runs", value=45) | |
| current_over_score = gr.Number(label="Current Over Score", value=8) | |
| predict_btn = gr.Button("Predict Runs") | |
| output = gr.Number(label="Predicted Runs in This Over") | |
| predict_btn.click( | |
| fn=predict_run, | |
| inputs=[over_number, ball_number, total_runs_till_now, total_wickets, | |
| current_run_rate, last_3_balls_runs, last_over_runs, | |
| partnership_runs, current_over_score], | |
| outputs=output | |
| ) | |
| demo.launch() |