Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import math | |
| import numpy as np | |
| from joblib import load | |
| # Load model once | |
| model = load('model2.joblib') | |
| # Prediction function | |
| def model2(data): | |
| input_array = np.array([data]).reshape(1, -1) | |
| prediction = model.predict(input_array)[0] | |
| return prediction | |
| # Gradio interface function | |
| def process_input(num_str): | |
| if len(num_str) != 6 or not num_str.isdigit(): | |
| return ["Error: Input must be a 6-digit number"] | |
| windows = [int(num_str[i:i+2]) for i in range(len(num_str)-1)] # [12, 23, 34, 45, 56] | |
| divisor = 3 * math.pi | |
| normalized = [x / divisor for x in windows] | |
| # Get predictions | |
| pred1 = model2(normalized[0]) | |
| pred2 = model2(normalized[1]) | |
| pred3 = model2(normalized[2]) | |
| pred4 = model2(normalized[3]) | |
| pred5 = model2(normalized[4]) | |
| # Errors | |
| err1 = pred1 - windows[1] | |
| err2 = pred2 - windows[2] | |
| err3 = pred3 - windows[3] | |
| err4 = pred4 - windows[4] | |
| errors = [err1, err2, err3, err4] | |
| combined_error = sum(errors) | |
| avg_error = combined_error / len(errors) | |
| distances = [abs(e - avg_error) for e in errors] | |
| nearest_indices = sorted(range(len(distances)), key=lambda i: distances[i])[:2] | |
| nearest_values = [errors[i] for i in nearest_indices] | |
| all_three = nearest_values + [avg_error] | |
| mean_val = np.mean(all_three) | |
| # Adjust predictions | |
| ads_list = [(pred4 + err if avg_error > 0 else pred4 - err) for err in errors] | |
| ads_array = np.array(ads_list, dtype=np.float32) | |
| ads2 = ads_array * (3 * math.pi) | |
| # Extract digit before decimal | |
| digit_before_decimal = [int(str(int(x))[-1]) for x in ads2] | |
| return digit_before_decimal | |
| # Gradio UI | |
| iface = gr.Interface( | |
| fn=process_input, | |
| inputs=gr.Textbox(label="Enter a 6-digit number"), | |
| outputs=gr.Textbox(label="Digit Before Decimal Array"), | |
| title="ML Prediction Error Adjustment", | |
| description="Input a 6-digit number. Returns processed digit array after model predictions and transformations." | |
| ) | |
| iface.launch() | |