aartstudio commited on
Commit
c448fa9
·
verified ·
1 Parent(s): 7d2679a

Upload 3 files

Browse files
Files changed (3) hide show
  1. DBS_SingDollar.csv +123 -0
  2. app.py +69 -0
  3. currency_gradio_app.py +101 -0
DBS_SingDollar.csv ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ,Date,DBS,SGD
2
+ 1,6/20/2017,20.45,1.38925
3
+ 2,6/19/2017,20.6,1.38707
4
+ 3,6/16/2017,20.45,1.38324
5
+ 4,6/15/2017,20.41,1.38378
6
+ 5,6/14/2017,20.59,1.37595
7
+ 6,6/13/2017,20.78,1.38172
8
+ 7,6/12/2017,20.59,1.38424
9
+ 8,6/9/2017,20.72,1.38401
10
+ 9,6/8/2017,20.47,1.3827
11
+ 10,6/7/2017,20.43,1.38187
12
+ 11,6/6/2017,20.49,1.37925
13
+ 12,6/5/2017,20.49,1.38157
14
+ 13,6/2/2017,20.48,1.38104
15
+ 14,6/1/2017,20.65,1.38653
16
+ 15,5/31/2017,20.47,1.3832
17
+ 16,5/30/2017,20.6,1.3853
18
+ 17,5/29/2017,20.84,1.38665
19
+ 18,5/26/2017,20.8,1.38071
20
+ 19,5/25/2017,21.04,1.38634
21
+ 20,5/24/2017,21,1.38451
22
+ 21,5/23/2017,21,1.38995
23
+ 22,5/22/2017,20.86,1.38642
24
+ 23,5/19/2017,20.7,1.38523
25
+ 24,5/18/2017,20.63,1.39297
26
+ 25,5/17/2017,20.8,1.38981
27
+ 26,5/16/2017,20.65,1.39474
28
+ 27,5/15/2017,20.77,1.39749
29
+ 28,5/12/2017,20.68,1.40394
30
+ 29,5/11/2017,20.93,1.40746
31
+ 30,5/9/2017,20.5,1.41128
32
+ 31,5/8/2017,20.3,1.40604
33
+ 32,5/5/2017,20.42,1.40496
34
+ 33,5/4/2017,20.45,1.40069
35
+ 34,5/3/2017,20.53,1.39724
36
+ 35,5/2/2017,19.57,1.39379
37
+ 36,4/28/2017,19.07,1.39739
38
+ 37,4/27/2017,18.93,1.3971
39
+ 38,4/26/2017,19.03,1.39598
40
+ 39,4/25/2017,18.92,1.39412
41
+ 40,4/24/2017,18.73,1.39261
42
+ 41,4/21/2017,18.5,1.39655
43
+ 42,4/20/2017,18.64,1.39772
44
+ 43,4/19/2017,18.52,1.39821
45
+ 44,4/18/2017,18.65,1.39584
46
+ 45,4/17/2017,18.63,1.39794
47
+ 46,4/13/2017,18.82,1.39799
48
+ 47,4/12/2017,18.97,1.39653
49
+ 48,4/11/2017,18.88,1.40404
50
+ 49,4/10/2017,18.85,1.4048
51
+ 50,4/7/2017,18.78,1.40474
52
+ 51,4/6/2017,18.85,1.40211
53
+ 52,4/5/2017,18.93,1.40067
54
+ 53,4/4/2017,18.95,1.39817
55
+ 54,4/3/2017,19.12,1.3971
56
+ 55,3/31/2017,19.12,1.39639
57
+ 56,3/30/2017,19.02,1.39769
58
+ 57,3/29/2017,19.01,1.39407
59
+ 58,3/28/2017,18.75,1.39677
60
+ 59,3/27/2017,18.43,1.39397
61
+ 60,3/24/2017,18.52,1.39885
62
+ 61,3/23/2017,18.38,1.3997
63
+ 62,3/22/2017,18.4,1.39854
64
+ 63,3/21/2017,18.73,1.39934
65
+ 64,3/20/2017,18.74,1.39688
66
+ 65,3/17/2017,18.86,1.40201
67
+ 66,3/16/2017,18.84,1.40121
68
+ 67,3/15/2017,18.85,1.40224
69
+ 68,3/14/2017,18.91,1.41592
70
+ 69,3/13/2017,18.83,1.41477
71
+ 70,3/10/2017,18.66,1.41173
72
+ 71,3/9/2017,18.67,1.42132
73
+ 72,3/8/2017,19,1.41763
74
+ 73,3/7/2017,18.82,1.41135
75
+ 74,3/6/2017,18.79,1.41235
76
+ 75,3/3/2017,18.74,1.41037
77
+ 76,3/2/2017,18.76,1.41213
78
+ 77,3/1/2017,18.55,1.40907
79
+ 78,2/28/2017,18.46,1.405
80
+ 79,2/27/2017,18.54,1.40429
81
+ 80,2/24/2017,18.6,1.40447
82
+ 81,2/23/2017,18.62,1.40763
83
+ 82,2/22/2017,18.48,1.41323
84
+ 83,2/21/2017,18.19,1.42045
85
+ 84,2/20/2017,18.23,1.41793
86
+ 85,2/17/2017,18.33,1.41901
87
+ 86,2/16/2017,18.27,1.41659
88
+ 87,2/15/2017,17.97,1.41906
89
+ 88,2/14/2017,18,1.41995
90
+ 89,2/13/2017,18.64,1.42416
91
+ 90,2/10/2017,18.7,1.42058
92
+ 91,2/9/2017,18.55,1.42066
93
+ 92,2/8/2017,18.53,1.41667
94
+ 93,2/7/2017,18.59,1.41857
95
+ 94,2/6/2017,18.66,1.40917
96
+ 95,2/3/2017,18.39,1.40746
97
+ 96,2/2/2017,18.51,1.41138
98
+ 97,2/1/2017,18.61,1.41244
99
+ 98,1/31/2017,18.7,1.40954
100
+ 99,1/27/2017,18.89,1.43178
101
+ 100,1/26/2017,18.76,1.42589
102
+ 101,1/25/2017,18.58,1.41285
103
+ 102,1/24/2017,18.39,1.42117
104
+ 103,1/23/2017,18.08,1.41598
105
+ 104,1/20/2017,18.01,1.42705
106
+ 105,1/19/2017,18.02,1.42616
107
+ 106,1/18/2017,17.94,1.42876
108
+ 107,1/17/2017,18.09,1.41601
109
+ 108,1/16/2017,18.06,1.4303
110
+ 109,1/13/2017,18.05,1.42592
111
+ 110,1/12/2017,17.86,1.42728
112
+ 111,1/11/2017,17.78,1.42678
113
+ 112,1/10/2017,17.73,1.43654
114
+ 113,1/9/2017,17.59,1.43529
115
+ 114,1/6/2017,17.42,1.43938
116
+ 115,1/5/2017,17.54,1.42847
117
+ 116,1/4/2017,17.27,1.43942
118
+ 117,1/3/2017,17.07,1.44992
119
+ 118,12/30/2016,17.09,1.44655
120
+ 119,12/29/2016,17.13,1.44814
121
+ 120,12/28/2016,17.22,1.45018
122
+ 121,12/27/2016,17.19,1.44954
123
+ 122,12/23/2016,17.14,1.44766
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """2.7 (Optional DBS).ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1Js2DNDDuusJx06SBvdh-lmKbuCBTBw9B
8
+
9
+ chatgpt prompt:
10
+ Please create a regression model to predict DBS base on SGD exchange rate, create the model and then use gradio for the interface
11
+ """
12
+
13
+ # Install dependencies (uncomment if needed)
14
+ # !pip install pandas scikit-learn gradio
15
+
16
+ import pandas as pd
17
+ from sklearn.model_selection import train_test_split
18
+ from sklearn.linear_model import LinearRegression
19
+ from sklearn.metrics import mean_squared_error, r2_score
20
+ import gradio as gr
21
+
22
+ # 1. LOAD DATA
23
+ # Make sure the CSV file is in the same folder, or give full path
24
+ df = pd.read_csv("DBS_SingDollar.csv")
25
+
26
+ # If your header row is exactly: Date,DBS,SGD this will work directly
27
+ # Keep only needed columns and drop missing values
28
+ df = df[["DBS", "SGD"]].dropna()
29
+
30
+ X = df[["SGD"]] # feature
31
+ y = df["DBS"] # target
32
+
33
+ # 2. TRAIN / TEST SPLIT
34
+ X_train, X_test, y_train, y_test = train_test_split(
35
+ X, y, test_size=0.2, random_state=42
36
+ )
37
+
38
+ # 3. TRAIN MODEL (Linear Regression)
39
+ model = LinearRegression()
40
+ model.fit(X_train, y_train)
41
+
42
+ # 4. EVALUATE (optional, shows in console)
43
+ y_pred = model.predict(X_test)
44
+ rmse = mean_squared_error(y_test, y_pred) ** 0.5
45
+ r2 = r2_score(y_test, y_pred)
46
+
47
+ print(f"RMSE: {rmse:.4f}")
48
+ print(f"R²: {r2:.4f}")
49
+
50
+ # 5. PREDICTION FUNCTION FOR GRADIO
51
+ def predict_dbs_price(sgd_rate: float) -> str:
52
+ """
53
+ Input: SGD exchange rate (float)
54
+ Output: predicted DBS price (string for display)
55
+ """
56
+ pred = model.predict([[sgd_rate]])[0]
57
+ return f"Predicted DBS price: {pred:.2f}"
58
+
59
+ # 6. GRADIO INTERFACE
60
+ iface = gr.Interface(
61
+ fn=predict_dbs_price,
62
+ inputs=gr.Number(label="SGD Exchange Rate"),
63
+ outputs=gr.Textbox(label="Predicted DBS Price"),
64
+ title="DBS Price Predictor",
65
+ description="Enter the SGD exchange rate to predict the DBS share price (trained with historical DBS & SGD data)."
66
+ )
67
+
68
+ iface.launch()
69
+
currency_gradio_app.py ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import pandas as pd
3
+ import joblib
4
+ import gradio as gr
5
+
6
+ # 1. Load & train the model (you can also pre-train and just load a .joblib file)
7
+ df = pd.read_csv("DBS_SingDollar.csv")
8
+
9
+ # Parse dates and sort just in case
10
+ df["Date"] = pd.to_datetime(df["Date"])
11
+ df = df.sort_values("Date")
12
+
13
+ # Create simple supervised features:
14
+ # - Use today's SGD and DBS price to predict tomorrow's SGD rate
15
+ df["SGD_today"] = df["SGD"]
16
+ df["DBS_today"] = df["DBS"]
17
+ df["SGD_next"] = df["SGD"].shift(-1)
18
+
19
+ data = df.dropna(subset=["SGD_next"]).copy()
20
+
21
+ X = data[["SGD_today", "DBS_today"]]
22
+ y = data["SGD_next"]
23
+
24
+ from sklearn.ensemble import RandomForestRegressor
25
+ from sklearn.model_selection import train_test_split
26
+ from sklearn.metrics import mean_absolute_error, r2_score
27
+
28
+ X_train, X_test, y_train, y_test = train_test_split(
29
+ X, y, test_size=0.2, shuffle=False
30
+ )
31
+
32
+ model = RandomForestRegressor(
33
+ n_estimators=300,
34
+ random_state=42,
35
+ min_samples_leaf=2,
36
+ )
37
+
38
+ model.fit(X_train, y_train)
39
+
40
+ # Evaluate (printed in console)
41
+ y_pred = model.predict(X_test)
42
+ mae = mean_absolute_error(y_test, y_pred)
43
+ r2 = r2_score(y_test, y_pred)
44
+ print(f"Test MAE: {mae:.6f}")
45
+ print(f"Test R^2: {r2:.4f}")
46
+
47
+ # 2. Define Gradio prediction function
48
+
49
+ def predict_next_sgd(current_sgd, current_dbs):
50
+ """Predict tomorrow's SGD rate from today's SGD + DBS price."""
51
+ import numpy as np
52
+
53
+ X_input = np.array([[current_sgd, current_dbs]])
54
+ next_sgd = model.predict(X_input)[0]
55
+
56
+ change = next_sgd - current_sgd
57
+ pct_change = (change / current_sgd) * 100 if current_sgd != 0 else 0.0
58
+ direction = "up" if change > 0 else "down or flat"
59
+
60
+ return (
61
+ f"Predicted next-day SGD rate: {next_sgd:.5f}\n"
62
+ f"Expected move: {direction} ({change:+.5f}, {pct_change:+.3f}%)"
63
+ )
64
+
65
+ # 3. Build Gradio UI
66
+
67
+ with gr.Blocks() as demo:
68
+ gr.Markdown(
69
+ """
70
+ # SGD Currency Movement Predictor
71
+
72
+ This demo model is trained on your DBS / SGD dataset.
73
+
74
+ Enter today's SGD rate and DBS price to predict the next day's SGD rate.
75
+ (Note: this is a simple ML model for experimentation/learning, not trading advice.)
76
+ """
77
+ )
78
+
79
+ with gr.Row():
80
+ current_sgd = gr.Number(
81
+ label="Today's SGD rate",
82
+ value=float(X.iloc[-1]["SGD_today"]),
83
+ precision=5,
84
+ )
85
+ current_dbs = gr.Number(
86
+ label="Today's DBS price",
87
+ value=float(X.iloc[-1]["DBS_today"]),
88
+ precision=2,
89
+ )
90
+
91
+ predict_button = gr.Button("Predict next-day SGD")
92
+ output = gr.Textbox(label="Prediction", lines=3)
93
+
94
+ predict_button.click(
95
+ fn=predict_next_sgd,
96
+ inputs=[current_sgd, current_dbs],
97
+ outputs=output,
98
+ )
99
+
100
+ if __name__ == "__main__":
101
+ demo.launch()