Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,62 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
-
from datetime import datetime, timedelta
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
# LOAD MODEL
|
| 10 |
-
# ---------------------------
|
| 11 |
model = load_model("model.h5")
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
# ---------------------------
|
| 17 |
-
# STREAMLIT UI
|
| 18 |
-
# ---------------------------
|
| 19 |
st.title("π PSX Stock Prediction (Hamza Jadoon β FYP)")
|
| 20 |
-
st.write("This app predicts the next-day closing price using your trained ML model.")
|
| 21 |
|
| 22 |
-
|
| 23 |
-
symbol = st.text_input("Enter PSX Stock Symbol (e.g., UBL, ENGRO, OGDC):", "OGDC")
|
| 24 |
|
| 25 |
-
# Predict button
|
| 26 |
if st.button("Predict"):
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
if df.empty:
|
| 41 |
-
st.error("β No data found for this symbol.")
|
| 42 |
else:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
window = 10
|
| 52 |
-
if len(df) < window:
|
| 53 |
-
st.error("Not enough data for prediction.")
|
| 54 |
-
else:
|
| 55 |
-
X = df["Close"].values[-window:]
|
| 56 |
-
X = X.reshape(1, window, 1)
|
| 57 |
-
|
| 58 |
-
# ---------------------------
|
| 59 |
-
# PREDICT
|
| 60 |
-
# ---------------------------
|
| 61 |
-
prediction = model.predict(X)[0][0]
|
| 62 |
-
|
| 63 |
-
st.subheader("π Prediction")
|
| 64 |
-
st.write(f"**Next Close Price:** Rs {prediction:.2f}")
|
| 65 |
-
|
| 66 |
-
# Chart
|
| 67 |
-
st.line_chart(df["Close"])
|
| 68 |
-
|
| 69 |
-
except Exception as e:
|
| 70 |
-
st.error(f"Error: {e}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
+
import requests
|
| 5 |
from tensorflow.keras.models import load_model
|
|
|
|
| 6 |
|
| 7 |
+
# Load your saved model
|
|
|
|
|
|
|
| 8 |
model = load_model("model.h5")
|
| 9 |
|
| 10 |
+
API_KEY = "demo" # AlphaVantage public key (works for testing)
|
| 11 |
+
|
| 12 |
+
def get_psx_data(symbol):
|
| 13 |
+
"""Fetch PSX stock data using AlphaVantage"""
|
| 14 |
+
url = f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol={symbol}.XPSX&apikey={API_KEY}"
|
| 15 |
+
r = requests.get(url).json()
|
| 16 |
+
|
| 17 |
+
if "Time Series (Daily)" not in r:
|
| 18 |
+
return None
|
| 19 |
+
|
| 20 |
+
data = r["Time Series (Daily)"]
|
| 21 |
+
df = pd.DataFrame(data).T
|
| 22 |
+
df.index = pd.to_datetime(df.index)
|
| 23 |
+
df = df.sort_index()
|
| 24 |
+
|
| 25 |
+
df = df.rename(columns={
|
| 26 |
+
"1. open": "Open",
|
| 27 |
+
"2. high": "High",
|
| 28 |
+
"3. low": "Low",
|
| 29 |
+
"4. close": "Close",
|
| 30 |
+
"5. volume": "Volume"
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
df = df.astype(float)
|
| 34 |
+
return df
|
| 35 |
+
|
| 36 |
|
|
|
|
|
|
|
|
|
|
| 37 |
st.title("π PSX Stock Prediction (Hamza Jadoon β FYP)")
|
|
|
|
| 38 |
|
| 39 |
+
symbol = st.text_input("Enter Stock Symbol (ex: OGDC, HBL, ENGRO):", "OGDC")
|
|
|
|
| 40 |
|
|
|
|
| 41 |
if st.button("Predict"):
|
| 42 |
+
df = get_psx_data(symbol)
|
| 43 |
+
|
| 44 |
+
if df is None:
|
| 45 |
+
st.error("β Failed to load PSX data. Try another symbol.")
|
| 46 |
+
else:
|
| 47 |
+
st.success("Data loaded!")
|
| 48 |
+
|
| 49 |
+
st.line_chart(df["Close"])
|
| 50 |
+
|
| 51 |
+
# Last 10 days
|
| 52 |
+
if len(df) < 10:
|
| 53 |
+
st.error("Not enough data (need 10 days).")
|
|
|
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
+
X = df["Close"].values[-10:]
|
| 56 |
+
X = X.reshape(1, 10, 1)
|
| 57 |
+
|
| 58 |
+
# Predict
|
| 59 |
+
result = model.predict(X)[0][0]
|
| 60 |
+
|
| 61 |
+
st.subheader("π Prediction Result")
|
| 62 |
+
st.write(f"**Next Close Price:** Rs {result:.2f}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|