ckharche commited on
Commit
6a9fb3f
·
verified ·
1 Parent(s): 22e34f0

Update streamlit_app.py

Browse files
Files changed (1) hide show
  1. streamlit_app.py +71 -0
streamlit_app.py CHANGED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+
5
+ # The URL where your FastAPI is running inside the Space
6
+ API_URL = "http://localhost:7860"
7
+
8
+ st.set_page_config(layout="wide")
9
+ st.title("📈 ProfitBook Trading Analysis Engine")
10
+
11
+ # --- Status Check ---
12
+ st.header("API Status")
13
+ if st.button("Check API Status"):
14
+ try:
15
+ response = requests.get(API_URL)
16
+ if response.status_code == 200:
17
+ st.success("API is operational.")
18
+ st.json(response.json())
19
+ else:
20
+ st.error(f"API returned status code: {response.status_code}")
21
+ st.text(response.text)
22
+ except requests.exceptions.ConnectionError as e:
23
+ st.error(f"Failed to connect to the API. Is it running? Error: {e}")
24
+
25
+ st.divider()
26
+
27
+ # --- Prediction Endpoint ---
28
+ st.header("Get Trading Signal")
29
+ symbol = st.text_input("Enter a stock symbol (e.g., NVDA, SPY, QQQ)", "NVDA").upper()
30
+ timeframe = st.selectbox("Select a timeframe", ["1m", "5m", "15m", "1h"], index=2)
31
+ strategy = st.selectbox("Select a strategy", ["momentum", "scalp", "gap"], index=0)
32
+
33
+ if st.button("Analyze Symbol"):
34
+ with st.spinner(f"Running full-stack AI analysis for {symbol}..."):
35
+ try:
36
+ # The endpoint URL for prediction
37
+ predict_url = f"{API_URL}/predict/enhanced/"
38
+ params = {
39
+ "symbol": symbol,
40
+ "timeframe": timeframe,
41
+ "strategy_mode": strategy
42
+ }
43
+
44
+ response = requests.post(predict_url, params=params)
45
+
46
+ if response.status_code == 200:
47
+ data = response.json()
48
+
49
+ st.subheader(f"Signal for {data['symbol']}: {data['signal']}")
50
+
51
+ col1, col2, col3 = st.columns(3)
52
+ col1.metric("Confidence", f"{data['confidence']:.0f}%")
53
+ col2.metric("Position Size", f"{data['position_size']:.2f}")
54
+ col3.metric("Hold Time", data['expected_hold_time'])
55
+
56
+ with st.expander("Reasoning and Strategy Details"):
57
+ st.write("**Reasoning:**", data['reasoning'])
58
+ st.write("**Suggested Options Strategy:**")
59
+ st.json(data['options_strategy'])
60
+
61
+ with st.expander("Full Analysis Data"):
62
+ st.json(data)
63
+
64
+ else:
65
+ st.error(f"API Error: Status {response.status_code}")
66
+ st.json(response.json())
67
+
68
+ except requests.exceptions.ConnectionError:
69
+ st.error("Failed to connect to the API.")
70
+ except Exception as e:
71
+ st.error(f"An unexpected error occurred: {e}")