import streamlit as st import requests import pandas as pd # The URL where your FastAPI is running inside the Space API_URL = "http://localhost:7860" st.set_page_config(layout="wide") st.title("📈 ProfitBook Trading Analysis Engine") # --- Status Check --- st.header("API Status") if st.button("Check API Status"): try: response = requests.get(API_URL) if response.status_code == 200: st.success("API is operational.") st.json(response.json()) else: st.error(f"API returned status code: {response.status_code}") st.text(response.text) except requests.exceptions.ConnectionError as e: st.error(f"Failed to connect to the API. Is it running? Error: {e}") st.divider() # --- Prediction Endpoint --- st.header("Get Trading Signal") symbol = st.text_input("Enter a stock symbol (e.g., NVDA, SPY, QQQ)", "NVDA").upper() timeframe = st.selectbox("Select a timeframe", ["1m", "5m", "15m", "1h"], index=2) strategy = st.selectbox("Select a strategy", ["momentum", "scalp", "gap"], index=0) if st.button("Analyze Symbol"): with st.spinner(f"Running full-stack AI analysis for {symbol}..."): try: # The endpoint URL for prediction predict_url = f"{API_URL}/predict/enhanced/" params = { "symbol": symbol, "timeframe": timeframe, "strategy_mode": strategy } response = requests.post(predict_url, params=params) if response.status_code == 200: data = response.json() st.subheader(f"Signal for {data['symbol']}: {data['signal']}") col1, col2, col3 = st.columns(3) col1.metric("Confidence", f"{data['confidence']:.0f}%") col2.metric("Position Size", f"{data['position_size']:.2f}") col3.metric("Hold Time", data['expected_hold_time']) with st.expander("Reasoning and Strategy Details"): st.write("**Reasoning:**", data['reasoning']) st.write("**Suggested Options Strategy:**") st.json(data['options_strategy']) with st.expander("Full Analysis Data"): st.json(data) else: st.error(f"API Error: Status {response.status_code}") st.json(response.json()) except requests.exceptions.ConnectionError: st.error("Failed to connect to the API.") except Exception as e: st.error(f"An unexpected error occurred: {e}")