Spaces:
Sleeping
Sleeping
| """ | |
| Compare local model vs AI API: latency and estimated cost per query. | |
| """ | |
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| LEVEL_PROMPT = """Rate the complexity of the target word in context. | |
| Levels: Very Easy, Easy, Medium, Hard, Very Hard. | |
| Sentence: {sentence} | |
| Target word: {target_word} | |
| Reply with only the level label.""" | |
| # USD per 1k queries (rough; matches 08_efficiency.py defaults) | |
| COST_PER_1K = { | |
| "openai_gpt4o_mini": 0.15, | |
| "gemini_flash": 0.075, | |
| } | |
| # Typical latency when API is used (ms) — used when no live API call | |
| TYPICAL_AI_LATENCY_MS = { | |
| "openai_gpt4o_mini": 1200.0, | |
| "gemini_flash": 900.0, | |
| } | |
| class QueryComparison: | |
| local_latency_ms: float | |
| ai_latency_ms: float | |
| ai_provider: str | |
| ai_cost_usd: float | |
| ai_cost_per_1k_usd: float | |
| local_cost_usd: float | |
| speedup_factor: float | |
| prompt_tokens_est: int | |
| def _estimate_tokens(text: str) -> int: | |
| return max(1, len(text.split()) + len(text) // 4) | |
| def estimate_ai_cost(sentence: str, target_word: str, provider: str = "openai_gpt4o_mini") -> float: | |
| prompt = LEVEL_PROMPT.format(sentence=sentence, target_word=target_word) | |
| tokens = _estimate_tokens(prompt) + 10 # short reply | |
| # gpt-4o-mini ballpark: ~$0.15/1M input + $0.60/1M output — ~$0.0002 per simple call | |
| cost_per_1k = COST_PER_1K.get(provider, 0.15) | |
| return (cost_per_1k / 1000.0) * (tokens / 200.0) | |
| def compare_query(sentence: str, target_word: str, local_latency_ms: float) -> QueryComparison: | |
| provider = "openai_gpt4o_mini" | |
| prompt = LEVEL_PROMPT.format(sentence=sentence, target_word=target_word) | |
| tokens = _estimate_tokens(prompt) | |
| ai_cost = estimate_ai_cost(sentence, target_word, provider) | |
| ai_latency = TYPICAL_AI_LATENCY_MS[provider] | |
| local_cost = 0.0 | |
| speedup = ai_latency / max(local_latency_ms, 0.1) | |
| return QueryComparison( | |
| local_latency_ms=local_latency_ms, | |
| ai_latency_ms=ai_latency, | |
| ai_provider="GPT-4o-mini (estimated)", | |
| ai_cost_usd=ai_cost, | |
| ai_cost_per_1k_usd=COST_PER_1K[provider], | |
| local_cost_usd=local_cost, | |
| speedup_factor=speedup, | |
| prompt_tokens_est=tokens, | |
| ) | |