modified: streamlit_slop_with_models.py

This commit is contained in:
Russell Ballestrini 2025-04-05 18:36:05 -04:00
parent 0fcd05a7a1
commit a5cc1871d3

View file

@ -1,6 +1,14 @@
import streamlit as st
import requests
import logging
# Configure logging to output to terminal for debugging
logging.basicConfig(
level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s"
)
logger = logging.getLogger(__name__)
# API endpoint
# BASE_URL = "http://localhost:31337"
BASE_URL = "https://slop.unturf.com"
@ -161,23 +169,34 @@ def tools_interface():
# Initialize session state for blackjack game ID
if "blackjack_game_id" not in st.session_state:
st.session_state.blackjack_game_id = None
logger.debug("Initialized blackjack_game_id as None in session state")
# Start a new game
if st.session_state.blackjack_game_id is None:
bet = st.number_input("Bet Amount", min_value=1, step=1, value=10)
if st.button("Start Game"):
logger.debug(
f"Starting blackjack with bet: {bet}, agent_id: {agent_id}"
)
try:
response = requests.post(
f"{BASE_URL}/tools/blackjack",
json={"bet": bet, "agent_id": agent_id},
timeout=5,
)
logger.debug(
f"POST /tools/blackjack status: {response.status_code}, content: {response.text}"
)
response.raise_for_status()
result = response.json()
if "error" in result:
st.error(f"Error: {result['error']}")
logger.error(f"Blackjack start error: {result['error']}")
else:
st.session_state.blackjack_game_id = result["game_id"]
logger.debug(
f"Set blackjack_game_id: {st.session_state.blackjack_game_id}"
)
st.write(f"**Game Started**: {result['explanation']}")
st.write(
f"**Your Hand**: {result['game_state']['player_hand']} (Total: {result['game_state']['player_total']})"
@ -187,14 +206,21 @@ def tools_interface():
)
st.rerun()
except requests.RequestException as e:
st.error(f"Error: {str(e)}")
st.error(f"Error starting blackjack: {str(e)}")
logger.error(f"Error starting blackjack: {str(e)}", exc_info=True)
else:
# Display current game state and allow hit/stand
logger.debug(
f"Fetching game state for game_id: {st.session_state.blackjack_game_id}"
)
try:
response = requests.get(
f"{BASE_URL}/resources/casino/blackjack/games/{st.session_state.blackjack_game_id}",
timeout=5,
)
logger.debug(
f"GET /resources/.../{st.session_state.blackjack_game_id} status: {response.status_code}, content: {response.text}"
)
response.raise_for_status()
game_state = response.json()["content"]
st.write(
@ -209,6 +235,9 @@ def tools_interface():
col1, col2 = st.columns(2)
with col1:
if st.button("Hit"):
logger.debug(
f"Sending hit action for game_id: {st.session_state.blackjack_game_id}"
)
try:
response = requests.post(
f"{BASE_URL}/tools/blackjack",
@ -219,23 +248,36 @@ def tools_interface():
},
timeout=5,
)
logger.debug(
f"POST /tools/blackjack (hit) status: {response.status_code}, content: {response.text}"
)
response.raise_for_status()
result = response.json()
if "error" in result:
st.error(f"Error: {result['error']}")
logger.error(f"Hit error: {result['error']}")
else:
st.write(f"**Result**: {result['explanation']}")
if result["game_complete"]:
st.write(f"**Payout**: {result['payout']}")
st.write(
f"**Payout**: {result.get('payout', 'N/A')}"
)
st.write(
f"**New Balance**: {result['new_balance']}"
)
st.session_state.blackjack_game_id = None
logger.debug(
"Game completed, reset blackjack_game_id to None"
)
st.rerun()
except requests.RequestException as e:
st.error(f"Error: {str(e)}")
st.error(f"Error during hit: {str(e)}")
logger.error(f"Error during hit: {str(e)}", exc_info=True)
with col2:
if st.button("Stand"):
logger.debug(
f"Sending stand action for game_id: {st.session_state.blackjack_game_id}"
)
try:
response = requests.post(
f"{BASE_URL}/tools/blackjack",
@ -246,10 +288,14 @@ def tools_interface():
},
timeout=5,
)
logger.debug(
f"POST /tools/blackjack (stand) status: {response.status_code}, content: {response.text}"
)
response.raise_for_status()
result = response.json()
if "error" in result:
st.error(f"Error: {result['error']}")
logger.error(f"Stand error: {result['error']}")
else:
st.write(f"**Result**: {result['explanation']}")
st.write(
@ -261,12 +307,18 @@ def tools_interface():
st.write(f"**Payout**: {result['payout']}")
st.write(f"**New Balance**: {result['new_balance']}")
st.session_state.blackjack_game_id = None
logger.debug(
"Game completed, reset blackjack_game_id to None"
)
st.rerun()
except requests.RequestException as e:
st.error(f"Error: {str(e)}")
st.error(f"Error during stand: {str(e)}")
logger.error(f"Error during stand: {str(e)}", exc_info=True)
except requests.RequestException as e:
st.error(f"Error fetching game state: {str(e)}")
logger.error(f"Error fetching game state: {str(e)}", exc_info=True)
st.session_state.blackjack_game_id = None # Reset if game not found
logger.debug("Reset blackjack_game_id to None due to fetch error")
def memory_interface():