Skip to content

Commit

Permalink
add new source
Browse files Browse the repository at this point in the history
  • Loading branch information
WilliamJlvt committed Oct 3, 2024
1 parent 8673aaa commit 5296c52
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ LLM Pricing SDK is a Python package designed to scrape and organize pricing info
from the following sources:
- https://huggingface.co/spaces/philschmid/llm-pricing
- https://www.botgenuity.com/tools/llm-pricing
- https://llm-price.com

## Installation
~~You can install the package using pip:~~ (not yet available)
Expand Down
2 changes: 2 additions & 0 deletions examples/flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ def get_pricing():
results = LlmPricingScraper.scrape(DataSources.BOTGENUITY)
elif source.lower() == DataSources.HUGGINGFACE.value:
results = LlmPricingScraper.scrape(DataSources.HUGGINGFACE)
elif source.lower() == DataSources.HUHUHANG.value:
results = LlmPricingScraper.scrape(DataSources.HUHUHANG)
else:
return jsonify(
{"error": f"Source '{source}' is not supported."}), 400
Expand Down
1 change: 1 addition & 0 deletions llm_pricing_sdk/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
class DataSources(Enum):
BOTGENUITY = "botgenuity"
HUGGINGFACE = "huggingface"
HUHUHANG = "huhuhang"
3 changes: 3 additions & 0 deletions llm_pricing_sdk/scrapers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from llm_pricing_sdk.enums import DataSources
from llm_pricing_sdk.scrapers.botgenuity import BotgenuityScraper
from llm_pricing_sdk.scrapers.huggingface import HuggingfaceScraper
from llm_pricing_sdk.scrapers.huhuhang import HuhuhangScraper

class LlmPricingScraper:
@staticmethod
Expand All @@ -14,5 +15,7 @@ def scrape(source: DataSources = DataSources.HUGGINGFACE):
return BotgenuityScraper.scrape()
elif source == DataSources.HUGGINGFACE:
return HuggingfaceScraper.scrape()
elif source == DataSources.HUHUHANG:
return HuhuhangScraper.scrape()
else:
raise Exception(f"Source '{source}' is not supported.")
45 changes: 45 additions & 0 deletions llm_pricing_sdk/scrapers/huhuhang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import requests
from datetime import datetime
from llm_pricing_sdk.models import LLMModelPricing

class HuhuhangScraper:
@staticmethod
def scrape():
url = "https://llm-price.huhuhang.workers.dev/"
response = requests.get(url)

if response.status_code != 200:
raise Exception(f"Failed to fetch data from {url}. Status code: {response.status_code}")

data = response.json()

results = []

for item in data:
try:
fields = item["fields"]
model_name = fields.get("model_name", "")
provider = fields.get("provider", "")
input_tokens_price = fields.get("input_tokens", 0.0)
output_tokens_price = fields.get("output_tokens", 0.0)
source_url = fields.get("url", "")
updated_str = fields.get("update_time", "")

updated_date = datetime.strptime(updated_str, "%Y-%m-%dT%H:%M:%S.%fZ").strftime("%Y-%m-%d")

pricing_data = LLMModelPricing(
model=model_name,
provider=provider,
input_tokens_price=input_tokens_price,
output_tokens_price=output_tokens_price,
context="",
source=source_url,
updated=updated_date
)

results.append(pricing_data)

except Exception as e:
print(f"Error processing item {item['id']}: {e}")

return results

0 comments on commit 5296c52

Please sign in to comment.