import gradio as gr from huggingface_hub import InferenceClient import yfinance as yf import plotly.express as px import pandas as pd from datetime import datetime # ---------- LLM Setup ---------- client = InferenceClient("microsoft/phi-4") def random_respond(message, history): messages = [{ "role": "system", "content": "You are an expert financial advisor and teacher from LSE and Oxford. You help young people understand finance by breaking down complex terms clearly." }] if history: for user_msg, bot_msg in history: messages.append({"role": "user", "content": user_msg}) messages.append({"role": "assistant", "content": bot_msg}) messages.append({"role": "user", "content": message}) response = client.chat_completion(messages, max_tokens=1000) return response['choices'][0]['message']['content'].strip() # ---------- Investment Simulator Setup ---------- portfolio = {"cash": 500.0, "stocks": {}} history = [] portfolio_history = [] def get_price(ticker): try: data = yf.Ticker(ticker) todays_data = data.history(period='1d', interval='1m') latest_price = todays_data['Close'].dropna().iloc[-1] return float(latest_price) except: return None def compute_portfolio_value(): total_stock_value = 0.0 for ticker, qty in portfolio["stocks"].items(): price = get_price(ticker) if price: total_stock_value += qty * price return portfolio["cash"] + total_stock_value def update_portfolio_history(): now = datetime.now() total_value = compute_portfolio_value() portfolio_history.append((now, total_value)) def plot_portfolio_value(): if not portfolio_history: return None df = pd.DataFrame(portfolio_history, columns=["Time", "Value"]) fig = px.line(df, x="Time", y="Value", title="📈 Portfolio Value Over Time", labels={"Time": "Time", "Value": "Value (£)"}) fig.update_layout(xaxis_rangeslider_visible=True) return fig def process_input(user_input): global portfolio, history user_input = user_input.lower() try: if "buy" in user_input: amount = int(user_input.split("£")[1].split()[0]) ticker = user_input.split("of")[1].strip().upper() price = get_price(ticker) if not price: return f"❌ Couldn't fetch price for {ticker}." qty = round(amount / price, 4) if amount > portfolio["cash"]: return f"❌ Insufficient funds (£{portfolio['cash']:.2f})." portfolio["cash"] -= amount portfolio["stocks"][ticker] = portfolio["stocks"].get(ticker, 0) + qty history.append(f"🟢 Bought £{amount} of {ticker} ({qty} shares at £{price:.2f})") update_portfolio_history() return f"✅ Bought {qty} shares of {ticker} at £{price:.2f}" elif "sell" in user_input: amount = int(user_input.split("£")[1].split()[0]) ticker = user_input.split("of")[1].strip().upper() price = get_price(ticker) if not price: return f"❌ Couldn't fetch price for {ticker}." qty_to_sell = round(amount / price, 4) current_qty = portfolio["stocks"].get(ticker, 0) if qty_to_sell > current_qty: return f"❌ You don't own enough of {ticker}." portfolio["stocks"][ticker] -= qty_to_sell portfolio["cash"] += amount history.append(f"🔴 Sold £{amount} of {ticker} ({qty_to_sell} shares at £{price:.2f})") update_portfolio_history() return f"✅ Sold {qty_to_sell} shares of {ticker} at £{price:.2f}" elif "portfolio" in user_input: cash = portfolio['cash'] total_stock_value = 0.0 summary = [f"💰 Cash: £{cash:.2f}"] for ticker, qty in portfolio["stocks"].items(): live_price = get_price(ticker) if live_price: value = qty * live_price total_stock_value += value summary.append(f"📈 {ticker}: {qty:.4f} shares (£{value:.2f})") else: summary.append(f"📈 {ticker}: {qty:.4f} shares (price unavailable)") total_value = cash + total_stock_value summary.append(f"\n💼 Total Portfolio Value: £{total_value:.2f}") return "\n".join(summary) elif "history" in user_input: return "\n".join(history[-5:]) or "No trades yet." elif "reset" in user_input: portfolio["cash"] = 500.0 portfolio["stocks"].clear() history.clear() portfolio_history.clear() update_portfolio_history() return "🔁 Portfolio reset." else: return "❓ Try commands like:\n• Buy £100 of AAPL\n• Sell £50 of TSLA\n• Show portfolio" except Exception as e: return f"⚠️ Error: {e}" def run_all(text): response = process_input(text) fig = plot_portfolio_value() return response, fig update_portfolio_history() # ---------- UI Layout ---------- custom_css = """ #ZenoLogo { display: block; margin-left: auto; margin-right: auto; width: 200px; } #landing-content { text-align: center; margin-top: 100px; } #landing_page { background-color: #AEC3B0; padding: 50px; min-height: 100vh; overflow: hidden; } #sidebar-toggle { font-size: 5em; background: none; border: none; margin: 10px; cursor: pointer; } #sidebar { color: Black; background-color: #124559; border-right: 1px solid #ddd; padding: 10px; } """ with gr.Blocks(css=custom_css) as demo: active_section = gr.State("chat") sidebar_open = gr.State(True) landing_page = gr.Column(visible=True, elem_id="landing_page") with landing_page: gr.HTML("""