Kartheek Akella commited on
Commit
7d95f28
·
1 Parent(s): bbba87b
app.py CHANGED
@@ -26,8 +26,10 @@ IST = timezone(timedelta(hours=5, minutes=30))
26
  DATASET_DIR = Path("league_data")
27
  DATASET_DIR.mkdir(parents=True, exist_ok=True)
28
 
29
- MATCHES_FILE = DATASET_DIR / f"matches-{uuid4()}.jsonl"
30
- DELETION_LOG_FILE = DATASET_DIR / f"deletions-{uuid4()}.jsonl"
 
 
31
 
32
  # Initialize HfApi for immediate uploads
33
  api = HfApi()
@@ -62,25 +64,6 @@ def ensure_repo_exists():
62
  logger.error(f"✗ Error creating repository: {e}")
63
  logger.error("Please create the repository manually or check your HF_TOKEN permissions")
64
 
65
-
66
- def initialize_matches_from_league_scores():
67
- """Initialize matches from league_scores.py data."""
68
- initial_matches = [
69
- ["Seelam", "Akhil", 5, 3],
70
- ["Seelam", "Kartheek", 4, 4],
71
- ["Shiva", "Akhil", 1, 6],
72
- ["Shiva", "Kartheek", 8, 3],
73
- ["Shiva", "Kartheek", 4, 1],
74
- ["Seelam", "Kartheek", 5, 1],
75
- ["Seelam", "Kartheek", 1, 6],
76
- ["Akhil", "Kartheek", 1, 5],
77
- ["Shiva", "Akhil", 3, 1],
78
- ["Shiva", "Akhil", 3, 3],
79
- ["Seelam", "Kartheek", 1, 3],
80
- ]
81
- return initial_matches
82
-
83
-
84
  def load_matches():
85
  """Load matches from HuggingFace dataset repository."""
86
  global matches
@@ -100,46 +83,6 @@ def load_matches():
100
  data_files = [f for f in repo_files if f.startswith(f"{PATH_IN_REPO}/")]
101
  logger.info(f"→ Found {len(data_files)} files in {PATH_IN_REPO}/ directory")
102
 
103
- if not data_files:
104
- # Initialize with data from league_scores.py if no files exist in dataset
105
- logger.warning("⚠ No files found in dataset, initializing with default data...")
106
- initial_matches = initialize_matches_from_league_scores()
107
- logger.info(f"→ Creating {len(initial_matches)} initial matches")
108
-
109
- # Save initial matches
110
- for match in initial_matches:
111
- match_id = str(uuid4())
112
- with file_lock:
113
- with MATCHES_FILE.open("a") as f:
114
- record = {
115
- "id": match_id,
116
- "home": match[0],
117
- "away": match[1],
118
- "home_goals": match[2],
119
- "away_goals": match[3],
120
- "datetime": datetime.now(IST).isoformat()
121
- }
122
- json.dump(record, f)
123
- f.write("\n")
124
- matches.append([match_id, match[0], match[1], match[2], match[3], record["datetime"]])
125
-
126
- # Upload initial matches file immediately
127
- try:
128
- logger.info(f"→ Uploading initial matches to dataset: {MATCHES_FILE.name}")
129
- api.upload_file(
130
- path_or_fileobj=str(MATCHES_FILE),
131
- path_in_repo=f"{PATH_IN_REPO}/{MATCHES_FILE.name}",
132
- repo_id=REPO_ID,
133
- repo_type=REPO_TYPE,
134
- )
135
- logger.info(f"✓ Initial matches uploaded to dataset successfully")
136
- except Exception as e:
137
- logger.error(f"✗ Error uploading initial matches: {e}")
138
-
139
- logger.info(f"✓ Initialized with {len(matches)} matches")
140
- logger.info("=" * 60)
141
- return matches
142
-
143
  # Load deleted match IDs from dataset
144
  deleted_ids = set()
145
  deletion_files = [f for f in data_files if "deletions-" in f and f.endswith(".jsonl")]
@@ -216,21 +159,6 @@ def load_matches():
216
 
217
  except Exception as e:
218
  logger.error(f"✗ Error accessing dataset repository: {e}")
219
- logger.warning("⚠ Falling back to default data...")
220
- # Fallback to initial matches if dataset access fails
221
- initial_matches = initialize_matches_from_league_scores()
222
- for match in initial_matches:
223
- match_id = str(uuid4())
224
- matches.append([
225
- match_id,
226
- match[0],
227
- match[1],
228
- match[2],
229
- match[3],
230
- datetime.now(IST).isoformat()
231
- ])
232
- logger.info(f"✓ Initialized with {len(matches)} default matches")
233
- logger.info("=" * 60)
234
 
235
  return matches
236
 
@@ -655,6 +583,15 @@ def build_interface():
655
  # Load initial data
656
  load_matches()
657
 
 
 
 
 
 
 
 
 
 
658
  with gr.Blocks(title="League Table Manager") as demo:
659
  gr.Markdown("# League Table Manager")
660
 
@@ -814,6 +751,13 @@ def build_interface():
814
  outputs=[h2h_stats]
815
  )
816
 
 
 
 
 
 
 
 
817
  return demo
818
 
819
 
 
26
  DATASET_DIR = Path("league_data")
27
  DATASET_DIR.mkdir(parents=True, exist_ok=True)
28
 
29
+ # Session-specific file names (generated once per app startup)
30
+ SESSION_ID = str(uuid4())
31
+ MATCHES_FILE = DATASET_DIR / f"matches-{SESSION_ID}.jsonl"
32
+ DELETION_LOG_FILE = DATASET_DIR / f"deletions-{SESSION_ID}.jsonl"
33
 
34
  # Initialize HfApi for immediate uploads
35
  api = HfApi()
 
64
  logger.error(f"✗ Error creating repository: {e}")
65
  logger.error("Please create the repository manually or check your HF_TOKEN permissions")
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  def load_matches():
68
  """Load matches from HuggingFace dataset repository."""
69
  global matches
 
83
  data_files = [f for f in repo_files if f.startswith(f"{PATH_IN_REPO}/")]
84
  logger.info(f"→ Found {len(data_files)} files in {PATH_IN_REPO}/ directory")
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  # Load deleted match IDs from dataset
87
  deleted_ids = set()
88
  deletion_files = [f for f in data_files if "deletions-" in f and f.endswith(".jsonl")]
 
159
 
160
  except Exception as e:
161
  logger.error(f"✗ Error accessing dataset repository: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
 
163
  return matches
164
 
 
583
  # Load initial data
584
  load_matches()
585
 
586
+ def refresh_data():
587
+ """Reload matches from HuggingFace and return updated tables."""
588
+ load_matches()
589
+ return (
590
+ calculate_table(matches),
591
+ get_matches_dataframe(matches),
592
+ calculate_head_to_head(TEAMS[0], TEAMS[1], matches)
593
+ )
594
+
595
  with gr.Blocks(title="League Table Manager") as demo:
596
  gr.Markdown("# League Table Manager")
597
 
 
751
  outputs=[h2h_stats]
752
  )
753
 
754
+ # Load fresh data when the page loads/refreshes
755
+ demo.load(
756
+ fn=refresh_data,
757
+ inputs=[],
758
+ outputs=[league_table, matches_table, h2h_stats]
759
+ )
760
+
761
  return demo
762
 
763
 
league_data/matches-c1ee47d3-9631-40bb-8f38-995aa1cd62cb.jsonl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ {"id": "4b9f8e20-c549-4784-95dc-af9387eb1d9a", "home": "Seelam", "away": "Kartheek", "home_goals": 3, "away_goals": 7, "datetime": "2025-12-31T18:20:21.023628+05:30"}
2
+ {"id": "291a9360-e76d-4acc-b4de-1b9c0b9fc852", "home": "Seelam", "away": "Kartheek", "home_goals": 7, "away_goals": 4, "datetime": "2025-12-31T18:20:33.757475+05:30"}
3
+ {"id": "f62a1714-414e-44dd-a275-2e9f99f64eef", "home": "Seelam", "away": "Kartheek", "home_goals": 3, "away_goals": 3, "datetime": "2025-12-31T18:20:47.553864+05:30"}