Views
No views yet
distilbert-base-uncased) model trained to automatically classify mental health discussion threads, support queries, and social media posts into structured topic categories.distilbert-base-uncased)en)transformers library in Python:1import torch
2import joblib
3from huggingface_hub import hf_hub_download
4from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
6REPO_ID = "emanfatima123/mental-health-topic-classifier"
7
8 1. Load Tokenizer and Model
9tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
10model = AutoModelForSequenceClassification.from_pretrained(REPO_ID)
11model.eval()
12
132. Download and Load Label Encoder
14try:
15 encoder_path = hf_hub_download(repo_id=REPO_ID, filename="label_encoder.pkl")
16 label_encoder = joblib.load(encoder_path)
17except Exception:
18 label_encoder = None
19
203. Perform Inference
21def classify_text(text):
22 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128, padding=True)
23 with torch.no_grad():
24 outputs = model(**inputs)
25 probs = torch.softmax(outputs.logits, dim=-1)
26 pred_id = torch.argmax(probs, dim=-1).item()
27 confidence = probs[0][pred_id].item()
28
29 if label_encoder is not None:
30 label = label_encoder.inverse_transform([pred_id])[0]
31 else:
32 label = model.config.id2label.get(pred_id, f"Class {pred_id}")
33
34 return label, confidence
35
36 Example Prediction
37sample_text = "I feel so overwhelmed with my upcoming university exams and assignments."
38topic, score = classify_text(sample_text)
39
40print(f"Predicted Topic: {topic}")
41print(f"Confidence Score: {score * 100:.2f}%")