Shreyas5555555 commited on
Commit
afef063
·
verified ·
1 Parent(s): 750b0de

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import math
3
+ import numpy as np
4
+ from joblib import load
5
+
6
+ # Load model once
7
+ model = load('model2.joblib')
8
+
9
+ # Prediction function
10
+ def model2(data):
11
+ input_array = np.array([data]).reshape(1, -1)
12
+ prediction = model.predict(input_array)[0]
13
+ return prediction
14
+
15
+ # Gradio interface function
16
+ def process_input(num_str):
17
+ if len(num_str) != 6 or not num_str.isdigit():
18
+ return ["Error: Input must be a 6-digit number"]
19
+
20
+ windows = [int(num_str[i:i+2]) for i in range(len(num_str)-1)] # [12, 23, 34, 45, 56]
21
+ divisor = 3 * math.pi
22
+ normalized = [x / divisor for x in windows]
23
+
24
+ # Get predictions
25
+ pred1 = model2(normalized[0])
26
+ pred2 = model2(normalized[1])
27
+ pred3 = model2(normalized[2])
28
+ pred4 = model2(normalized[3])
29
+ pred5 = model2(normalized[4])
30
+
31
+ # Errors
32
+ err1 = pred1 - windows[1]
33
+ err2 = pred2 - windows[2]
34
+ err3 = pred3 - windows[3]
35
+ err4 = pred4 - windows[4]
36
+ errors = [err1, err2, err3, err4]
37
+
38
+ combined_error = sum(errors)
39
+ avg_error = combined_error / len(errors)
40
+ distances = [abs(e - avg_error) for e in errors]
41
+ nearest_indices = sorted(range(len(distances)), key=lambda i: distances[i])[:2]
42
+ nearest_values = [errors[i] for i in nearest_indices]
43
+ all_three = nearest_values + [avg_error]
44
+ mean_val = np.mean(all_three)
45
+
46
+ # Adjust predictions
47
+ ads_list = [(pred4 + err if avg_error > 0 else pred4 - err) for err in errors]
48
+ ads_array = np.array(ads_list, dtype=np.float32)
49
+ ads2 = ads_array * (3 * math.pi)
50
+
51
+ # Extract digit before decimal
52
+ digit_before_decimal = [int(str(int(x))[-1]) for x in ads2]
53
+
54
+ return digit_before_decimal
55
+
56
+ # Gradio UI
57
+ iface = gr.Interface(
58
+ fn=process_input,
59
+ inputs=gr.Textbox(label="Enter a 6-digit number"),
60
+ outputs=gr.Textbox(label="Digit Before Decimal Array"),
61
+ title="ML Prediction Error Adjustment",
62
+ description="Input a 6-digit number. Returns processed digit array after model predictions and transformations."
63
+ )
64
+
65
+ iface.launch()