Delete app.py
Browse files
app.py
DELETED
|
@@ -1,69 +0,0 @@
|
|
| 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 (1)")
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|