DheerajAlamuri commited on
Commit
cea6507
·
verified ·
1 Parent(s): 4e99b38

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -72
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 # NEW import
7
-
8
- st.set_page_config(layout="wide")
9
- st.title("♟️ Chess Game Analyzer")
10
-
11
- uploaded_file = st.file_uploader("Upload a PGN file", type=["pgn"])
12
-
13
- if uploaded_file:
14
- analyzer = ChessAnalyzer(uploaded_file)
15
- analyzer.analyze_game()
16
-
17
- scorecards = analyzer.get_scorecards()
18
- moves = analyzer.moves_info
19
- opening = analyzer.opening_name
20
-
21
- st.subheader(f"📖 Opening: {opening}")
22
- col1, col2 = st.columns(2)
23
- col1.metric(f"{scorecards['White']['name']} (White)", f"{scorecards['White']['accuracy']}% Accuracy")
24
- col2.metric(f"{scorecards['Black']['name']} (Black)", f"{scorecards['Black']['accuracy']}% Accuracy")
25
-
26
- st.markdown("---")
27
- st.subheader("🧠 Move-by-Move Analysis")
28
-
29
- selected_index = st.slider("Select Move Number", 1, len(moves), 1)
30
- selected_move = moves[selected_index - 1]
31
-
32
- # Evaluation bar and board side-by-side
33
- from PIL import Image as PILImage
34
-
35
- img_col, bar_col = st.columns([5, 1], gap="small")
36
-
37
- with img_col:
38
- st.image(
39
- PILImage.open(io.BytesIO(selected_move["image"])),
40
- caption=f"{selected_move['color']} plays {selected_move['move']}"
41
- )
42
-
43
- with bar_col:
44
- bar_image = get_eval_bar_image(selected_move["eval_before"])
45
- st.image(
46
- bar_image,
47
- use_container_width=False,
48
- width=40
49
- )
50
-
51
-
52
-
53
- st.write(f"**Move Type:** `{selected_move['type']}`")
54
- st.write(f"**Centipawn Loss:** `{int(selected_move['cp_loss'])}`")
55
- st.write(f"**Evaluation Before:** `{selected_move['eval_before']}`")
56
- st.write(f"**Evaluation After:** `{selected_move['eval_after']}`")
57
-
58
- st.markdown("---")
59
- st.subheader("📋 All Moves Table")
60
-
61
- table_data = [
62
- {
63
- "Move #": move["move_number"],
64
- "Color": move["color"],
65
- "Move": move["move"],
66
- "Type": move["type"],
67
- "CP Loss": int(move["cp_loss"])
68
- }
69
- for move in moves
70
- ]
71
- df = pd.DataFrame(table_data)
72
- st.dataframe(df, use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
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)