Spaces:
Running
Running
Update usage_logging.py
Browse files- usage_logging.py +26 -23
usage_logging.py
CHANGED
|
@@ -145,32 +145,35 @@ def _country_lookup(ip: str) -> tuple[str, str]:
|
|
| 145 |
return ("", "")
|
| 146 |
|
| 147 |
|
| 148 |
-
def append_visit_to_dataset(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
api = _hf_api()
|
| 150 |
if not api:
|
| 151 |
return
|
| 152 |
|
| 153 |
-
existing = _download_text_if_exists(
|
| 154 |
-
USAGE_DATASET_REPO,
|
| 155 |
-
USAGE_JSONL_PATH
|
| 156 |
-
)
|
| 157 |
|
| 158 |
event = {
|
| 159 |
"ts_utc": datetime.utcnow().isoformat() + "Z",
|
| 160 |
"space_url": SPACE_URL,
|
| 161 |
-
"country": country,
|
| 162 |
-
"
|
|
|
|
|
|
|
| 163 |
"event": event_type,
|
| 164 |
}
|
| 165 |
|
|
|
|
| 166 |
if extra_fields:
|
| 167 |
-
event.update(extra_fields)
|
| 168 |
|
| 169 |
-
new_content = (
|
| 170 |
-
existing.rstrip("\n") + "\n"
|
| 171 |
-
if existing.strip()
|
| 172 |
-
else ""
|
| 173 |
-
) + json.dumps(event) + "\n"
|
| 174 |
|
| 175 |
try:
|
| 176 |
api.upload_file(
|
|
@@ -189,11 +192,11 @@ def record_visit(request: gr.Request):
|
|
| 189 |
country_hint = _country_from_headers(request)
|
| 190 |
if _is_valid_country_code(country_hint):
|
| 191 |
append_visit_to_dataset(
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
)
|
| 198 |
return
|
| 199 |
|
|
@@ -203,11 +206,11 @@ def record_visit(request: gr.Request):
|
|
| 203 |
cc, name = _country_lookup(ip)
|
| 204 |
if _is_valid_country_code(cc):
|
| 205 |
append_visit_to_dataset(
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
)
|
| 212 |
else:
|
| 213 |
append_visit_to_dataset(
|
|
|
|
| 145 |
return ("", "")
|
| 146 |
|
| 147 |
|
| 148 |
+
def append_visit_to_dataset(
|
| 149 |
+
country: str,
|
| 150 |
+
city: str,
|
| 151 |
+
event_type: str = "usage_start",
|
| 152 |
+
country_source: str = "unknown",
|
| 153 |
+
country_code: str = "",
|
| 154 |
+
**extra_fields
|
| 155 |
+
):
|
| 156 |
api = _hf_api()
|
| 157 |
if not api:
|
| 158 |
return
|
| 159 |
|
| 160 |
+
existing = _download_text_if_exists(USAGE_DATASET_REPO, USAGE_JSONL_PATH)
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
event = {
|
| 163 |
"ts_utc": datetime.utcnow().isoformat() + "Z",
|
| 164 |
"space_url": SPACE_URL,
|
| 165 |
+
"country": country or "Unknown",
|
| 166 |
+
"country_code": (country_code or "").strip().upper(),
|
| 167 |
+
"country_source": country_source or "unknown",
|
| 168 |
+
"city": city or "",
|
| 169 |
"event": event_type,
|
| 170 |
}
|
| 171 |
|
| 172 |
+
# Drop None values so you never write JSON nulls
|
| 173 |
if extra_fields:
|
| 174 |
+
event.update({k: v for k, v in extra_fields.items() if v is not None})
|
| 175 |
|
| 176 |
+
new_content = (existing.rstrip("\n") + "\n" if existing.strip() else "") + json.dumps(event) + "\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
try:
|
| 179 |
api.upload_file(
|
|
|
|
| 192 |
country_hint = _country_from_headers(request)
|
| 193 |
if _is_valid_country_code(country_hint):
|
| 194 |
append_visit_to_dataset(
|
| 195 |
+
country=_expand_country_code(country_hint),
|
| 196 |
+
city="",
|
| 197 |
+
event_type="usage_start",
|
| 198 |
+
country_source="header",
|
| 199 |
+
country_code=country_hint,
|
| 200 |
)
|
| 201 |
return
|
| 202 |
|
|
|
|
| 206 |
cc, name = _country_lookup(ip)
|
| 207 |
if _is_valid_country_code(cc):
|
| 208 |
append_visit_to_dataset(
|
| 209 |
+
country=name or _expand_country_code(cc),
|
| 210 |
+
city="",
|
| 211 |
+
event_type="usage_start",
|
| 212 |
+
country_source="ipinfo",
|
| 213 |
+
country_code=cc,
|
| 214 |
)
|
| 215 |
else:
|
| 216 |
append_visit_to_dataset(
|