Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import yfinance as yf
|
|
| 4 |
import plotly.express as px
|
| 5 |
import pandas as pd
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
# ---------- LLM Setup ----------
|
| 9 |
client = InferenceClient("microsoft/phi-4")
|
|
@@ -51,7 +52,7 @@ class Investment_Simulator:
|
|
| 51 |
|
| 52 |
def update_portfolio_history(self):
|
| 53 |
now = datetime.now()
|
| 54 |
-
total_value = compute_portfolio_value(
|
| 55 |
self.portfolio_history.append((now, total_value))
|
| 56 |
|
| 57 |
def plot_portfolio_value(self):
|
|
@@ -79,7 +80,7 @@ class Investment_Simulator:
|
|
| 79 |
self.portfolio["cash"] -= amount
|
| 80 |
self.portfolio["stocks"][ticker] = self.portfolio["stocks"].get(ticker, 0) + qty
|
| 81 |
self.history.append(f"🟢 Bought £{amount} of {ticker} ({qty} shares at £{price:.2f})")
|
| 82 |
-
update_portfolio_history(
|
| 83 |
return f"✅ Bought {qty} shares of {ticker} at £{price:.2f}"
|
| 84 |
|
| 85 |
elif "sell" in user_input:
|
|
@@ -95,7 +96,7 @@ class Investment_Simulator:
|
|
| 95 |
self.portfolio["stocks"][ticker] -= qty_to_sell
|
| 96 |
self.portfolio["cash"] += amount
|
| 97 |
self.history.append(f"🔴 Sold £{amount} of {ticker} ({qty_to_sell} shares at £{price:.2f})")
|
| 98 |
-
update_portfolio_history(
|
| 99 |
return f"✅ Sold {qty_to_sell} shares of {ticker} at £{price:.2f}"
|
| 100 |
|
| 101 |
elif "portfolio" in user_input:
|
|
@@ -122,7 +123,7 @@ class Investment_Simulator:
|
|
| 122 |
self.portfolio["stocks"].clear()
|
| 123 |
self.history.clear()
|
| 124 |
self.portfolio_history.clear()
|
| 125 |
-
update_portfolio_history(
|
| 126 |
return "🔁 Portfolio reset."
|
| 127 |
|
| 128 |
else:
|
|
@@ -132,11 +133,52 @@ class Investment_Simulator:
|
|
| 132 |
return f"⚠️ Error: {e}"
|
| 133 |
|
| 134 |
def run_all(self, text):
|
| 135 |
-
response = process_input(
|
| 136 |
-
fig = plot_portfolio_value(
|
| 137 |
return response, fig
|
| 138 |
|
| 139 |
-
update_portfolio_history(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
# ---------- UI Layout ----------
|
| 142 |
custom_css = """
|
|
@@ -212,7 +254,7 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 212 |
|
| 213 |
Zeno_Investments = gr.Column(visible=False, elem_id="Investments")
|
| 214 |
with Zeno_Investments:
|
| 215 |
-
gr.Markdown("##
|
| 216 |
|
| 217 |
simulator = Investment_Simulator(portfolio=None, history=None, portfolio_history=None)
|
| 218 |
|
|
@@ -222,6 +264,12 @@ with gr.Blocks(css=custom_css) as demo:
|
|
| 222 |
|
| 223 |
invest_input.submit(run_all, inputs=invest_input, outputs=[invest_output, invest_chart])
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
def handle_first_input(msg):
|
| 226 |
return gr.update(visible=False), gr.update(visible=True), msg, []
|
| 227 |
|
|
|
|
| 4 |
import plotly.express as px
|
| 5 |
import pandas as pd
|
| 6 |
from datetime import datetime
|
| 7 |
+
import feedparser
|
| 8 |
|
| 9 |
# ---------- LLM Setup ----------
|
| 10 |
client = InferenceClient("microsoft/phi-4")
|
|
|
|
| 52 |
|
| 53 |
def update_portfolio_history(self):
|
| 54 |
now = datetime.now()
|
| 55 |
+
total_value = compute_portfolio_value()
|
| 56 |
self.portfolio_history.append((now, total_value))
|
| 57 |
|
| 58 |
def plot_portfolio_value(self):
|
|
|
|
| 80 |
self.portfolio["cash"] -= amount
|
| 81 |
self.portfolio["stocks"][ticker] = self.portfolio["stocks"].get(ticker, 0) + qty
|
| 82 |
self.history.append(f"🟢 Bought £{amount} of {ticker} ({qty} shares at £{price:.2f})")
|
| 83 |
+
update_portfolio_history()
|
| 84 |
return f"✅ Bought {qty} shares of {ticker} at £{price:.2f}"
|
| 85 |
|
| 86 |
elif "sell" in user_input:
|
|
|
|
| 96 |
self.portfolio["stocks"][ticker] -= qty_to_sell
|
| 97 |
self.portfolio["cash"] += amount
|
| 98 |
self.history.append(f"🔴 Sold £{amount} of {ticker} ({qty_to_sell} shares at £{price:.2f})")
|
| 99 |
+
update_portfolio_history()
|
| 100 |
return f"✅ Sold {qty_to_sell} shares of {ticker} at £{price:.2f}"
|
| 101 |
|
| 102 |
elif "portfolio" in user_input:
|
|
|
|
| 123 |
self.portfolio["stocks"].clear()
|
| 124 |
self.history.clear()
|
| 125 |
self.portfolio_history.clear()
|
| 126 |
+
update_portfolio_history()
|
| 127 |
return "🔁 Portfolio reset."
|
| 128 |
|
| 129 |
else:
|
|
|
|
| 133 |
return f"⚠️ Error: {e}"
|
| 134 |
|
| 135 |
def run_all(self, text):
|
| 136 |
+
response = process_input()
|
| 137 |
+
fig = plot_portfolio_value()
|
| 138 |
return response, fig
|
| 139 |
|
| 140 |
+
update_portfolio_history()
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
class News:
|
| 144 |
+
def __init__(self):
|
| 145 |
+
pass
|
| 146 |
+
|
| 147 |
+
def get_news(self, tickers):
|
| 148 |
+
ticker_list = [t.strip().upper() for t in tickers.split(",")]
|
| 149 |
+
|
| 150 |
+
market_url = "https://finance.yahoo.com/rss/topstories"
|
| 151 |
+
market_feed = feedparser.parse(market_url)
|
| 152 |
+
|
| 153 |
+
result = "## General Market News\n"
|
| 154 |
+
if market_feed.entries:
|
| 155 |
+
for entry in market_feed.entries[:5]:
|
| 156 |
+
result += f"**{entry.title}**\n{entry.link}\n{entry.published}\n\n"
|
| 157 |
+
else:
|
| 158 |
+
result += "_No general market news found_\n\n"
|
| 159 |
+
|
| 160 |
+
for ticker in ticker_list:
|
| 161 |
+
rss_url = f"https://feeds.finance.yahoo.com/rss/2.0/headline?s={ticker}®ion=US&lang=en-US"
|
| 162 |
+
feed = feedparser.parse(rss_url)
|
| 163 |
+
|
| 164 |
+
result += f"## News for {ticker}\n"
|
| 165 |
+
if feed.entries:
|
| 166 |
+
for entry in feed.entries[:5]:
|
| 167 |
+
result += f"**{entry.title}**\n{entry.link}\n{entry.published}\n\n"
|
| 168 |
+
else:
|
| 169 |
+
result += "_No news found_\n\n"
|
| 170 |
+
|
| 171 |
+
return result
|
| 172 |
+
|
| 173 |
+
iface = gr.Interface(
|
| 174 |
+
fn=get_news,
|
| 175 |
+
inputs=gr.Textbox(label="Enter stock tickers (comma-separated, e.g., AAPL, TSLA, MSFT)"),
|
| 176 |
+
outputs=gr.Markdown(),
|
| 177 |
+
title="Stock and Market News",
|
| 178 |
+
description="Get the latest Yahoo Finance news for the general market and specific stocks."
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
iface.launch()
|
| 182 |
|
| 183 |
# ---------- UI Layout ----------
|
| 184 |
custom_css = """
|
|
|
|
| 254 |
|
| 255 |
Zeno_Investments = gr.Column(visible=False, elem_id="Investments")
|
| 256 |
with Zeno_Investments:
|
| 257 |
+
gr.Markdown("## Live Investment Simulator (Practice £500) + Portfolio Tracker")
|
| 258 |
|
| 259 |
simulator = Investment_Simulator(portfolio=None, history=None, portfolio_history=None)
|
| 260 |
|
|
|
|
| 264 |
|
| 265 |
invest_input.submit(run_all, inputs=invest_input, outputs=[invest_output, invest_chart])
|
| 266 |
|
| 267 |
+
Zeno_News = gr.Column(visible=False, elem_id="Stock and Market News")
|
| 268 |
+
with Zeno_News:
|
| 269 |
+
gr.Markdown("## Up To Date Stock and Market News")
|
| 270 |
+
zeno_news = News()
|
| 271 |
+
|
| 272 |
+
|
| 273 |
def handle_first_input(msg):
|
| 274 |
return gr.update(visible=False), gr.update(visible=True), msg, []
|
| 275 |
|