Views
No views yet
bert-base-cased model fine-tuned for multi-label country classification. It identifies which countries are mentioned in a paragraph of text from a news article.class_thresholds.json file in this repository.transformers pipeline:Install python package:uv add transformers[torch] requests
1
2from transformers import pipeline
3import json
4import requests
5
6# Load Model and Thresholds
7model_id = "paneru-rajan/bert-news-paragraph-country-classify"
8
9# Load the classification pipeline from the Hub
10pipe = pipeline("text-classification", model=model_id, truncation=True, padding=True, max_length=512)
11
12# Download the recommended thresholds file from the Hub
13try:
14 thresholds_url = f"https://huggingface.co/{model_id}/raw/main/class_thresholds.json"
15 thresholds = requests.get(thresholds_url).json()
16except Exception as e:
17 print(f"Could not download thresholds, using 0.5 for all classes. Error: {e}")
18 thresholds = {}
19
20# Make a Prediction
21text = "Donald Trump has strongly endorsed the Aukus pact and praised prime minister Anthony Albanese as a “great” leader, but the president’s navy secretary says the US may seek to “clarify some ambiguities” in the nuclear submarine deal.\n\nTrump and Albanese also signed a multibillion-dollar agreement for Australia to supply the United States with critical minerals, amid a deepening trade war as China threatens to cut its supply of rare earth elements. But the president also downplayed any prospect of cutting tariffs on Australian goods."
22
23# Get scores for all labels
24predictions = pipe(text, top_k=None)
25
26# Apply Thresholds to Get Final Labels
27final_labels = []
28for pred in predictions:
29 # The model returns country codes (e.g., 'au' for Australia)
30 country_code = pred['label']
31 # Use the specific threshold for that country, or a default of 0.5
32 if pred['score'] > thresholds.get(country_code, 0.5):
33 final_labels.append(country_code)
34
35print(f"Text: '{text}'")
36print(f"Predicted Countries: {final_labels}")
37# Expected output for this example: ['au', 'us', 'cn']google-bert/bert-base-cased2e-5 and a max sequence length of 512.

