Spaces:
Sleeping

PERDYPTO commited on
Commit
f510773
·
verified ·
1 Parent(s): 28afd47

Update hf_space.py

Browse files
Files changed (1) hide show
  1. hf_space.py +20 -14
hf_space.py CHANGED
@@ -144,28 +144,31 @@ async def background_uploader():
144
  else:
145
  print("[Checkpoint] No changes detected. Skipping.")
146
 
147
- def run_duckdb_ingest_sync():
148
- """Runs the DuckDB remote join and inserts data into SQLite in chunks."""
149
  global pending_ids, ram_lock
150
  if duckdb is None or HfFileSystem is None:
151
  print("[Ingest] DuckDB or HfFileSystem not installed. Ingestion disabled.")
152
  return
153
 
154
- print("[Ingest] Starting DuckDB remote join for Python metadata...")
155
  try:
156
  fs = HfFileSystem()
157
  con = duckdb.connect()
158
  con.register_filesystem(fs)
159
 
160
  query = """
161
- WITH unnested_metadata AS (
162
- SELECT repo_id, repo_path, num_files, UNNEST(files) AS file_info
163
- FROM 'hf://buckets/HuggingFaceCode/stack-v3-full/metadata/*.parquet'
164
  )
165
- SELECT DISTINCT repo_id, repo_path, num_files
166
- FROM unnested_metadata AS m
167
- INNER JOIN 'hf://buckets/HuggingFaceCode/stack-v3-full/contents/language=Python/*.parquet' AS c
168
- ON m.file_info.content_id = c.content_id
 
 
 
169
  """
170
 
171
  cur = con.execute(query)
@@ -199,13 +202,14 @@ def run_duckdb_ingest_sync():
199
  )
200
  db_conn.commit()
201
 
202
- # Add to RAM queue
203
  new_jobs = [{"repo_id": r[0], "repo_path": r[1]} for r in new_rows]
 
204
  async def add_to_ram(jobs):
205
  async with ram_lock:
206
  pending_ids.extend(jobs)
207
- # Schedule the RAM update safely from this sync thread
208
- asyncio.run_coroutine_threadsafe(add_to_ram(new_jobs), asyncio.get_event_loop())
209
 
210
  print("[Ingest] DuckDB remote join completed successfully.")
211
 
@@ -235,7 +239,9 @@ async def lifespan(app: FastAPI):
235
  last_db_changes = db_conn.total_changes
236
 
237
  # Start DuckDB ingestion in a separate thread so it doesn't block FastAPI
238
- asyncio.create_task(asyncio.to_thread(run_duckdb_ingest_sync))
 
 
239
  # Start background uploader
240
  asyncio.create_task(background_uploader())
241
  yield
 
144
  else:
145
  print("[Checkpoint] No changes detected. Skipping.")
146
 
147
+ def run_duckdb_ingest_sync(loop: asyncio.AbstractEventLoop):
148
+ """Runs the DuckDB remote join starting from the Python content folder."""
149
  global pending_ids, ram_lock
150
  if duckdb is None or HfFileSystem is None:
151
  print("[Ingest] DuckDB or HfFileSystem not installed. Ingestion disabled.")
152
  return
153
 
154
+ print("[Ingest] Starting DuckDB remote join (Python contents -> Metadata)...")
155
  try:
156
  fs = HfFileSystem()
157
  con = duckdb.connect()
158
  con.register_filesystem(fs)
159
 
160
  query = """
161
+ WITH python_repo_ids AS (
162
+ SELECT DISTINCT UNNEST(repo_ids) AS repo_id
163
+ FROM 'hf://buckets/HuggingFaceCode/stack-v3-full/contents/language=Python/*.parquet'
164
  )
165
+ SELECT
166
+ m.repo_id,
167
+ m.repo_path,
168
+ m.num_files
169
+ FROM python_repo_ids AS p
170
+ INNER JOIN 'hf://buckets/HuggingFaceCode/stack-v3-full/metadata/*.parquet' AS m
171
+ ON p.repo_id = m.repo_id
172
  """
173
 
174
  cur = con.execute(query)
 
202
  )
203
  db_conn.commit()
204
 
205
+ # Add to RAM queue safely from this sync thread
206
  new_jobs = [{"repo_id": r[0], "repo_path": r[1]} for r in new_rows]
207
+
208
  async def add_to_ram(jobs):
209
  async with ram_lock:
210
  pending_ids.extend(jobs)
211
+
212
+ asyncio.run_coroutine_threadsafe(add_to_ram(new_jobs), loop)
213
 
214
  print("[Ingest] DuckDB remote join completed successfully.")
215
 
 
239
  last_db_changes = db_conn.total_changes
240
 
241
  # Start DuckDB ingestion in a separate thread so it doesn't block FastAPI
242
+ loop = asyncio.get_running_loop()
243
+ asyncio.create_task(asyncio.to_thread(run_duckdb_ingest_sync, loop))
244
+
245
  # Start background uploader
246
  asyncio.create_task(background_uploader())
247
  yield