Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +130 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,132 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
"
|
| 28 |
-
"
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import warnings
|
| 3 |
|
| 4 |
+
# --- 1. SILENCE ALL WARNINGS & ERRORS ---
|
| 5 |
+
warnings.filterwarnings("ignore", category=RuntimeWarning)
|
| 6 |
+
warnings.filterwarnings("ignore", category=FutureWarning)
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import sys
|
| 10 |
+
import logging
|
| 11 |
+
import contextlib
|
| 12 |
+
from dotenv import load_dotenv
|
| 13 |
+
import yfinance as yf
|
| 14 |
+
from duckduckgo_search import DDGS
|
| 15 |
+
from langchain_core.language_models.llms import LLM
|
| 16 |
+
from huggingface_hub import InferenceClient
|
| 17 |
+
from typing import Any, List, Optional
|
| 18 |
+
from pydantic import Field
|
| 19 |
+
|
| 20 |
+
# Silence yfinance logger
|
| 21 |
+
logging.getLogger('yfinance').setLevel(logging.CRITICAL)
|
| 22 |
+
|
| 23 |
+
@contextlib.contextmanager
|
| 24 |
+
def suppress_output():
|
| 25 |
+
"""Redirects stdout/stderr to devnull to hide yfinance errors."""
|
| 26 |
+
with open(os.devnull, "w") as devnull:
|
| 27 |
+
old_stdout, old_stderr = sys.stdout, sys.stderr
|
| 28 |
+
try:
|
| 29 |
+
sys.stdout, sys.stderr = devnull, devnull
|
| 30 |
+
yield
|
| 31 |
+
finally:
|
| 32 |
+
sys.stdout, sys.stderr = old_stdout, old_stderr
|
| 33 |
+
|
| 34 |
+
# --- CONFIG ---
|
| 35 |
+
st.set_page_config(page_title="Stock Scout", page_icon="📈", layout="centered")
|
| 36 |
+
load_dotenv()
|
| 37 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 38 |
+
|
| 39 |
+
if not hf_token:
|
| 40 |
+
st.error("Missing HF_TOKEN in .env file.")
|
| 41 |
+
st.stop()
|
| 42 |
+
|
| 43 |
+
# --- MODEL (Unbreakable Cluster) ---
|
| 44 |
+
class HFChatModel(LLM):
|
| 45 |
+
token: str = Field(...)
|
| 46 |
+
models: List[str] = [
|
| 47 |
+
"microsoft/Phi-3.5-mini-instruct",
|
| 48 |
+
"Qwen/Qwen2.5-7B-Instruct",
|
| 49 |
+
"HuggingFaceH4/zephyr-7b-beta"
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
def _call(self, prompt: str, stop: Optional[List[str]] = None, **kwargs: Any) -> str:
|
| 53 |
+
client = InferenceClient(token=self.token)
|
| 54 |
+
messages = [{"role": "user", "content": prompt}]
|
| 55 |
+
for model_id in self.models:
|
| 56 |
+
try:
|
| 57 |
+
response = client.chat_completion(
|
| 58 |
+
model=model_id, messages=messages, max_tokens=600, temperature=0.5
|
| 59 |
+
)
|
| 60 |
+
return response.choices[0].message.content
|
| 61 |
+
except:
|
| 62 |
+
continue
|
| 63 |
+
return "System Busy."
|
| 64 |
+
|
| 65 |
+
@property
|
| 66 |
+
def _llm_type(self) -> str:
|
| 67 |
+
return "custom_hf_chat_cluster"
|
| 68 |
+
|
| 69 |
+
llm = HFChatModel(token=hf_token)
|
| 70 |
+
|
| 71 |
+
# --- FUNCTIONS ---
|
| 72 |
+
def get_stock_data(ticker):
|
| 73 |
+
try:
|
| 74 |
+
with suppress_output():
|
| 75 |
+
stock = yf.Ticker(ticker)
|
| 76 |
+
hist = stock.history(period="5d")
|
| 77 |
+
if len(hist) < 2: return None, None
|
| 78 |
+
curr = hist['Close'].iloc[-1]
|
| 79 |
+
prev = hist['Close'].iloc[-2]
|
| 80 |
+
change = ((curr - prev) / prev) * 100
|
| 81 |
+
return curr, change
|
| 82 |
+
except:
|
| 83 |
+
return None, None
|
| 84 |
+
|
| 85 |
+
def analyze_news(ticker):
|
| 86 |
+
try:
|
| 87 |
+
with suppress_output():
|
| 88 |
+
results = DDGS().news(f"{ticker} stock news", max_results=5)
|
| 89 |
+
if not results: return "No recent news found."
|
| 90 |
+
|
| 91 |
+
news_context = []
|
| 92 |
+
sources_txt = "\n\n**Sources:**\n"
|
| 93 |
+
for i, res in enumerate(results, 1):
|
| 94 |
+
title = res.get('title', '?')
|
| 95 |
+
link = res.get('url', '#')
|
| 96 |
+
news_context.append(f"[Source {i}]: {title}")
|
| 97 |
+
sources_txt += f"{i}. [{title}]({link})\n"
|
| 98 |
+
|
| 99 |
+
full_text = "\n".join(news_context)
|
| 100 |
+
prompt = f"""
|
| 101 |
+
Analyze news for {ticker}:
|
| 102 |
+
{full_text}
|
| 103 |
+
1. Why is it moving? (Cite [Source X])
|
| 104 |
+
2. Sentiment: BULLISH/BEARISH/NEUTRAL
|
| 105 |
+
"""
|
| 106 |
+
return llm.invoke(prompt) + sources_txt
|
| 107 |
+
except:
|
| 108 |
+
return "Analysis failed."
|
| 109 |
+
|
| 110 |
+
# --- UI (SIMPLE DROPDOWN ONLY) ---
|
| 111 |
+
st.title("📈 Stock Scout")
|
| 112 |
+
|
| 113 |
+
# 1. The Safe List
|
| 114 |
+
POPULAR_TICKERS = [
|
| 115 |
+
"AAPL", "NVDA", "TSLA", "AMD", "AMZN", "MSFT", "GOOGL", "META",
|
| 116 |
+
"JPM", "BAC", "WMT", "DIS", "NFLX", "KO", "PEP", "BA", "INTC", "PYPL"
|
| 117 |
+
]
|
| 118 |
+
|
| 119 |
+
# 2. The Selector
|
| 120 |
+
selected_ticker = st.selectbox("Select Stock:", options=POPULAR_TICKERS)
|
| 121 |
+
|
| 122 |
+
# 3. Execution (Instant)
|
| 123 |
+
if selected_ticker:
|
| 124 |
+
price, change = get_stock_data(selected_ticker)
|
| 125 |
+
|
| 126 |
+
if price:
|
| 127 |
+
st.metric(selected_ticker, f"${price:.2f}", f"{change:.2f}%")
|
| 128 |
+
st.divider()
|
| 129 |
+
with st.spinner(f"Analyzing {selected_ticker}..."):
|
| 130 |
+
st.info(analyze_news(selected_ticker))
|
| 131 |
+
else:
|
| 132 |
+
st.error("Data unavailable.")
|