Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,6 +20,66 @@ import re
|
|
| 20 |
import json
|
| 21 |
import os
|
| 22 |
from googleapiclient.discovery import build
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# Load the LSTM model
|
| 25 |
model = tf.keras.models.load_model("nse_lstm_model_fixed.h5")
|
|
@@ -689,6 +749,18 @@ Diversification, review cycles (quarterly), and informed decisions based on pred
|
|
| 689 |
with gr.Blocks(css=custom_css) as app:
|
| 690 |
with gr.Tabs():
|
| 691 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 692 |
# π Prediction Tab
|
| 693 |
with gr.Tab("π Prediction"):
|
| 694 |
gr.Markdown("## π Stock Prediction with LSTM & Sentiment")
|
|
@@ -718,7 +790,6 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 718 |
sentiment_chart = gr.Image(label="π§ Sentiment Score Comparison", height=250)
|
| 719 |
shap_img = gr.Image(label="π§ Feature Influence (Approximate)", height=250)
|
| 720 |
|
| 721 |
-
|
| 722 |
with gr.Row():
|
| 723 |
stock_news = gr.Textbox(label="π° Stock Headlines", lines=4)
|
| 724 |
industry_news = gr.Textbox(label="π Industry Headlines", lines=4)
|
|
@@ -738,23 +809,23 @@ with gr.Blocks(css=custom_css) as app:
|
|
| 738 |
rec_btn = gr.Button("π§ Personalized Advice")
|
| 739 |
advice_box = gr.Textbox(label="π AI-Powered Investment Recommendation", lines=25, show_copy_button=True)
|
| 740 |
|
| 741 |
-
# π§ Market Mind Reader
|
| 742 |
with gr.Tab("π§ Market Mind Reader"):
|
| 743 |
gr.Markdown("### π§ Predict Retail Investor Behavior Using Sentiment + Fundamentals")
|
| 744 |
mm_symbol = gr.Textbox(label="Enter Stock Symbol", value="RELIANCE.NS")
|
| 745 |
mm_btn = gr.Button("π Analyze Behavior")
|
| 746 |
mm_output = gr.Textbox(label="π§ Behavior Prediction", lines=10, show_copy_button=True)
|
| 747 |
|
| 748 |
-
# π§ Greed & Fear Gauge
|
| 749 |
with gr.Tab("π§ Greed & Fear Gauge"):
|
| 750 |
gr.Markdown("### π§ Real-Time Greed & Fear Score")
|
| 751 |
gr.HTML("""
|
| 752 |
-
|
| 753 |
-
|
| 754 |
-
|
| 755 |
-
|
| 756 |
-
|
| 757 |
-
|
| 758 |
gf_symbol = gr.Textbox(label="Enter Stock Symbol", value="RELIANCE.NS")
|
| 759 |
gf_btn = gr.Button("π Show Market Mood")
|
| 760 |
gf_output = gr.Textbox(label="π Greed & Fear Result", lines=5, show_copy_button=True)
|
|
@@ -840,31 +911,28 @@ Made with β€οΈ for investors who want smarter decisions.
|
|
| 840 |
β οΈ <strong>Disclaimer</strong>: This application is for educational purposes only. All AI-based predictions and insights are illustrative and should not be considered financial advice. Always consult a licensed financial advisor before investing.
|
| 841 |
</div>
|
| 842 |
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 843 |
|
| 844 |
# π Button Bindings
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
predict_btn.click(predict_price, inputs=symbol, outputs=[
|
| 850 |
-
pred, reco, reason,
|
| 851 |
-
|
| 852 |
-
stock_news, industry_news, finance,
|
| 853 |
-
tech_box, corp_actions_box, sector_chart,
|
| 854 |
-
shap_img
|
| 855 |
])
|
| 856 |
-
|
| 857 |
-
|
| 858 |
-
port_summary, port_chart
|
| 859 |
])
|
| 860 |
-
|
| 861 |
rec_btn.click(generate_recommendation, inputs=port_summary, outputs=advice_box)
|
| 862 |
-
|
| 863 |
mm_btn.click(market_mind_reader, inputs=mm_symbol, outputs=mm_output)
|
| 864 |
gf_btn.click(greed_fear_gauge, inputs=gf_symbol, outputs=gf_output)
|
| 865 |
-
|
| 866 |
|
| 867 |
-
# π Launch App
|
| 868 |
app.launch()
|
| 869 |
|
| 870 |
|
|
|
|
| 20 |
import json
|
| 21 |
import os
|
| 22 |
from googleapiclient.discovery import build
|
| 23 |
+
import firebase_admin
|
| 24 |
+
from firebase_admin import credentials, firestore
|
| 25 |
+
from datetime import datetime
|
| 26 |
+
|
| 27 |
+
# Firebase config
|
| 28 |
+
firebase_config = {
|
| 29 |
+
"apiKey": "AIzaSyAU02LKQngy8e7JUAzwH7DY8HY6fdWYtPI",
|
| 30 |
+
"authDomain": "stock-predictor-566ce.firebaseapp.com",
|
| 31 |
+
"databaseURL": "https://stock-predictor-566ce-default-rtdb.asia-southeast1.firebasedatabase.app",
|
| 32 |
+
"projectId": "stock-predictor-566ce",
|
| 33 |
+
"storageBucket": "stock-predictor-566ce.appspot.com",
|
| 34 |
+
"messagingSenderId": "YOUR_SENDER_ID",
|
| 35 |
+
"appId": "YOUR_APP_ID"
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
firebase = pyrebase.initialize_app(firebase_config)
|
| 39 |
+
auth = firebase.auth()
|
| 40 |
+
|
| 41 |
+
cred = credentials.Certificate("firebase-service-key.json")
|
| 42 |
+
firebase_admin.initialize_app(cred)
|
| 43 |
+
firestore_db = firestore.client()
|
| 44 |
+
|
| 45 |
+
current_uid = None # To track logged in user
|
| 46 |
+
|
| 47 |
+
# --- Part 2: Auth Functions ---
|
| 48 |
+
def signup_user(email, password):
|
| 49 |
+
try:
|
| 50 |
+
user = auth.create_user_with_email_and_password(email, password)
|
| 51 |
+
auth.send_email_verification(user['idToken'])
|
| 52 |
+
return "β
Signup successful! Please verify your email."
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return f"β Signup error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
def login_user(email, password):
|
| 57 |
+
global current_uid
|
| 58 |
+
try:
|
| 59 |
+
user = auth.sign_in_with_email_and_password(email, password)
|
| 60 |
+
user_info = auth.get_account_info(user['idToken'])
|
| 61 |
+
current_uid = user_info['users'][0]['localId']
|
| 62 |
+
return current_uid, "β
Login successful!"
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return None, f"β Login error: {str(e)}"
|
| 65 |
+
|
| 66 |
+
def logout_user():
|
| 67 |
+
global current_uid
|
| 68 |
+
current_uid = None
|
| 69 |
+
return "π Logged out."
|
| 70 |
+
|
| 71 |
+
# --- Part 3: History Functions ---
|
| 72 |
+
def save_user_history(uid, action_type, data):
|
| 73 |
+
firestore_db.collection("user_history").add({
|
| 74 |
+
"uid": uid,
|
| 75 |
+
"type": action_type, # 'prediction' or 'portfolio'
|
| 76 |
+
"data": data,
|
| 77 |
+
"timestamp": datetime.now().isoformat()
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
def get_user_history(uid):
|
| 81 |
+
docs = firestore_db.collection("user_history").where("uid", "==", uid).stream()
|
| 82 |
+
return [doc.to_dict() for doc in docs]
|
| 83 |
|
| 84 |
# Load the LSTM model
|
| 85 |
model = tf.keras.models.load_model("nse_lstm_model_fixed.h5")
|
|
|
|
| 749 |
with gr.Blocks(css=custom_css) as app:
|
| 750 |
with gr.Tabs():
|
| 751 |
|
| 752 |
+
# π Login Tab
|
| 753 |
+
with gr.Tab("π Login / Signup"):
|
| 754 |
+
gr.Markdown("### π Welcome to Trade Sense")
|
| 755 |
+
with gr.Row():
|
| 756 |
+
email_input = gr.Textbox(label="Email")
|
| 757 |
+
password_input = gr.Textbox(label="Password", type="password")
|
| 758 |
+
with gr.Row():
|
| 759 |
+
login_btn = gr.Button("Login")
|
| 760 |
+
signup_btn = gr.Button("Sign Up")
|
| 761 |
+
logout_btn = gr.Button("Logout")
|
| 762 |
+
auth_status = gr.Textbox(label="Auth Status", interactive=False)
|
| 763 |
+
|
| 764 |
# π Prediction Tab
|
| 765 |
with gr.Tab("π Prediction"):
|
| 766 |
gr.Markdown("## π Stock Prediction with LSTM & Sentiment")
|
|
|
|
| 790 |
sentiment_chart = gr.Image(label="π§ Sentiment Score Comparison", height=250)
|
| 791 |
shap_img = gr.Image(label="π§ Feature Influence (Approximate)", height=250)
|
| 792 |
|
|
|
|
| 793 |
with gr.Row():
|
| 794 |
stock_news = gr.Textbox(label="π° Stock Headlines", lines=4)
|
| 795 |
industry_news = gr.Textbox(label="π Industry Headlines", lines=4)
|
|
|
|
| 809 |
rec_btn = gr.Button("π§ Personalized Advice")
|
| 810 |
advice_box = gr.Textbox(label="π AI-Powered Investment Recommendation", lines=25, show_copy_button=True)
|
| 811 |
|
| 812 |
+
# π§ Market Mind Reader
|
| 813 |
with gr.Tab("π§ Market Mind Reader"):
|
| 814 |
gr.Markdown("### π§ Predict Retail Investor Behavior Using Sentiment + Fundamentals")
|
| 815 |
mm_symbol = gr.Textbox(label="Enter Stock Symbol", value="RELIANCE.NS")
|
| 816 |
mm_btn = gr.Button("π Analyze Behavior")
|
| 817 |
mm_output = gr.Textbox(label="π§ Behavior Prediction", lines=10, show_copy_button=True)
|
| 818 |
|
| 819 |
+
# π§ Greed & Fear Gauge
|
| 820 |
with gr.Tab("π§ Greed & Fear Gauge"):
|
| 821 |
gr.Markdown("### π§ Real-Time Greed & Fear Score")
|
| 822 |
gr.HTML("""
|
| 823 |
+
<div style='font-size:15px;'>
|
| 824 |
+
π₯ <span style='color:red; font-weight:bold;'>Extreme Greed</span> > 70ββ
|
| 825 |
+
π¨ <span style='color:orange; font-weight:bold;'>Neutral/Caution</span> 30β70ββ
|
| 826 |
+
π© <span style='color:green; font-weight:bold;'>Fear/Panic</span> < 30
|
| 827 |
+
</div>
|
| 828 |
+
""")
|
| 829 |
gf_symbol = gr.Textbox(label="Enter Stock Symbol", value="RELIANCE.NS")
|
| 830 |
gf_btn = gr.Button("π Show Market Mood")
|
| 831 |
gf_output = gr.Textbox(label="π Greed & Fear Result", lines=5, show_copy_button=True)
|
|
|
|
| 911 |
β οΈ <strong>Disclaimer</strong>: This application is for educational purposes only. All AI-based predictions and insights are illustrative and should not be considered financial advice. Always consult a licensed financial advisor before investing.
|
| 912 |
</div>
|
| 913 |
""")
|
| 914 |
+
with gr.Tab("π My History"):
|
| 915 |
+
gr.Markdown("### π View Your Saved History")
|
| 916 |
+
load_history_btn = gr.Button("Load My History")
|
| 917 |
+
history_output = gr.Textbox(label="Your History", lines=12, interactive=False)
|
| 918 |
|
| 919 |
# π Button Bindings
|
| 920 |
+
login_btn.click(fn=login_user, inputs=[email_input, password_input], outputs=[None, auth_status])
|
| 921 |
+
signup_btn.click(fn=signup_user, inputs=[email_input, password_input], outputs=auth_status)
|
| 922 |
+
logout_btn.click(fn=logout_user, outputs=auth_status)
|
|
|
|
| 923 |
predict_btn.click(predict_price, inputs=symbol, outputs=[
|
| 924 |
+
pred, reco, reason, chart1, chart2, volume_chart, sentiment_chart,
|
| 925 |
+
stock_news, industry_news, finance, tech_box, corp_actions_box, sector_chart, shap_img
|
|
|
|
|
|
|
|
|
|
| 926 |
])
|
| 927 |
+
refresh_btn.click(fetch_live_snapshot, inputs=symbol, outputs=[
|
| 928 |
+
live_price, day_range, rsi_val, pe_val, bb_range, week_52
|
|
|
|
| 929 |
])
|
| 930 |
+
simulate_btn.click(simulate_portfolio, inputs=portfolio_input, outputs=[port_summary, port_chart])
|
| 931 |
rec_btn.click(generate_recommendation, inputs=port_summary, outputs=advice_box)
|
|
|
|
| 932 |
mm_btn.click(market_mind_reader, inputs=mm_symbol, outputs=mm_output)
|
| 933 |
gf_btn.click(greed_fear_gauge, inputs=gf_symbol, outputs=gf_output)
|
| 934 |
+
load_history_btn.click(fn=show_history, outputs=history_output)
|
| 935 |
|
|
|
|
| 936 |
app.launch()
|
| 937 |
|
| 938 |
|