Spaces:
Paused
Paused
Download lib/pricing.ts from imkrish/ig-costing: direct link, hf CLI and curl.
- Browser
- Download file 8.54 kB
-
https://huggingface.co/spaces/imkrish/ig-costing/resolve/main/lib/pricing.ts
- Command line
-
hf download hf://spaces/imkrish/ig-costing/lib/pricing.ts
-
curl -L -o pricing.ts https://huggingface.co/spaces/imkrish/ig-costing/resolve/main/lib/pricing.ts
8.54 kB
| export type UnitType = 'token' | 'second' | 'char' | 'credit' | |
| export interface PricingEntry { | |
| type: 'llm' | 'stt' | 'tts' | 'api' | |
| model: string | |
| provider: string | |
| unit_type: UnitType | |
| input_rate?: number | |
| output_rate?: number | |
| rate?: number | |
| rate_key?: 'rate_per_second' | 'rate_per_character' | 'cost_per_submission' | 'cost_per_token' | 'cost_per_character' | |
| } | |
| export const LLM_PRICING: Record<string, { input_rate: number; output_rate: number; provider: string }> = { | |
| 'gpt-5': { input_rate: 0.00000125, output_rate: 0.00001, provider: 'openai' }, | |
| 'gpt-4o-mini': { input_rate: 0.00000015, output_rate: 0.0000006, provider: 'openai' }, | |
| 'gpt-4o': { input_rate: 0.0000025, output_rate: 0.00001, provider: 'openai' }, | |
| 'gpt-4': { input_rate: 0.00003, output_rate: 0.00006, provider: 'openai' }, | |
| 'gpt-3.5-turbo-0125': { input_rate: 0.0000005, output_rate: 0.0000015, provider: 'openai' }, | |
| 'gpt-4o-mini-2024-07-18': { input_rate: 0.00000015, output_rate: 0.0000006, provider: 'openai' }, | |
| 'gpt-4o-2024-08-06': { input_rate: 0.0000025, output_rate: 0.00001, provider: 'openai' }, | |
| 'llama3-70b-8192': { input_rate: 0.00000059, output_rate: 0.00000079, provider: 'groq' }, | |
| 'gemini-2.0-flash': { input_rate: 0.0000001, output_rate: 0.0000004, provider: 'google' }, | |
| 'gemini-2.5-pro': { input_rate: 0.00000125, output_rate: 0.00001, provider: 'google' }, | |
| 'gemini-2.5-flash-lite': { input_rate: 0.0000001, output_rate: 0.0000004, provider: 'google' }, | |
| 'gemini-2.5-flash-lite-preview-tts': { input_rate: 0.0000005, output_rate: 0.00001, provider: 'google' }, | |
| 'gemini-2.5-pro-preview-tts': { input_rate: 0.00000125, output_rate: 0.00001, provider: 'google' }, | |
| 'gemini-2.5-flash-preview-tts': { input_rate: 0.0000005, output_rate: 0.00001, provider: 'google' }, | |
| 'gemini-1.5-pro': { input_rate: 0.00000125, output_rate: 0.000005, provider: 'google' }, | |
| 'gemini-3-flash-preview': { input_rate: 0.0000005, output_rate: 0.000003, provider: 'google' }, | |
| 'gemini-3-pro-preview': { input_rate: 0.000002, output_rate: 0.000012, provider: 'google' }, | |
| 'claude-sonnet-4-20250514': { input_rate: 0.000003, output_rate: 0.000015, provider: 'anthropic' }, | |
| 'claude-sonnet-4-5-20250929': { input_rate: 0.000003, output_rate: 0.000015, provider: 'anthropic' }, | |
| 'claude-opus-4-20250514': { input_rate: 0.000015, output_rate: 0.000075, provider: 'anthropic' }, | |
| 'gemini-3.1-pro-preview': { input_rate: 0.000002, output_rate: 0.000012, provider: 'google' }, | |
| 'meta-llama/llama-4-scout-17b-16e-instruct': { input_rate: 0.00000011, output_rate: 0.00000034, provider: 'groq' }, | |
| } | |
| export const STT_PRICING: Record<string, { rate_per_second: number; provider: string }> = { | |
| 'whisper-1': { rate_per_second: 0.0001, provider: 'openai' }, | |
| 'whisper': { rate_per_second: 0.0001, provider: 'openai' }, | |
| 'lemonfox_stt': { rate_per_second: 0.000047, provider: 'lemonfox' }, | |
| 'sarvam_stt': { rate_per_second: 0.0001, provider: 'sarvam' }, | |
| 'sarvam_saaras_v3': { rate_per_second: 0.0083, provider: 'sarvam' }, | |
| 'faster-whisper': { rate_per_second: 0.0, provider: 'internal' }, | |
| } | |
| export const TTS_PRICING: Record<string, { rate_per_character: number; provider: string }> = { | |
| 'tts-1': { rate_per_character: 0.000015, provider: 'openai' }, | |
| 'chirp': { rate_per_character: 0.00003, provider: 'google' }, | |
| 'gemini_tts': { rate_per_character: 0.0, provider: 'google' }, | |
| 'indian_tts': { rate_per_character: 0.00002, provider: 'indian_tts' }, | |
| 'sarvam_tts': { rate_per_character: 0.003, provider: 'sarvam' }, | |
| 'bulbul:v3': { rate_per_character: 0.000002, provider: 'sarvam' }, | |
| 'en-US-Chirp-HD': { rate_per_character: 0.00003, provider: 'google' }, | |
| 'gtts': { rate_per_character: 0.0, provider: 'google' }, | |
| // playht bills per character, not per credit — priced here so char_count is actually used | |
| 'playht': { rate_per_character: 0.000156, provider: 'playht' }, | |
| } | |
| export const API_PRICING: Record<string, { cost_per_submission?: number; cost_per_token?: number; cost_per_character?: number; provider: string }> = { | |
| 'judge0_rapidapi': { cost_per_submission: 0.0017, provider: 'judge0' }, | |
| 'judge0': { cost_per_submission: 0.0017, provider: 'judge0' }, | |
| 'heygen': { cost_per_token: 0.099, provider: 'heygen' }, | |
| // ZeroGPT bills AI Detection at $0.069/1000 words; backend sends `usage` = word count, | |
| // so cost_per_token here is really cost-per-word ($0.069/1000 = 0.000069/word). | |
| 'zerogpt': { cost_per_token: 0.000069, provider: 'zerogpt' }, | |
| } | |
| export interface CostResult { | |
| unit_type: UnitType | |
| input_cost: number | |
| output_cost: number | |
| total_cost: number | |
| pricing_snapshot: Record<string, number> | |
| } | |
| export function calcCost( | |
| model: string, | |
| fields: { | |
| input_tokens?: number | |
| output_tokens?: number | |
| audio_seconds?: number | |
| char_count?: number | |
| usage_count?: number | |
| } | |
| ): CostResult | null { | |
| // LLM (token-based) | |
| if (LLM_PRICING[model]) { | |
| const p = LLM_PRICING[model] | |
| const inp = fields.input_tokens ?? 0 | |
| const out = fields.output_tokens ?? 0 | |
| const input_cost = inp * p.input_rate | |
| const output_cost = out * p.output_rate | |
| return { | |
| unit_type: 'token', | |
| input_cost, | |
| output_cost, | |
| total_cost: input_cost + output_cost, | |
| pricing_snapshot: { input_rate: p.input_rate, output_rate: p.output_rate }, | |
| } | |
| } | |
| // STT (second-based) | |
| if (STT_PRICING[model]) { | |
| const p = STT_PRICING[model] | |
| const secs = fields.audio_seconds ?? fields.input_tokens ?? 0 | |
| const cost = secs * p.rate_per_second | |
| return { | |
| unit_type: 'second', | |
| input_cost: cost, | |
| output_cost: 0, | |
| total_cost: cost, | |
| pricing_snapshot: { rate_per_second: p.rate_per_second }, | |
| } | |
| } | |
| // TTS (char-based) | |
| if (TTS_PRICING[model]) { | |
| const p = TTS_PRICING[model] | |
| const chars = fields.char_count ?? fields.output_tokens ?? 0 | |
| const cost = chars * p.rate_per_character | |
| return { | |
| unit_type: 'char', | |
| input_cost: 0, | |
| output_cost: cost, | |
| total_cost: cost, | |
| pricing_snapshot: { rate_per_character: p.rate_per_character }, | |
| } | |
| } | |
| // API (credit-based) | |
| if (API_PRICING[model]) { | |
| const p = API_PRICING[model] | |
| const count = fields.usage_count ?? fields.input_tokens ?? 1 | |
| let cost = 0 | |
| let snapshot: Record<string, number> = {} | |
| if (p.cost_per_submission !== undefined) { | |
| cost = count * p.cost_per_submission | |
| snapshot = { cost_per_submission: p.cost_per_submission } | |
| } else if (p.cost_per_token !== undefined) { | |
| cost = count * p.cost_per_token | |
| snapshot = { cost_per_token: p.cost_per_token } | |
| } else if (p.cost_per_character !== undefined) { | |
| cost = count * p.cost_per_character | |
| snapshot = { cost_per_character: p.cost_per_character } | |
| } | |
| return { | |
| unit_type: 'credit', | |
| input_cost: cost, | |
| output_cost: 0, | |
| total_cost: cost, | |
| pricing_snapshot: snapshot, | |
| } | |
| } | |
| return null | |
| } | |
| export function getProviderForModel(model: string): string | null { | |
| return ( | |
| LLM_PRICING[model]?.provider ?? | |
| STT_PRICING[model]?.provider ?? | |
| TTS_PRICING[model]?.provider ?? | |
| API_PRICING[model]?.provider ?? | |
| null | |
| ) | |
| } | |
| export function getServiceType(model: string): string { | |
| if (model === 'heygen') return 'Avatar' | |
| if (LLM_PRICING[model]) return 'LLM' | |
| if (STT_PRICING[model]) return 'STT' | |
| if (TTS_PRICING[model]) return 'TTS' | |
| if (API_PRICING[model]) return 'API' | |
| return 'Unknown' | |
| } | |
| export function getAllPricing() { | |
| const entries: PricingEntry[] = [] | |
| for (const [model, p] of Object.entries(LLM_PRICING)) { | |
| entries.push({ type: 'llm', model, provider: p.provider, unit_type: 'token', input_rate: p.input_rate, output_rate: p.output_rate }) | |
| } | |
| for (const [model, p] of Object.entries(STT_PRICING)) { | |
| entries.push({ type: 'stt', model, provider: p.provider, unit_type: 'second', rate: p.rate_per_second, rate_key: 'rate_per_second' }) | |
| } | |
| for (const [model, p] of Object.entries(TTS_PRICING)) { | |
| entries.push({ type: 'tts', model, provider: p.provider, unit_type: 'char', rate: p.rate_per_character, rate_key: 'rate_per_character' }) | |
| } | |
| for (const [model, p] of Object.entries(API_PRICING)) { | |
| const rate = p.cost_per_submission ?? p.cost_per_token ?? p.cost_per_character ?? 0 | |
| const rate_key = p.cost_per_submission !== undefined ? 'cost_per_submission' | |
| : p.cost_per_token !== undefined ? 'cost_per_token' | |
| : 'cost_per_character' | |
| entries.push({ type: 'api', model, provider: p.provider, unit_type: 'credit', rate, rate_key }) | |
| } | |
| return entries | |
| } | |