MrSimple07 commited on
Commit
19ccaae
Β·
1 Parent(s): 2c0bb01

extract user rating + lichess puzzle rating

Browse files
Files changed (3) hide show
  1. app.py +1 -1
  2. core/chess_api.py +1 -1
  3. core/core.py +32 -5
app.py CHANGED
@@ -40,7 +40,7 @@ with gr.Blocks(title="Chess Study Plan Pro", theme=gr.themes.Soft()) as demo:
40
  )
41
  username_pgn = gr.Textbox(
42
  label="Foydalanuvchi nomi (PGN uchun)",
43
- placeholder="PGN dagi o'yinchi nomi (ixtiyoriy)",
44
  )
45
 
46
  analyze_btn = gr.Button("πŸ” Tahlil qilish", variant="primary", size="lg")
 
40
  )
41
  username_pgn = gr.Textbox(
42
  label="Foydalanuvchi nomi (PGN uchun)",
43
+ placeholder="PGN dagi o'yinchi nomi",
44
  )
45
 
46
  analyze_btn = gr.Button("πŸ” Tahlil qilish", variant="primary", size="lg")
core/chess_api.py CHANGED
@@ -58,7 +58,7 @@ def fetch_lichess_puzzles(error_types, user_rating=1500, count=5):
58
 
59
  # Map Uzbek error categories to Lichess puzzle themes
60
  theme_mapping = {
61
- "Qo'pol xatolar": ["mate", "mateIn1", "mateIn2", "hangingPiece"],
62
  "Kichik xatolar": ["advantage", "crushing", "attackingF2F7"],
63
  "Himoyasiz qoldirish": ["hangingPiece", "pin", "skewer", "discoveredAttack"],
64
  "Debyut xatolari": ["opening", "middlegame"],
 
58
 
59
  # Map Uzbek error categories to Lichess puzzle themes
60
  theme_mapping = {
61
+ "Qo'pol xatolar": ["crushing", "hangingPiece"],
62
  "Kichik xatolar": ["advantage", "crushing", "attackingF2F7"],
63
  "Himoyasiz qoldirish": ["hangingPiece", "pin", "skewer", "discoveredAttack"],
64
  "Debyut xatolari": ["opening", "middlegame"],
core/core.py CHANGED
@@ -8,11 +8,34 @@ from core.ai_integration import get_comprehensive_analysis
8
  from core.openings import detect_opening, load_opening_database
9
  from core.chess_api import get_user_games_from_chess_com, fetch_lichess_puzzles
10
 
11
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  def analyze_games(username_chesscom, pgn_file, username_pgn):
14
  actual_username = None
15
  pgn_content = None
 
16
 
17
  if username_chesscom:
18
  pgn_content, error = get_user_games_from_chess_com(username_chesscom)
@@ -45,6 +68,9 @@ def analyze_games(username_chesscom, pgn_file, username_pgn):
45
  if not games:
46
  return "❌ O'yinlar topilmadi yoki tahlil qilinmadi", "", "", "", None, None, None, None, None
47
 
 
 
 
48
  all_analyses = []
49
  opening_stats = defaultdict(lambda: {'wins': 0, 'losses': 0, 'draws': 0, 'total': 0})
50
  color_stats = {
@@ -83,6 +109,7 @@ def analyze_games(username_chesscom, pgn_file, username_pgn):
83
  all_mistakes.extend(analysis.get('mistakes', []))
84
 
85
  stats_report = f"## πŸ“Š {len(games)} ta o'yin tahlili\n\n"
 
86
  stats_report += f"**Jami xatolar:** {len(all_mistakes)} ta\n\n"
87
 
88
  stats_report += "### 🎯 Eng zaif 3 tomoningiz:\n\n"
@@ -130,14 +157,15 @@ def analyze_games(username_chesscom, pgn_file, username_pgn):
130
  ai_report = f"## πŸ€– AI Murabbiy: To'liq Tahlil va O'quv Rejasi\n\n{ai_analysis}"
131
 
132
  weakness_themes = [w['category'] for w in weaknesses[:3]]
133
- puzzles = fetch_lichess_puzzles(weakness_themes, count=5)
134
 
135
  puzzle_text = "## 🧩 Sizning shaxsiy masalalaringiz\n\n"
136
- puzzle_text += "Masalalarni yechish uchun quyidagi havoladan foydalaning:\n\n"
137
  for i, puzzle in enumerate(puzzles, 1):
138
  theme = puzzle.get('theme', 'Tactics')
 
139
  url = puzzle.get('url', 'https://lichess.org/training')
140
- puzzle_text += f"**Puzzle {i}: {theme}**\n"
141
  puzzle_text += f"- [Lichess Training]({url})\n\n"
142
 
143
  return (
@@ -152,7 +180,6 @@ def analyze_games(username_chesscom, pgn_file, username_pgn):
152
  None
153
  )
154
 
155
-
156
  def parse_pgn_content(pgn_content):
157
  games = []
158
  if isinstance(pgn_content, list):
 
8
  from core.openings import detect_opening, load_opening_database
9
  from core.chess_api import get_user_games_from_chess_com, fetch_lichess_puzzles
10
 
11
+ def extract_user_rating(games, username):
12
+ ratings = []
13
+ username_lower = username.strip().lower()
14
+
15
+ for game in games:
16
+ white_player = game.headers.get("White", "").strip().lower()
17
+ black_player = game.headers.get("Black", "").strip().lower()
18
+
19
+ if username_lower == white_player:
20
+ white_elo = game.headers.get("WhiteElo", "")
21
+ if white_elo and white_elo.isdigit():
22
+ ratings.append(int(white_elo))
23
+
24
+ elif username_lower == black_player:
25
+ black_elo = game.headers.get("BlackElo", "")
26
+ if black_elo and black_elo.isdigit():
27
+ ratings.append(int(black_elo))
28
+
29
+ if ratings:
30
+ avg_rating = sum(ratings) // len(ratings)
31
+ return avg_rating
32
+
33
+ return 1500
34
 
35
  def analyze_games(username_chesscom, pgn_file, username_pgn):
36
  actual_username = None
37
  pgn_content = None
38
+ user_rating = 1500 # Default rating
39
 
40
  if username_chesscom:
41
  pgn_content, error = get_user_games_from_chess_com(username_chesscom)
 
68
  if not games:
69
  return "❌ O'yinlar topilmadi yoki tahlil qilinmadi", "", "", "", None, None, None, None, None
70
 
71
+ # Extract user rating from games
72
+ user_rating = extract_user_rating(games, actual_username)
73
+
74
  all_analyses = []
75
  opening_stats = defaultdict(lambda: {'wins': 0, 'losses': 0, 'draws': 0, 'total': 0})
76
  color_stats = {
 
109
  all_mistakes.extend(analysis.get('mistakes', []))
110
 
111
  stats_report = f"## πŸ“Š {len(games)} ta o'yin tahlili\n\n"
112
+ stats_report += f"**Sizning reytingingiz:** {user_rating}\n"
113
  stats_report += f"**Jami xatolar:** {len(all_mistakes)} ta\n\n"
114
 
115
  stats_report += "### 🎯 Eng zaif 3 tomoningiz:\n\n"
 
157
  ai_report = f"## πŸ€– AI Murabbiy: To'liq Tahlil va O'quv Rejasi\n\n{ai_analysis}"
158
 
159
  weakness_themes = [w['category'] for w in weaknesses[:3]]
160
+ puzzles = fetch_lichess_puzzles(weakness_themes, user_rating=user_rating, count=5)
161
 
162
  puzzle_text = "## 🧩 Sizning shaxsiy masalalaringiz\n\n"
163
+ puzzle_text += f"Sizning reytingingiz: **{user_rating}** - Masalalar shu darajaga moslashtirilgan\n\n"
164
  for i, puzzle in enumerate(puzzles, 1):
165
  theme = puzzle.get('theme', 'Tactics')
166
+ rating = puzzle.get('rating', user_rating)
167
  url = puzzle.get('url', 'https://lichess.org/training')
168
+ puzzle_text += f"**Puzzle {i}: {theme}** (Rating: {rating})\n"
169
  puzzle_text += f"- [Lichess Training]({url})\n\n"
170
 
171
  return (
 
180
  None
181
  )
182
 
 
183
  def parse_pgn_content(pgn_content):
184
  games = []
185
  if isinstance(pgn_content, list):