Spaces:
Running
Running
adds lotties
Browse files- core/seed.py +19 -20
- data/viralata.db +0 -0
core/seed.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
"""
|
| 2 |
seed.py — Dados de demonstração para o mapa de Brasília, DF.
|
| 3 |
-
Chamado no startup do app.py
|
| 4 |
"""
|
| 5 |
import json
|
| 6 |
import logging
|
|
@@ -10,9 +10,6 @@ import numpy as np
|
|
| 10 |
|
| 11 |
log = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
-
# Animais de demonstração espalhados por Brasília, DF.
|
| 14 |
-
# photo_path é relativo ao DATA_DIR (ex: 'photos/seed/pretao.jpg'),
|
| 15 |
-
# servido em /photos/seed/pretao.jpg pelo static mount.
|
| 16 |
_SEED_ANIMALS = [
|
| 17 |
{
|
| 18 |
"species": "dog",
|
|
@@ -46,7 +43,7 @@ _SEED_ANIMALS = [
|
|
| 46 |
"secondary_colors": ["caramelo"],
|
| 47 |
"distinctive_marks": ["manchas caramelo nas costas", "orelhas caramelo"],
|
| 48 |
"condition": "saudável",
|
| 49 |
-
"description_text": "medium white dog with caramel patches, resting
|
| 50 |
"_ai_success": True,
|
| 51 |
},
|
| 52 |
"sightings": [
|
|
@@ -137,47 +134,49 @@ _SEED_ANIMALS = [
|
|
| 137 |
]
|
| 138 |
|
| 139 |
|
| 140 |
-
def _random_embedding()
|
| 141 |
-
"""Gera um embedding aleatório normalizado (384-dim, float32)."""
|
| 142 |
v = np.random.randn(384).astype(np.float32)
|
| 143 |
v /= np.linalg.norm(v)
|
| 144 |
return v.tobytes()
|
| 145 |
|
| 146 |
|
| 147 |
-
def seed_if_empty(db)
|
| 148 |
-
"""Garante que os animais fixos
|
| 149 |
-
Insere apenas os que ainda
|
| 150 |
-
Dados reais dos
|
| 151 |
"""
|
| 152 |
with db._conn() as conn:
|
| 153 |
existing_names = {
|
| 154 |
-
row[0] for row in conn.execute(
|
|
|
|
|
|
|
| 155 |
}
|
| 156 |
|
| 157 |
to_insert = [a for a in _SEED_ANIMALS if a["name"] not in existing_names]
|
| 158 |
if not to_insert:
|
| 159 |
-
log.info("Animais fixos
|
| 160 |
return
|
| 161 |
|
| 162 |
-
log.info("Inserindo %d animais fixos de
|
| 163 |
now = datetime.utcnow()
|
| 164 |
|
| 165 |
with db._conn() as conn:
|
| 166 |
for animal_data in to_insert:
|
| 167 |
sightings = animal_data["sightings"]
|
| 168 |
desc = animal_data["description"]
|
| 169 |
-
photo_path =
|
| 170 |
|
| 171 |
most_recent_days = min(s["days_ago"] for s in sightings)
|
| 172 |
last_seen = now - timedelta(days=most_recent_days)
|
| 173 |
first_seen = now - timedelta(days=max(s["days_ago"] for s in sightings))
|
| 174 |
|
| 175 |
cur = conn.execute(
|
| 176 |
-
"INSERT INTO animals
|
|
|
|
| 177 |
" VALUES (?, ?, ?, ?, ?, ?, ?)",
|
| 178 |
(
|
| 179 |
animal_data["species"],
|
| 180 |
-
animal_data
|
| 181 |
json.dumps(desc, ensure_ascii=False),
|
| 182 |
_random_embedding(),
|
| 183 |
first_seen.strftime("%Y-%m-%d %H:%M:%S"),
|
|
@@ -189,10 +188,10 @@ def seed_if_empty(db) -> None:
|
|
| 189 |
|
| 190 |
for i, s in enumerate(sightings):
|
| 191 |
created_at = now - timedelta(days=s["days_ago"])
|
| 192 |
-
# foto só no avistamento mais recente
|
| 193 |
s_photo = photo_path if i == len(sightings) - 1 else None
|
| 194 |
conn.execute(
|
| 195 |
-
"INSERT INTO sightings
|
|
|
|
| 196 |
" VALUES (?, ?, ?, ?, ?, ?)",
|
| 197 |
(
|
| 198 |
animal_id,
|
|
@@ -204,6 +203,6 @@ def seed_if_empty(db) -> None:
|
|
| 204 |
),
|
| 205 |
)
|
| 206 |
|
| 207 |
-
log.info("Seed
|
| 208 |
len(to_insert),
|
| 209 |
sum(len(a["sightings"]) for a in to_insert))
|
|
|
|
| 1 |
"""
|
| 2 |
seed.py — Dados de demonstração para o mapa de Brasília, DF.
|
| 3 |
+
Chamado no startup do app.py. Garante que os 6 animais fixos estão sempre no banco.
|
| 4 |
"""
|
| 5 |
import json
|
| 6 |
import logging
|
|
|
|
| 10 |
|
| 11 |
log = logging.getLogger(__name__)
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
_SEED_ANIMALS = [
|
| 14 |
{
|
| 15 |
"species": "dog",
|
|
|
|
| 43 |
"secondary_colors": ["caramelo"],
|
| 44 |
"distinctive_marks": ["manchas caramelo nas costas", "orelhas caramelo"],
|
| 45 |
"condition": "saudável",
|
| 46 |
+
"description_text": "medium white dog with caramel patches, resting near closed shop",
|
| 47 |
"_ai_success": True,
|
| 48 |
},
|
| 49 |
"sightings": [
|
|
|
|
| 134 |
]
|
| 135 |
|
| 136 |
|
| 137 |
+
def _random_embedding():
|
|
|
|
| 138 |
v = np.random.randn(384).astype(np.float32)
|
| 139 |
v /= np.linalg.norm(v)
|
| 140 |
return v.tobytes()
|
| 141 |
|
| 142 |
|
| 143 |
+
def seed_if_empty(db):
|
| 144 |
+
"""Garante que os animais fixos estão sempre no banco.
|
| 145 |
+
Insere apenas os que ainda nao existem (por nome).
|
| 146 |
+
Dados reais dos usuarios nunca sao removidos.
|
| 147 |
"""
|
| 148 |
with db._conn() as conn:
|
| 149 |
existing_names = {
|
| 150 |
+
row[0] for row in conn.execute(
|
| 151 |
+
"SELECT name FROM animals WHERE name IS NOT NULL"
|
| 152 |
+
)
|
| 153 |
}
|
| 154 |
|
| 155 |
to_insert = [a for a in _SEED_ANIMALS if a["name"] not in existing_names]
|
| 156 |
if not to_insert:
|
| 157 |
+
log.info("Animais fixos ja presentes — seed ignorado.")
|
| 158 |
return
|
| 159 |
|
| 160 |
+
log.info("Inserindo %d animais fixos de demonstracao...", len(to_insert))
|
| 161 |
now = datetime.utcnow()
|
| 162 |
|
| 163 |
with db._conn() as conn:
|
| 164 |
for animal_data in to_insert:
|
| 165 |
sightings = animal_data["sightings"]
|
| 166 |
desc = animal_data["description"]
|
| 167 |
+
photo_path = "photos/seed/" + animal_data["photo_filename"]
|
| 168 |
|
| 169 |
most_recent_days = min(s["days_ago"] for s in sightings)
|
| 170 |
last_seen = now - timedelta(days=most_recent_days)
|
| 171 |
first_seen = now - timedelta(days=max(s["days_ago"] for s in sightings))
|
| 172 |
|
| 173 |
cur = conn.execute(
|
| 174 |
+
"INSERT INTO animals"
|
| 175 |
+
" (species, name, description, embedding, first_seen, last_seen, sighting_count)"
|
| 176 |
" VALUES (?, ?, ?, ?, ?, ?, ?)",
|
| 177 |
(
|
| 178 |
animal_data["species"],
|
| 179 |
+
animal_data["name"],
|
| 180 |
json.dumps(desc, ensure_ascii=False),
|
| 181 |
_random_embedding(),
|
| 182 |
first_seen.strftime("%Y-%m-%d %H:%M:%S"),
|
|
|
|
| 188 |
|
| 189 |
for i, s in enumerate(sightings):
|
| 190 |
created_at = now - timedelta(days=s["days_ago"])
|
|
|
|
| 191 |
s_photo = photo_path if i == len(sightings) - 1 else None
|
| 192 |
conn.execute(
|
| 193 |
+
"INSERT INTO sightings"
|
| 194 |
+
" (animal_id, photo_path, latitude, longitude, notes, created_at)"
|
| 195 |
" VALUES (?, ?, ?, ?, ?, ?)",
|
| 196 |
(
|
| 197 |
animal_id,
|
|
|
|
| 203 |
),
|
| 204 |
)
|
| 205 |
|
| 206 |
+
log.info("Seed concluido: %d animais inseridos (%d avistamentos).",
|
| 207 |
len(to_insert),
|
| 208 |
sum(len(a["sightings"]) for a in to_insert))
|
data/viralata.db
CHANGED
|
Binary files a/data/viralata.db and b/data/viralata.db differ
|
|
|