Spaces:
Sleeping
Sleeping
PERDYPTO commited on
Update hf_space.py
Browse files- 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
|
| 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
|
| 155 |
try:
|
| 156 |
fs = HfFileSystem()
|
| 157 |
con = duckdb.connect()
|
| 158 |
con.register_filesystem(fs)
|
| 159 |
|
| 160 |
query = """
|
| 161 |
-
WITH
|
| 162 |
-
SELECT
|
| 163 |
-
FROM 'hf://buckets/HuggingFaceCode/stack-v3-full/
|
| 164 |
)
|
| 165 |
-
SELECT
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 208 |
-
asyncio.run_coroutine_threadsafe(add_to_ram(new_jobs),
|
| 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.
|
|
|
|
|
|
|
| 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
|