Spaces:
Running
Running
imtrt004 commited on
Commit ·
0829183
1
Parent(s): 69975bb
fix: update tier mode
Browse files- app.py +18 -15
- persistence/tier.py +65 -32
app.py
CHANGED
|
@@ -246,7 +246,8 @@ async def chat(req: ChatRequest):
|
|
| 246 |
use_deepmind = req.use_deepmind and deepmind_allowed
|
| 247 |
|
| 248 |
if use_deepmind:
|
| 249 |
-
|
|
|
|
| 250 |
if not dm_ok:
|
| 251 |
raise HTTPException(status_code=429, detail=dm_msg)
|
| 252 |
|
|
@@ -299,13 +300,14 @@ async def chat(req: ChatRequest):
|
|
| 299 |
|
| 300 |
# Save user message (primary doc_id for legacy compatibility)
|
| 301 |
supa.table("chat_history").insert({
|
| 302 |
-
"doc_id":
|
| 303 |
-
"session_id":
|
| 304 |
-
"user_id":
|
| 305 |
-
"role":
|
| 306 |
-
"content":
|
| 307 |
-
"expires_at":
|
| 308 |
-
"is_deepmind":
|
|
|
|
| 309 |
}).execute()
|
| 310 |
|
| 311 |
def generate():
|
|
@@ -328,13 +330,14 @@ async def chat(req: ChatRequest):
|
|
| 328 |
|
| 329 |
# Persist assistant response
|
| 330 |
supa.table("chat_history").insert({
|
| 331 |
-
"doc_id":
|
| 332 |
-
"session_id":
|
| 333 |
-
"user_id":
|
| 334 |
-
"role":
|
| 335 |
-
"content":
|
| 336 |
-
"expires_at":
|
| 337 |
-
"is_deepmind":
|
|
|
|
| 338 |
}).execute()
|
| 339 |
yield "data: [DONE]\n\n"
|
| 340 |
|
|
|
|
| 246 |
use_deepmind = req.use_deepmind and deepmind_allowed
|
| 247 |
|
| 248 |
if use_deepmind:
|
| 249 |
+
submode = req.deepmind_submode or "pro"
|
| 250 |
+
dm_ok, dm_msg = check_deepmind_limit(req.user_id, submode)
|
| 251 |
if not dm_ok:
|
| 252 |
raise HTTPException(status_code=429, detail=dm_msg)
|
| 253 |
|
|
|
|
| 300 |
|
| 301 |
# Save user message (primary doc_id for legacy compatibility)
|
| 302 |
supa.table("chat_history").insert({
|
| 303 |
+
"doc_id": req.doc_id,
|
| 304 |
+
"session_id": req.session_id,
|
| 305 |
+
"user_id": req.user_id,
|
| 306 |
+
"role": "user",
|
| 307 |
+
"content": req.query,
|
| 308 |
+
"expires_at": expires.isoformat(),
|
| 309 |
+
"is_deepmind": use_deepmind,
|
| 310 |
+
"deepmind_submode": (req.deepmind_submode or "pro") if use_deepmind else None,
|
| 311 |
}).execute()
|
| 312 |
|
| 313 |
def generate():
|
|
|
|
| 330 |
|
| 331 |
# Persist assistant response
|
| 332 |
supa.table("chat_history").insert({
|
| 333 |
+
"doc_id": req.doc_id,
|
| 334 |
+
"session_id": req.session_id,
|
| 335 |
+
"user_id": req.user_id,
|
| 336 |
+
"role": "assistant",
|
| 337 |
+
"content": "".join(full_resp),
|
| 338 |
+
"expires_at": expires.isoformat(),
|
| 339 |
+
"is_deepmind": use_deepmind,
|
| 340 |
+
"deepmind_submode": (req.deepmind_submode or "pro") if use_deepmind else None,
|
| 341 |
}).execute()
|
| 342 |
yield "data: [DONE]\n\n"
|
| 343 |
|
persistence/tier.py
CHANGED
|
@@ -26,11 +26,19 @@ DOC_LIMIT: dict[Tier, int | None] = {Tier.FREE: 1, Tier.PRO: 10, Tier.SCHOLAR:
|
|
| 26 |
# General message limits (per session for FREE, per day for paid)
|
| 27 |
MSG_LIMIT: dict[Tier, int | None] = {Tier.FREE: 5, Tier.PRO: 200, Tier.SCHOLAR: None}
|
| 28 |
|
| 29 |
-
# DeepMind
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
Tier.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
|
|
@@ -111,14 +119,15 @@ def check_message_limit(user_id: str, session_id: str) -> tuple[bool, str]:
|
|
| 111 |
return True, "ok"
|
| 112 |
|
| 113 |
|
| 114 |
-
def check_deepmind_limit(user_id: str) -> tuple[bool, str]:
|
| 115 |
-
"""Check the user's DeepMind
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
|
|
|
| 119 |
"""
|
| 120 |
tier = get_user_tier(user_id)
|
| 121 |
-
limit =
|
| 122 |
if limit is None:
|
| 123 |
return True, "ok"
|
| 124 |
|
|
@@ -130,41 +139,65 @@ def check_deepmind_limit(user_id: str) -> tuple[bool, str]:
|
|
| 130 |
.eq("user_id", user_id)
|
| 131 |
.eq("role", "user")
|
| 132 |
.eq("is_deepmind", True)
|
|
|
|
| 133 |
.gte("created_at", today)
|
| 134 |
.execute()
|
| 135 |
.count
|
| 136 |
-
)
|
| 137 |
|
| 138 |
if count >= limit:
|
| 139 |
-
tier_label
|
|
|
|
| 140 |
return False, (
|
| 141 |
-
f"DeepMind daily limit reached ({limit} messages/day on {tier_label} plan). "
|
| 142 |
"Resets at midnight UTC."
|
| 143 |
)
|
| 144 |
return True, "ok"
|
| 145 |
|
| 146 |
|
| 147 |
def get_deepmind_usage(user_id: str) -> dict:
|
| 148 |
-
"""Return DeepMind usage stats for today
|
| 149 |
-
|
| 150 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
return {
|
| 166 |
-
"
|
| 167 |
-
"
|
| 168 |
-
|
| 169 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 170 |
}
|
|
|
|
| 26 |
# General message limits (per session for FREE, per day for paid)
|
| 27 |
MSG_LIMIT: dict[Tier, int | None] = {Tier.FREE: 5, Tier.PRO: 200, Tier.SCHOLAR: None}
|
| 28 |
|
| 29 |
+
# DeepMind daily message limits — split by submode.
|
| 30 |
+
# "pro" submode = Groq-powered (fast, context-limited)
|
| 31 |
+
# "super" submode = Cerebras-powered (ultra-fast, full-context)
|
| 32 |
+
DEEPMIND_PRO_LIMIT: dict[Tier, int | None] = {
|
| 33 |
+
Tier.FREE: 5, # 5 Pro messages/day
|
| 34 |
+
Tier.PRO: 100, # 100 Pro messages/day
|
| 35 |
+
Tier.SCHOLAR: 300, # 300 Pro messages/day
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
DEEPMIND_SUPER_LIMIT: dict[Tier, int | None] = {
|
| 39 |
+
Tier.FREE: 2, # 2 Super messages/day (Free users)
|
| 40 |
+
Tier.PRO: 50, # 50 Super messages/day
|
| 41 |
+
Tier.SCHOLAR: 200, # 200 Super messages/day
|
| 42 |
}
|
| 43 |
|
| 44 |
|
|
|
|
| 119 |
return True, "ok"
|
| 120 |
|
| 121 |
|
| 122 |
+
def check_deepmind_limit(user_id: str, submode: str = "pro") -> tuple[bool, str]:
|
| 123 |
+
"""Check the user's DeepMind daily limit for the given submode.
|
| 124 |
|
| 125 |
+
Submode 'pro' → Groq-powered — limits: Free=5, Pro=100, Scholar=300
|
| 126 |
+
Submode 'super' → Cerebras-powered — limits: Free=2, Pro=50, Scholar=200
|
| 127 |
+
All counts are per UTC calendar day.
|
| 128 |
"""
|
| 129 |
tier = get_user_tier(user_id)
|
| 130 |
+
limit = (DEEPMIND_SUPER_LIMIT if submode == "super" else DEEPMIND_PRO_LIMIT)[tier]
|
| 131 |
if limit is None:
|
| 132 |
return True, "ok"
|
| 133 |
|
|
|
|
| 139 |
.eq("user_id", user_id)
|
| 140 |
.eq("role", "user")
|
| 141 |
.eq("is_deepmind", True)
|
| 142 |
+
.eq("deepmind_submode", submode)
|
| 143 |
.gte("created_at", today)
|
| 144 |
.execute()
|
| 145 |
.count
|
| 146 |
+
) or 0
|
| 147 |
|
| 148 |
if count >= limit:
|
| 149 |
+
tier_label = tier.capitalize()
|
| 150 |
+
mode_label = "Super" if submode == "super" else "Pro"
|
| 151 |
return False, (
|
| 152 |
+
f"DeepMind {mode_label} daily limit reached ({limit} messages/day on {tier_label} plan). "
|
| 153 |
"Resets at midnight UTC."
|
| 154 |
)
|
| 155 |
return True, "ok"
|
| 156 |
|
| 157 |
|
| 158 |
def get_deepmind_usage(user_id: str) -> dict:
|
| 159 |
+
"""Return per-submode DeepMind usage stats for today.
|
| 160 |
+
|
| 161 |
+
Returns:
|
| 162 |
+
{
|
| 163 |
+
tier: str,
|
| 164 |
+
pro: { used, limit, remaining },
|
| 165 |
+
super: { used, limit, remaining },
|
| 166 |
+
}
|
| 167 |
+
"""
|
| 168 |
+
tier = get_user_tier(user_id)
|
| 169 |
+
client = _client()
|
| 170 |
+
today = datetime.now(UTC).date().isoformat()
|
| 171 |
|
| 172 |
+
def _count(submode: str) -> int:
|
| 173 |
+
return (
|
| 174 |
+
client
|
| 175 |
+
.table("chat_history")
|
| 176 |
+
.select("id", count="exact")
|
| 177 |
+
.eq("user_id", user_id)
|
| 178 |
+
.eq("role", "user")
|
| 179 |
+
.eq("is_deepmind", True)
|
| 180 |
+
.eq("deepmind_submode", submode)
|
| 181 |
+
.gte("created_at", today)
|
| 182 |
+
.execute()
|
| 183 |
+
.count
|
| 184 |
+
) or 0
|
| 185 |
+
|
| 186 |
+
pro_used = _count("pro")
|
| 187 |
+
super_used = _count("super")
|
| 188 |
+
pro_limit = DEEPMIND_PRO_LIMIT[tier]
|
| 189 |
+
sup_limit = DEEPMIND_SUPER_LIMIT[tier]
|
| 190 |
|
| 191 |
return {
|
| 192 |
+
"tier": str(tier),
|
| 193 |
+
"pro": {
|
| 194 |
+
"used": pro_used,
|
| 195 |
+
"limit": pro_limit,
|
| 196 |
+
"remaining": (pro_limit - pro_used) if pro_limit is not None else None,
|
| 197 |
+
},
|
| 198 |
+
"super": {
|
| 199 |
+
"used": super_used,
|
| 200 |
+
"limit": sup_limit,
|
| 201 |
+
"remaining": (sup_limit - super_used) if sup_limit is not None else None,
|
| 202 |
+
},
|
| 203 |
}
|