Views
No views yet
1import torch, sys, os, tempfile
2from transformers import DistilBertTokenizer
3from huggingface_hub import snapshot_download
4
5device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
6
7def print_sorted_label_scores(label_scores):
8 # Sort label_scores dict by score descending
9 sorted_items = sorted(label_scores.items(), key=lambda x: x[1], reverse=True)
10 for label, score in sorted_items:
11 print(f" {label}: {score:.6f}")
12
13# Model link and examples for this specific model
14model_link = 'sanchow/electric_vehicles-distilbert-classifier'
15examples = [
16 "Switching to electric cars can cut down on smog and carbon output."
17]
18
19print(f"\n{'='*60}")
20print("MODEL: ELECTRIC VEHICLES SECTOR")
21print(f"{'='*60}")
22
23print(f"Downloading model: {model_link}")
24with tempfile.TemporaryDirectory() as temp_dir:
25 snapshot_download(
26 repo_id=model_link,
27 local_dir=temp_dir,
28 local_dir_use_symlinks=False
29 )
30 model_class_path = os.path.join(temp_dir, 'model_class.py')
31 if not os.path.exists(model_class_path):
32 print(f"model_class.py not found in downloaded files")
33 print(f" Available files: {os.listdir(temp_dir)}")
34 else:
35 sys.path.insert(0, temp_dir)
36 from model_class import MultilabelClassifier
37 tokenizer = DistilBertTokenizer.from_pretrained(temp_dir)
38 checkpoint = torch.load(os.path.join(temp_dir, 'model.pt'), map_location='cpu', weights_only=False)
39 model = MultilabelClassifier(checkpoint['model_name'], len(checkpoint['label_names']))
40 model.load_state_dict(checkpoint['model_state_dict'])
41 model.to(device)
42 model.eval()
43 print("Model loaded successfully")
44 print(f" Labels: {checkpoint['label_names']}")
45 print("\nElectric Vehicles classifier results:\n")
46 for i, test_text in enumerate(examples):
47 inputs = tokenizer(
48 test_text,
49 return_tensors="pt",
50 truncation=True,
51 max_length=512,
52 padding=True
53 ).to(device)
54 with torch.no_grad():
55 outputs = model(**inputs)
56 predictions = outputs.cpu().numpy() if isinstance(outputs, (tuple, list)) else outputs.cpu().numpy()
57 label_scores = {label: float(score) for label, score in zip(checkpoint['label_names'], predictions[0])}
58 print(f"Example {i+1}: '{test_text}'")
59 print("Predictions (all label scores, highest first):")
60 print_sorted_label_scores(label_scores)
61 print("-" * 40)1optimal_thresholds = {'Alternative Modes': 0.28427787391225384, 'Charging Infrastructure': 0.3619448731592626, 'Environmental Benefit': 0.4029443119613918, 'Grid Impact And Energy Mix': 0.29907076386497516, 'Mineral Supply Chain': 0.2987419331439881, 'Policy And Mandates': 0.36899998622725905, 'Purchase Price': 0.3463644004166977}
2for label, score in zip(label_names, predictions[0]):
3 threshold = optimal_thresholds.get(label, 0.5)
4 if score > threshold:
5 print(f"{label}: {score:.3f}")1@misc{electric_vehicles_distilbert_classifier,
2 title={Electric Vehicles Classifier for Climate Change Analysis},
3 author={Sandeep Chowdhary},
4 year={2025},
5 publisher={Hugging Face},
6 journal={Hugging Face Hub},
7 howpublished={\url{https://huggingface.co/echoboi/electric_vehicles-distilbert-classifier}},
8}