Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,117 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import pandas as pd
|
| 4 |
-
import
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
st.
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
#
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
prediction = predict_next(df)
|
| 114 |
-
# Adjust prediction with sentiment (2% weight)
|
| 115 |
-
sentiment_adj = prediction + (prediction * sentiment * 0.02)
|
| 116 |
-
|
| 117 |
-
# Plot historical + predicted
|
| 118 |
-
fig, ax = plt.subplots()
|
| 119 |
-
ax.plot(df.index, df["Close"], label="Historical Price")
|
| 120 |
-
ax.axhline(sentiment_adj, linestyle="--", color="red", label="Predicted Price")
|
| 121 |
-
ax.set_title(f"{symbol} Stock Price Prediction")
|
| 122 |
-
ax.legend()
|
| 123 |
-
st.pyplot(fig)
|
| 124 |
-
|
| 125 |
-
# Display results
|
| 126 |
-
st.subheader("Prediction Result")
|
| 127 |
-
st.write(f"**Predicted Price:** Rs {sentiment_adj:.2f}")
|
| 128 |
-
st.write(f"**Sentiment Impact:** {sentiment:.3f}")
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import plotly.graph_objects as go
|
| 3 |
+
from groq import Groq
|
| 4 |
import numpy as np
|
| 5 |
import pandas as pd
|
| 6 |
+
import yfinance as yf
|
|
|
|
| 7 |
|
| 8 |
+
# prediction module
|
| 9 |
+
from predict_live import predict_next_price, get_live_data
|
| 10 |
+
# Streamlit Setup
|
| 11 |
+
|
| 12 |
+
st.set_page_config(page_title="π PSX Investment Advisor", layout="wide", page_icon="π")
|
| 13 |
+
st.title("π PSX Investment Dashboard")
|
| 14 |
+
st.write("GenAI-powered stock insights with LIVE LSTM predictions")
|
| 15 |
+
|
| 16 |
+
# Sidebar Inputs
|
| 17 |
+
st.sidebar.header("Investment Options")
|
| 18 |
+
investment_type = st.sidebar.radio("Investment Type:", ["Short Term", "Long Term"])
|
| 19 |
+
sector = st.sidebar.selectbox("Sector:", ["Banking", "Energy"])
|
| 20 |
+
stock = st.sidebar.text_input("Stock Symbol:", "HBL") # user enters HBL
|
| 21 |
+
|
| 22 |
+
#PSX ticker format
|
| 23 |
+
ticker = stock.upper() + ".KA"
|
| 24 |
+
# LIVE Prediction
|
| 25 |
try:
|
| 26 |
+
close_prices = get_live_data(ticker)
|
| 27 |
+
predicted_price = predict_next_price(ticker)
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
st.error(f"Error fetching live data: {e}")
|
| 31 |
+
st.stop()
|
| 32 |
+
# Dummy sentiment
|
| 33 |
+
sentiment_score = 0.10
|
| 34 |
+
sentiment_adjusted_pred = predicted_price + (predicted_price * sentiment_score * 0.02)
|
| 35 |
+
|
| 36 |
+
# Dashboard
|
| 37 |
+
col1, col2, col3 = st.columns(3)
|
| 38 |
+
col1.metric("Selected Stock", stock)
|
| 39 |
+
col2.metric("Live Last Price", f"Rs {close_prices[-1]:.2f}")
|
| 40 |
+
col3.metric("LSTM Prediction", f"Rs {sentiment_adjusted_pred:.2f}")
|
| 41 |
+
|
| 42 |
+
#Plot
|
| 43 |
+
def plot_stock(close_prices, predicted_price):
|
| 44 |
+
fig = go.Figure()
|
| 45 |
+
|
| 46 |
+
# Plot live historical
|
| 47 |
+
fig.add_trace(go.Scatter(
|
| 48 |
+
y=close_prices,
|
| 49 |
+
x=list(range(len(close_prices))),
|
| 50 |
+
mode='lines',
|
| 51 |
+
name='Live Prices'
|
| 52 |
+
))
|
| 53 |
+
|
| 54 |
+
# Plot prediction
|
| 55 |
+
fig.add_trace(go.Scatter(
|
| 56 |
+
x=[len(close_prices)],
|
| 57 |
+
y=[predicted_price],
|
| 58 |
+
mode='markers',
|
| 59 |
+
name='Predicted Next Price',
|
| 60 |
+
marker=dict(size=12)
|
| 61 |
+
))
|
| 62 |
+
|
| 63 |
+
fig.update_layout(
|
| 64 |
+
title=f"{stock} Live Price + Prediction",
|
| 65 |
+
xaxis_title="Time",
|
| 66 |
+
yaxis_title="Price (Rs)",
|
| 67 |
+
template="plotly_dark"
|
| 68 |
+
)
|
| 69 |
+
return fig
|
| 70 |
+
|
| 71 |
+
st.subheader("Live Price Chart")
|
| 72 |
+
st.plotly_chart(plot_stock(close_prices, sentiment_adjusted_pred), use_container_width=True)
|
| 73 |
+
|
| 74 |
+
st.subheader("Prediction Result")
|
| 75 |
+
st.write(f"**LSTM Next Price Prediction:** Rs {sentiment_adjusted_pred:.2f}")
|
| 76 |
+
st.write(f"**Sentiment Impact:** {sentiment_score:.2f}")
|
| 77 |
+
|
| 78 |
+
# AI Chatbox via Groq
|
| 79 |
+
|
| 80 |
+
st.subheader("π¬ Ask Your Investment Advisor")
|
| 81 |
+
|
| 82 |
+
user_input = st.text_input(
|
| 83 |
+
"Your question to AI:",
|
| 84 |
+
f"Should I invest in {stock} for {investment_type.lower()}?"
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
if st.button("Ask AI"):
|
| 88 |
+
if user_input:
|
| 89 |
+
with st.spinner("Thinking..."):
|
| 90 |
+
|
| 91 |
+
client = Groq(api_key=st.secrets["GROQ_API_KEY"])
|
| 92 |
+
|
| 93 |
+
advisor_prompt = f"""
|
| 94 |
+
You are a financial advisor AI.
|
| 95 |
+
|
| 96 |
+
Use the following data:
|
| 97 |
+
Current Price: {close_prices[-1]}
|
| 98 |
+
Predicted Next Price: {sentiment_adjusted_pred}
|
| 99 |
+
User Question: {user_input}
|
| 100 |
+
|
| 101 |
+
Respond MUST:
|
| 102 |
+
- Give a clear BUY / SELL / HOLD
|
| 103 |
+
- Explain in 2β3 simple lines
|
| 104 |
+
- Mention risk in simple terms
|
| 105 |
+
- 1 friendly tip
|
| 106 |
+
- No complex financial jargon
|
| 107 |
+
"""
|
| 108 |
+
response = client.chat.completions.create(
|
| 109 |
+
model="openai/gpt-oss-120b",
|
| 110 |
+
messages=[
|
| 111 |
+
{"role": "system", "content": "You are a professional stock advisor."},
|
| 112 |
+
{"role": "user", "content": advisor_prompt}
|
| 113 |
+
]
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
answer = response.choices[0].message.content
|
| 117 |
+
st.markdown(f"**AI:** {answer}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|