Spaces:
Sleeping
Sleeping
Download llm_services/llm_usage.py from EnvironmentalAI/WaterScopeAI-Search: direct link, hf CLI and curl.
- Browser
- Download file 1.1 kB
-
https://huggingface.co/spaces/EnvironmentalAI/WaterScopeAI-Search/resolve/main/llm_services/llm_usage.py
- Command line
-
hf download hf://spaces/EnvironmentalAI/WaterScopeAI-Search/llm_services/llm_usage.py
-
curl -L -o llm_usage.py https://huggingface.co/spaces/EnvironmentalAI/WaterScopeAI-Search/resolve/main/llm_services/llm_usage.py
1.1 kB
| """Safe usage summaries; no prompts or credential values are stored. | |
| Provide provider-specific prices through environment variables if desired; | |
| otherwise estimated_cost_usd is None (not an asserted price). | |
| """ | |
| import os | |
| def estimate_cost_usd(provider: str, usage: dict): | |
| prefix = provider.upper() | |
| in_price = os.getenv(f"{prefix}_INPUT_USD_PER_M") | |
| out_price = os.getenv(f"{prefix}_OUTPUT_USD_PER_M") | |
| if not in_price or not out_price: | |
| return None | |
| n_in = usage.get("input_tokens") | |
| n_out = usage.get("output_tokens") | |
| if n_in is None or n_out is None: | |
| return None | |
| try: | |
| cost = (n_in * float(in_price) + n_out * float(out_price)) / 1_000_000 | |
| return round(cost, 8) | |
| except (ValueError, TypeError): | |
| return None | |
| def usage_summary(result: dict): | |
| return { | |
| "provider": result["provider"], | |
| "model": result["model_requested"], | |
| "elapsed_s": result.get("elapsed_s"), | |
| "usage": result.get("usage", {}), | |
| "estimated_cost_usd": estimate_cost_usd(result["provider"], result.get("usage", {})), | |
| } | |