Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.9.3
4.9.4
2 changes: 1 addition & 1 deletion gas/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = "4.9.3"
__version__ = "4.9.4"

version_split = __version__.split(".")
__spec_version__ = (
Expand Down
14 changes: 14 additions & 0 deletions gas/cache/content_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ def write_miner_media(
return None

resolution, file_size = extract_media_info(save_path, modality)

# Audio presence feeds resolution/audio-tiered reward pricing.
has_audio = None
if modality == Modality.VIDEO:
try:
from gas.cache.util.video import get_video_metadata

has_audio = bool(get_video_metadata(save_path).get("has_audio"))
except Exception as e:
bt.logging.warning(f"Could not detect audio track in {save_path}: {e}")

media_id = self.media.add_media_entry(
prompt_id=prompt_id,
file_path=save_path,
Expand All @@ -234,6 +245,7 @@ def write_miner_media(
c2pa_verified=c2pa_verified,
c2pa_issuer=c2pa_issuer,
task_id=task_id,
has_audio=has_audio,
)
self.challenges.update_outcome(
task_id=task_id,
Expand Down Expand Up @@ -611,6 +623,7 @@ def record_challenge_outcome(
failure_reason: Optional[str] = None,
media_id: Optional[str] = None,
created_at: Optional[float] = None,
requested_resolution: Optional[str] = None,
) -> bool:
return self.challenges.record_outcome(
task_id=task_id,
Expand All @@ -622,6 +635,7 @@ def record_challenge_outcome(
failure_reason=failure_reason,
media_id=media_id,
created_at=created_at,
requested_resolution=requested_resolution,
)

def update_challenge_outcome(
Expand Down
57 changes: 50 additions & 7 deletions gas/cache/db/challenge_store.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
"""ChallengeStore — CRUD and reward stats for the generator_challenge_outcomes table."""

import json
import sqlite3
import time
from typing import List, Dict, Optional, Any
from typing import List, Dict, Optional, Any, Tuple

import bittensor as bt

from gas.cache.types import ChallengeOutcome
from gas.cache.db.connection import ConnectionManager


def _parse_resolution(raw: Optional[str]) -> Optional[Tuple[int, int]]:
"""Parse the media table's JSON-encoded (width, height) resolution."""
if not raw:
return None
try:
data = json.loads(raw)
return (int(data[0]), int(data[1]))
except (ValueError, TypeError, IndexError, json.JSONDecodeError):
return None


class ChallengeStore:
"""Data access for the ``generator_challenge_outcomes`` table."""

Expand All @@ -31,6 +43,7 @@ def record_outcome(
failure_reason: Optional[str] = None,
media_id: Optional[str] = None,
created_at: Optional[float] = None,
requested_resolution: Optional[str] = None,
) -> bool:
"""Insert or update a generation challenge outcome."""
try:
Expand All @@ -41,9 +54,9 @@ def record_outcome(
"""
INSERT INTO generator_challenge_outcomes (
task_id, uid, hotkey, prompt_id, modality, status,
failure_reason, media_id, created_at, updated_at
failure_reason, media_id, requested_resolution, created_at, updated_at
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(task_id) DO UPDATE SET
uid = excluded.uid,
hotkey = excluded.hotkey,
Expand All @@ -52,9 +65,10 @@ def record_outcome(
status = excluded.status,
failure_reason = excluded.failure_reason,
media_id = COALESCE(excluded.media_id, generator_challenge_outcomes.media_id),
requested_resolution = COALESCE(excluded.requested_resolution, generator_challenge_outcomes.requested_resolution),
updated_at = excluded.updated_at
""",
(task_id, uid, hotkey, prompt_id, modality, status, failure_reason, media_id, created_at, now),
(task_id, uid, hotkey, prompt_id, modality, status, failure_reason, media_id, requested_resolution, created_at, now),
)
conn.commit()
return True
Expand Down Expand Up @@ -144,9 +158,11 @@ def get_outcomes_last_n_hours(
conn.row_factory = sqlite3.Row
cursor = conn.execute(
"""
SELECT * FROM generator_challenge_outcomes
WHERE status IN ('verified', 'failed') AND updated_at >= ?
ORDER BY updated_at DESC LIMIT ?
SELECT o.*, m.resolution AS media_resolution, m.has_audio AS media_has_audio
FROM generator_challenge_outcomes o
LEFT JOIN media m ON o.media_id = m.id
WHERE o.status IN ('verified', 'failed') AND o.updated_at >= ?
ORDER BY o.updated_at DESC LIMIT ?
""",
(cutoff, int(limit)),
)
Expand All @@ -161,6 +177,13 @@ def get_outcomes_last_n_hours(
failure_reason=row["failure_reason"],
media_id=row["media_id"],
model_name=row["model_name"] if "model_name" in row.keys() else None,
requested_resolution=(
row["requested_resolution"] if "requested_resolution" in row.keys() else None
),
observed_resolution=_parse_resolution(row["media_resolution"]),
has_audio=(
bool(row["media_has_audio"]) if row["media_has_audio"] is not None else None
),
created_at=row["created_at"],
updated_at=row["updated_at"],
)
Expand All @@ -186,6 +209,7 @@ def get_outcome_stats_last_n_hours(
"image_verified": 0, "video_verified": 0,
"image_failed": 0, "video_failed": 0,
"image_model_names": [], "video_model_names": [],
"image_generations": [], "video_generations": [],
"last_timestamp": outcome.updated_at,
}
modality = (outcome.modality or "").lower()
Expand All @@ -195,10 +219,27 @@ def get_outcome_stats_last_n_hours(
miner_stats[hotkey]["image_verified"] += 1
if outcome.model_name:
miner_stats[hotkey]["image_model_names"].append(outcome.model_name)
miner_stats[hotkey]["image_generations"].append({
"model_name": outcome.model_name,
"requested_resolution": outcome.requested_resolution,
"observed_resolution": (
list(outcome.observed_resolution)
if outcome.observed_resolution else None
),
})
elif modality == "video":
miner_stats[hotkey]["video_verified"] += 1
if outcome.model_name:
miner_stats[hotkey]["video_model_names"].append(outcome.model_name)
miner_stats[hotkey]["video_generations"].append({
"model_name": outcome.model_name,
"requested_resolution": outcome.requested_resolution,
"observed_resolution": (
list(outcome.observed_resolution)
if outcome.observed_resolution else None
),
"has_audio": outcome.has_audio,
})
if outcome.media_id:
miner_stats[hotkey]["verified_media_ids"].append(outcome.media_id)
elif outcome.status == "failed":
Expand Down Expand Up @@ -229,10 +270,12 @@ def get_outcome_stats_last_n_hours(
"image_failed": img_f,
"image_pass_rate": (img_v / img_t) if img_t > 0 else 0.0,
"image_model_names": stats.get("image_model_names", []),
"image_generations": stats.get("image_generations", []),
"video_verified": vid_v,
"video_failed": vid_f,
"video_pass_rate": (vid_v / vid_t) if vid_t > 0 else 0.0,
"video_model_names": stats.get("video_model_names", []),
"video_generations": stats.get("video_generations", []),
"media_ids": stats["verified_media_ids"],
"last_timestamp": stats["last_timestamp"],
}
Expand Down
7 changes: 5 additions & 2 deletions gas/cache/db/media_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def _row_to_media_entry(row) -> MediaEntry:
perceptual_hash=row["perceptual_hash"] if "perceptual_hash" in row.keys() else None,
c2pa_verified=bool(row["c2pa_verified"]) if "c2pa_verified" in row.keys() and row["c2pa_verified"] is not None else False,
c2pa_issuer=row["c2pa_issuer"] if "c2pa_issuer" in row.keys() else None,
has_audio=bool(row["has_audio"]) if "has_audio" in row.keys() and row["has_audio"] is not None else None,
)

# ------------------------------------------------------------------
Expand Down Expand Up @@ -125,6 +126,7 @@ def add_media_entry(
c2pa_verified: Optional[bool] = None,
c2pa_issuer: Optional[str] = None,
task_id: Optional[str] = None,
has_audio: Optional[bool] = None,
) -> str:
"""Add a media entry to the database. Returns the media id."""
media_id = str(uuid.uuid4())
Expand All @@ -139,9 +141,9 @@ def add_media_entry(
model_name, generation_args, uid, hotkey,
verified, failed_verification, rewarded,
created_at, mask_path, timestamp, resolution, file_size, format,
perceptual_hash, c2pa_verified, c2pa_issuer, task_id
perceptual_hash, c2pa_verified, c2pa_issuer, task_id, has_audio
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
media_id, prompt_id, file_path, modality.value, media_type.value, source_type.value,
Expand All @@ -155,6 +157,7 @@ def add_media_entry(
1 if c2pa_verified else 0 if c2pa_verified is not None else None,
c2pa_issuer,
task_id,
1 if has_audio else 0 if has_audio is not None else None,
),
)
conn.commit()
Expand Down
7 changes: 7 additions & 0 deletions gas/cache/db/migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@
"ALTER TABLE media ADD COLUMN clip_embedding BLOB",
],
),
(
"add_resolution_tier_columns",
[
"ALTER TABLE generator_challenge_outcomes ADD COLUMN requested_resolution TEXT",
"ALTER TABLE media ADD COLUMN has_audio BOOLEAN",
],
),
(
"add_prompt_spec_columns",
[
Expand Down
6 changes: 6 additions & 0 deletions gas/cache/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ class MediaEntry:
c2pa_verified: Optional[bool] = False # C2PA validation passed
c2pa_issuer: Optional[str] = None # Issuer name if C2PA verified

# Video audio track presence (None = unknown / not applicable)
has_audio: Optional[bool] = None

# Miner challenge tracking
task_id: Optional[str] = None

Expand Down Expand Up @@ -141,6 +144,9 @@ class ChallengeOutcome:
failure_reason: Optional[str] = None
media_id: Optional[str] = None
model_name: Optional[str] = None
requested_resolution: Optional[str] = None # tier requested in the challenge (e.g. "1080p")
observed_resolution: Optional[tuple] = None # (width, height) of the stored media
has_audio: Optional[bool] = None # audio track present in the stored media
created_at: float = None
updated_at: float = None

Expand Down
12 changes: 10 additions & 2 deletions gas/evaluation/generative_challenge_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import Dict, Optional

from gas.cache.content_manager import ContentManager
from gas.evaluation.resolution_tiers import sample_challenge_tier
from gas.protocol.epistula import get_verifier
from gas.protocol.validator_requests import query_generative_miner
from gas.types import MediaType, MinerType, Modality
Expand Down Expand Up @@ -141,7 +142,12 @@ async def issue_generative_challenge(self):
async def send_generative_request(self, uid: int, prompt_entry, modality: Modality):
"""Scoring is handled by the callback in GeneratorEvaluator"""

#parameters = {"width": 1024, "height": 1024}
# Challenges request a resolution tier (video: 480p/720p/1080p,
# image: 1K/2K/4K); rewards are priced at min(observed, requested) so
# overshooting never pays more and miners whose model can't reach the
# tier degrade to a lower price, not failure.
requested_resolution = sample_challenge_tier(modality.value)
parameters = {"resolution": requested_resolution}

async with aiohttp.ClientSession() as session:
response_data = await query_generative_miner(
Expand All @@ -152,7 +158,7 @@ async def send_generative_request(self, uid: int, prompt_entry, modality: Modali
prompt=prompt_entry.content,
modality=modality,
webhook_url=self.generative_callback_url,
parameters=None,
parameters=parameters,
total_timeout=self.config.neuron.miner_total_timeout,
)

Expand All @@ -169,6 +175,7 @@ async def send_generative_request(self, uid: int, prompt_entry, modality: Modali
"media_type": MediaType.SYNTHETIC,
"status": "pending",
"sent_at": time.time(),
"requested_resolution": requested_resolution,
}
self.content_manager.record_challenge_outcome(
task_id=miner_task_id,
Expand All @@ -177,6 +184,7 @@ async def send_generative_request(self, uid: int, prompt_entry, modality: Modali
prompt_id=prompt_entry.id,
modality=modality.value,
status="pending",
requested_resolution=requested_resolution,
)
bt.logging.info(
f"Stored challenge task {miner_task_id} for UID {uid}. Total active tasks: {len(self.challenge_tasks)}"
Expand Down
Loading
Loading