Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from joblib import load
|
| 3 |
+
import numpy as np
|
| 4 |
+
|
| 5 |
+
# Load the model once globally
|
| 6 |
+
model = load('model1.joblib')
|
| 7 |
+
|
| 8 |
+
def model1(data):
|
| 9 |
+
input_array = np.array([data]).reshape(1, -1)
|
| 10 |
+
prediction = model.predict(input_array)[0]
|
| 11 |
+
return prediction
|
| 12 |
+
|
| 13 |
+
def process_input(six_digit_str):
|
| 14 |
+
if len(six_digit_str) != 6 or not six_digit_str.isdigit():
|
| 15 |
+
return "Error: Please enter a 6-digit number string."
|
| 16 |
+
|
| 17 |
+
# Create 2-digit windows with '3.' prefix
|
| 18 |
+
a = float("3." + six_digit_str[0:2])
|
| 19 |
+
b = float("3." + six_digit_str[1:3])
|
| 20 |
+
c = float("3." + six_digit_str[2:4])
|
| 21 |
+
d = float("3." + six_digit_str[3:5])
|
| 22 |
+
e = float("3." + six_digit_str[4:6])
|
| 23 |
+
|
| 24 |
+
# Predict
|
| 25 |
+
pred1 = model1(a)
|
| 26 |
+
pred2 = model1(b)
|
| 27 |
+
pred3 = model1(c)
|
| 28 |
+
pred4 = model1(d)
|
| 29 |
+
pred5 = model1(e)
|
| 30 |
+
|
| 31 |
+
# Errors
|
| 32 |
+
err1 = pred1 - b
|
| 33 |
+
err2 = pred2 - c
|
| 34 |
+
err3 = pred3 - d
|
| 35 |
+
err4 = pred4 - e
|
| 36 |
+
|
| 37 |
+
errors = [err1, err2, err3, err4]
|
| 38 |
+
avg_error = sum(errors) / len(errors)
|
| 39 |
+
|
| 40 |
+
# Distances from average
|
| 41 |
+
distances = [abs(e - avg_error) for e in errors]
|
| 42 |
+
nearest_indices = sorted(range(len(distances)), key=lambda i: distances[i])[:2]
|
| 43 |
+
nearest_values = [errors[i] for i in nearest_indices]
|
| 44 |
+
all_three = nearest_values + [avg_error]
|
| 45 |
+
|
| 46 |
+
# Adjust pred4
|
| 47 |
+
ads_list = []
|
| 48 |
+
for err in errors:
|
| 49 |
+
ads = pred4 + err if avg_error > 0 else pred4 - err
|
| 50 |
+
ads_list.append(ads)
|
| 51 |
+
|
| 52 |
+
# Round and extract 2nd decimal digit
|
| 53 |
+
rounded = [round(val, 2) for val in ads_list]
|
| 54 |
+
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]
|
| 55 |
+
|
| 56 |
+
return second_decimal_digits
|
| 57 |
+
|
| 58 |
+
# Gradio Interface
|
| 59 |
+
demo = gr.Interface(fn=process_input,
|
| 60 |
+
inputs=gr.Textbox(label="Enter 6-digit number string"),
|
| 61 |
+
outputs=gr.JSON(label="Second Decimal Digits"))
|
| 62 |
+
|
| 63 |
+
demo.launch()
|