Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from joblib import load | |
| import numpy as np | |
| # Load the model once globally | |
| model = load('model1.joblib') | |
| def model1(data): | |
| input_array = np.array([data]).reshape(1, -1) | |
| prediction = model.predict(input_array)[0] | |
| return prediction | |
| def process_input(six_digit_str): | |
| if len(six_digit_str) != 6 or not six_digit_str.isdigit(): | |
| return "Error: Please enter a 6-digit number string." | |
| # Create 2-digit windows with '3.' prefix | |
| a = float("3." + six_digit_str[0:2]) | |
| b = float("3." + six_digit_str[1:3]) | |
| c = float("3." + six_digit_str[2:4]) | |
| d = float("3." + six_digit_str[3:5]) | |
| e = float("3." + six_digit_str[4:6]) | |
| # Predict | |
| pred1 = model1(a) | |
| pred2 = model1(b) | |
| pred3 = model1(c) | |
| pred4 = model1(d) | |
| pred5 = model1(e) | |
| # Errors | |
| err1 = pred1 - b | |
| err2 = pred2 - c | |
| err3 = pred3 - d | |
| err4 = pred4 - e | |
| errors = [err1, err2, err3, err4] | |
| avg_error = sum(errors) / len(errors) | |
| # Distances from average | |
| 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] | |
| # Adjust pred4 | |
| ads_list = [] | |
| for err in errors: | |
| ads = pred4 + err if avg_error > 0 else pred4 - err | |
| ads_list.append(ads) | |
| # Round and extract 2nd decimal digit | |
| rounded = [round(val, 2) for val in ads_list] | |
| second_decimal_digits = [int(str(f).split(".")[1][1]) if '.' in str(f) and len(str(f).split(".")[1]) > 1 else 0 for f in rounded] | |
| return second_decimal_digits | |
| # Gradio Interface | |
| demo = gr.Interface(fn=process_input, | |
| inputs=gr.Textbox(label="Enter 6-digit number string"), | |
| outputs=gr.JSON(label="Second Decimal Digits")) | |
| demo.launch() | |