xPushkal Claude Opus 4.8 commited on
Commit
b7a4e12
·
1 Parent(s): d095888

fix: app/models package was silently gitignored and never committed

Browse files

.gitignore had an unanchored "models/" (intended for a root model cache),
which git matches at every depth — so it also ignored app/models/, the ORM
package (Video, Chunk). The app ran locally only because the files sat
untracked on disk; a fresh clone or Docker build (which builds from the git
tree) crashed with ModuleNotFoundError: No module named 'app.models'.

Anchor the pattern to "/models/" and commit the package.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

.gitignore CHANGED
@@ -11,10 +11,11 @@ venv/
11
  *.session
12
  *.session-*
13
 
14
- # Media / model cache
 
15
  data/media/
16
  .cache/
17
- models/
18
 
19
  # Node / Next.js
20
  frontend/node_modules/
 
11
  *.session
12
  *.session-*
13
 
14
+ # Media / model cache (anchored to repo root so they don't shadow source dirs
15
+ # like app/models — an unanchored "models/" matches at every depth in git).
16
  data/media/
17
  .cache/
18
+ /models/
19
 
20
  # Node / Next.js
21
  frontend/node_modules/
app/models/__init__.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ """ORM models. Importing this package registers all tables on Base.metadata."""
2
+ from app.models.chunk import Chunk
3
+ from app.models.video import Video
4
+
5
+ __all__ = ["Video", "Chunk"]
app/models/chunk.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Transcript chunks with their embeddings, tagged by video and modality.
2
+
3
+ Only content (transcript/hook) lives here. Numbers never become chunks — they
4
+ are answered from the `videos` table by the quantitative path, so the retriever
5
+ can never surface a number-as-text and have it treated as a fact (README).
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from pgvector.sqlalchemy import Vector
10
+ from sqlalchemy import Float, ForeignKey, Integer, String, Text
11
+ from sqlalchemy.orm import Mapped, mapped_column
12
+
13
+ from app.core.config import get_settings
14
+ from app.core.db import Base
15
+
16
+ _EMBED_DIM = get_settings().embedding_dim
17
+
18
+ # Chunk modalities. transcript/hook are wired now; ocr/visual are reserved for
19
+ # the Phase-3 multimodal hook analysis (schema is additive).
20
+ MODALITY_TRANSCRIPT = "transcript"
21
+ MODALITY_HOOK = "hook"
22
+
23
+
24
+ class Chunk(Base):
25
+ __tablename__ = "chunks"
26
+
27
+ chunk_id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
28
+ video_id: Mapped[str] = mapped_column(
29
+ String(1), ForeignKey("videos.video_id", ondelete="CASCADE"), index=True
30
+ )
31
+ modality: Mapped[str] = mapped_column(String(16), default=MODALITY_TRANSCRIPT)
32
+ text: Mapped[str] = mapped_column(Text)
33
+ start: Mapped[float | None] = mapped_column(Float, nullable=True) # seconds
34
+ end: Mapped[float | None] = mapped_column(Float, nullable=True)
35
+ embedding: Mapped[list[float]] = mapped_column(Vector(_EMBED_DIM))
app/models/video.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Video metadata: the normalized, cross-platform schema and its ORM table.
2
+
3
+ `engagement_rate` is deliberately NOT a stored column. It is derived from
4
+ likes/comments/views in code (see `VideoMetadata.engagement_rate`) so it can
5
+ never drift from its inputs (PRD FR-5, NFR-1, data §9).
6
+ """
7
+ from __future__ import annotations
8
+
9
+ from datetime import date, datetime
10
+
11
+ from pydantic import BaseModel, computed_field
12
+ from sqlalchemy import BigInteger, Date, DateTime, Float, String, func
13
+ from sqlalchemy.dialects.postgresql import ARRAY
14
+ from sqlalchemy.orm import Mapped, mapped_column
15
+
16
+ from app.core.db import Base
17
+
18
+
19
+ def compute_engagement_rate(
20
+ likes: int | None, comments: int | None, views: int | None
21
+ ) -> float | None:
22
+ """(likes + comments) / views * 100 — the one number that must be exact."""
23
+ if not views: # None or 0 → undefined
24
+ return None
25
+ return round(((likes or 0) + (comments or 0)) / views * 100, 4)
26
+
27
+
28
+ class Video(Base):
29
+ __tablename__ = "videos"
30
+
31
+ # "A" (YouTube) or "B" (Instagram). Primary key — one pair per instance.
32
+ video_id: Mapped[str] = mapped_column(String(1), primary_key=True)
33
+ source: Mapped[str] = mapped_column(String(16)) # youtube | instagram
34
+ url: Mapped[str] = mapped_column(String)
35
+
36
+ creator: Mapped[str | None] = mapped_column(String, nullable=True)
37
+ follower_count: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
38
+ views: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
39
+ likes: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
40
+ comments: Mapped[int | None] = mapped_column(BigInteger, nullable=True)
41
+
42
+ hashtags: Mapped[list[str]] = mapped_column(ARRAY(String), default=list)
43
+ upload_date: Mapped[date | None] = mapped_column(Date, nullable=True)
44
+ duration: Mapped[float | None] = mapped_column(Float, nullable=True) # seconds
45
+ title: Mapped[str | None] = mapped_column(String, nullable=True)
46
+ caption: Mapped[str | None] = mapped_column(String, nullable=True)
47
+
48
+ created_at: Mapped[datetime] = mapped_column(
49
+ DateTime(timezone=True), server_default=func.now()
50
+ )
51
+
52
+ @property
53
+ def engagement_rate(self) -> float | None:
54
+ return compute_engagement_rate(self.likes, self.comments, self.views)
55
+
56
+
57
+ class VideoMetadata(BaseModel):
58
+ """Normalized cross-platform schema produced by the ingestion layer and
59
+ returned by the API. Engagement rate is a computed field."""
60
+
61
+ video_id: str
62
+ source: str
63
+ url: str
64
+ creator: str | None = None
65
+ follower_count: int | None = None
66
+ views: int | None = None
67
+ likes: int | None = None
68
+ comments: int | None = None
69
+ hashtags: list[str] = []
70
+ upload_date: date | None = None
71
+ duration: float | None = None
72
+ title: str | None = None
73
+ caption: str | None = None
74
+
75
+ @computed_field # type: ignore[prop-decorator]
76
+ @property
77
+ def engagement_rate(self) -> float | None:
78
+ return compute_engagement_rate(self.likes, self.comments, self.views)
79
+
80
+ @classmethod
81
+ def from_orm_video(cls, v: Video) -> "VideoMetadata":
82
+ return cls(
83
+ video_id=v.video_id,
84
+ source=v.source,
85
+ url=v.url,
86
+ creator=v.creator,
87
+ follower_count=v.follower_count,
88
+ views=v.views,
89
+ likes=v.likes,
90
+ comments=v.comments,
91
+ hashtags=list(v.hashtags or []),
92
+ upload_date=v.upload_date,
93
+ duration=v.duration,
94
+ title=v.title,
95
+ caption=v.caption,
96
+ )