Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,83 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from analyzer import ChessAnalyzer
|
3 |
-
from PIL import Image
|
4 |
-
import io
|
5 |
-
import pandas as pd
|
6 |
-
from utils import get_eval_bar_image
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
if
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from analyzer import ChessAnalyzer
|
3 |
+
from PIL import Image
|
4 |
+
import io
|
5 |
+
import pandas as pd
|
6 |
+
from utils import get_eval_bar_image
|
7 |
+
import platform
|
8 |
+
import os
|
9 |
+
import urllib.request
|
10 |
+
|
11 |
+
# 🔁 Ensure stockfish is ready
|
12 |
+
if platform.system() != "Windows":
|
13 |
+
if not os.path.exists("stockfish"):
|
14 |
+
print("⏬ Downloading Stockfish binary from Hugging Face...")
|
15 |
+
url = "https://huggingface.co/datasets/DheerajAlamuri/stockfish-binary/resolve/main/stockfish"
|
16 |
+
urllib.request.urlretrieve(url, "stockfish")
|
17 |
+
os.chmod("stockfish", 0o755)
|
18 |
+
|
19 |
+
st.set_page_config(layout="wide")
|
20 |
+
st.title("♟️ Chess Game Analyzer")
|
21 |
+
|
22 |
+
uploaded_file = st.file_uploader("Upload a PGN file", type=["pgn"])
|
23 |
+
|
24 |
+
if uploaded_file:
|
25 |
+
analyzer = ChessAnalyzer(uploaded_file)
|
26 |
+
analyzer.analyze_game()
|
27 |
+
|
28 |
+
scorecards = analyzer.get_scorecards()
|
29 |
+
moves = analyzer.moves_info
|
30 |
+
opening = analyzer.opening_name
|
31 |
+
|
32 |
+
st.subheader(f"📖 Opening: {opening}")
|
33 |
+
col1, col2 = st.columns(2)
|
34 |
+
col1.metric(f"{scorecards['White']['name']} (White)", f"{scorecards['White']['accuracy']}% Accuracy")
|
35 |
+
col2.metric(f"{scorecards['Black']['name']} (Black)", f"{scorecards['Black']['accuracy']}% Accuracy")
|
36 |
+
|
37 |
+
st.markdown("---")
|
38 |
+
st.subheader("🧠 Move-by-Move Analysis")
|
39 |
+
|
40 |
+
selected_index = st.slider("Select Move Number", 1, len(moves), 1)
|
41 |
+
selected_move = moves[selected_index - 1]
|
42 |
+
|
43 |
+
# Evaluation bar and board side-by-side
|
44 |
+
from PIL import Image as PILImage
|
45 |
+
|
46 |
+
img_col, bar_col = st.columns([5, 1], gap="small")
|
47 |
+
|
48 |
+
with img_col:
|
49 |
+
st.image(
|
50 |
+
PILImage.open(io.BytesIO(selected_move["image"])),
|
51 |
+
caption=f"{selected_move['color']} plays {selected_move['move']}"
|
52 |
+
)
|
53 |
+
|
54 |
+
with bar_col:
|
55 |
+
bar_image = get_eval_bar_image(selected_move["eval_before"])
|
56 |
+
st.image(
|
57 |
+
bar_image,
|
58 |
+
use_container_width=False,
|
59 |
+
width=40
|
60 |
+
)
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
st.write(f"**Move Type:** `{selected_move['type']}`")
|
65 |
+
st.write(f"**Centipawn Loss:** `{int(selected_move['cp_loss'])}`")
|
66 |
+
st.write(f"**Evaluation Before:** `{selected_move['eval_before']}`")
|
67 |
+
st.write(f"**Evaluation After:** `{selected_move['eval_after']}`")
|
68 |
+
|
69 |
+
st.markdown("---")
|
70 |
+
st.subheader("📋 All Moves Table")
|
71 |
+
|
72 |
+
table_data = [
|
73 |
+
{
|
74 |
+
"Move #": move["move_number"],
|
75 |
+
"Color": move["color"],
|
76 |
+
"Move": move["move"],
|
77 |
+
"Type": move["type"],
|
78 |
+
"CP Loss": int(move["cp_loss"])
|
79 |
+
}
|
80 |
+
for move in moves
|
81 |
+
]
|
82 |
+
df = pd.DataFrame(table_data)
|
83 |
+
st.dataframe(df, use_container_width=True)
|