This python code (streamlit) while working great is not generating sound some times when the bomb blasts, pls look into it:

import streamlit as st
import numpy as np
import base64

# Function to generate the board
def generate_board(rows, cols, mines):
    board = np.zeros((rows, cols), dtype=int)
    # Place mines
    mine_positions = set()
    while len(mine_positions) < mines:
        position = (np.random.randint(0, rows), np.random.randint(0, cols))
        mine_positions.add(position)
    for (r, c) in mine_positions:
        board[r, c] = -1  # -1 represents a mine
    # Calculate numbers for non-mine cells
    for r in range(rows):
        for c in range(cols):
            if board[r, c] == -1:
                continue
            # Count neighboring mines
            count = 0
            for i in range(max(0, r - 1), min(rows, r + 2)):
                for j in range(max(0, c - 1), min(cols, c + 2)):
                    if board[i, j] == -1:
                        count += 1
            board[r, c] = count
    return board

# Initialize game state
if 'board' not in st.session_state:
    st.session_state.rows = 10
    st.session_state.cols = 10
    st.session_state.mines = 15
    st.session_state.flags = set()
    st.session_state.opened = set()
    st.session_state.game_over = False
    st.session_state.victory = False
    st.session_state.play_sound = False
    st.session_state.board = generate_board(
        st.session_state.rows, st.session_state.cols, st.session_state.mines
    )

# Function to reset the game
def reset_game():
    st.session_state.flags = set()
    st.session_state.opened = set()
    st.session_state.game_over = False
    st.session_state.victory = False
    st.session_state.play_sound = False
    st.session_state.board = generate_board(
        st.session_state.rows, st.session_state.cols, st.session_state.mines
    )

# Function to open a cell
def open_cell(r, c):
    if (r, c) in st.session_state.flags or st.session_state.game_over:
        return
    if (r, c) in st.session_state.opened:
        return
    st.session_state.opened.add((r, c))
    if st.session_state.board[r, c] == -1:
        # Hit a mine
        st.session_state.game_over = True
        st.session_state.play_sound = True
    elif st.session_state.board[r, c] == 0:
        # Recursively open neighboring cells
        for i in range(max(0, r - 1), min(st.session_state.rows, r + 2)):
            for j in range(max(0, c - 1), min(st.session_state.cols, c + 2)):
                if (i, j) != (r, c):
                    open_cell(i, j)
    # Check for victory condition
    if len(st.session_state.opened) == st.session_state.rows * st.session_state.cols - st.session_state.mines:
        st.session_state.victory = True
        st.session_state.game_over = True

# Function to flag or unflag a cell
def flag_cell(r, c):
    if (r, c) in st.session_state.opened or st.session_state.game_over:
        return
    if (r, c) in st.session_state.flags:
        st.session_state.flags.remove((r, c))
    else:
        st.session_state.flags.add((r, c))

# Game title
st.title("Minesweeper")

# Button to reset the game
if st.button("Reset Game"):
    reset_game()

# Display mines remaining
st.write(f"Mines remaining: {st.session_state.mines - len(st.session_state.flags)}")

# Toggle between 'Open Cell' and 'Flag Cell' mode
mode = st.radio("Select mode:", ('Open Cell', 'Flag Cell'), horizontal=True)

# Display the board
for r in range(st.session_state.rows):
    cols = st.columns(st.session_state.cols)
    for c in range(st.session_state.cols):
        label = ""
        if (r, c) in st.session_state.opened:
            if st.session_state.board[r, c] == -1:
                label = "💣"
            elif st.session_state.board[r, c] == 0:
                label = " "
            else:
                label = str(st.session_state.board[r, c])
            cols[c].button(
                label,
                key=f"cell_{r}_{c}",
                disabled=True,
            )
        elif (r, c) in st.session_state.flags:
            cols[c].button(
                "🚩",
                key=f"cell_{r}_{c}",
                on_click=flag_cell,
                args=(r, c),
            )
        else:
            if mode == 'Open Cell':
                cols[c].button(
                    label,
                    key=f"cell_{r}_{c}",
                    on_click=open_cell,
                    args=(r, c),
                )
            else:
                cols[c].button(
                    label,
                    key=f"cell_{r}_{c}",
                    on_click=flag_cell,
                    args=(r, c),
                )

# Function to play sound using JavaScript
def js_play_sound():
    js_code = """
    <script>
    var context = new AudioContext();
    var o = context.createOscillator();
    var g = context.createGain();
    o.type = "sawtooth";
    o.connect(g);
    g.connect(context.destination);
    o.start();
    g.gain.exponentialRampToValueAtTime(
        0.00001, context.currentTime + 1
    );
    </script>
    """
    st.components.v1.html(js_code)

# Game over messages and sound
if st.session_state.game_over:
    if st.session_state.victory:
        st.success("Congratulations! You've cleared all the mines!")
    else:
        st.error("Game Over! You hit a mine.")
        if st.session_state.play_sound:
            js_play_sound()
            st.session_state.play_sound = False  # Reset play_sound flag