ML Projects for Blogs
Collection
Collection of ML projects that I have created over time. β’ 20 items β’ Updated β’ 2
How to use kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-generation", model="kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification")
messages = [
{"role": "user", "content": "Who are you?"},
]
pipe(messages) # Load model directly
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained("kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification")
model = AutoModelForCausalLM.from_pretrained("kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification")
messages = [
{"role": "user", "content": "Who are you?"},
]
inputs = tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
tokenize=True,
return_dict=True,
return_tensors="pt",
).to(model.device)
outputs = model.generate(**inputs, max_new_tokens=40)
print(tokenizer.decode(outputs[0][inputs["input_ids"].shape[-1]:]))How to use kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification with vLLM:
# Install vLLM from pip:
pip install vllm
# Start the vLLM server:
vllm serve "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification"
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:8000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker model run hf.co/kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification
How to use kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification with SGLang:
# Install SGLang from pip:
pip install sglang
# Start the SGLang server:
python3 -m sglang.launch_server \
--model-path "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'docker run --gpus all \
--shm-size 32g \
-p 30000:30000 \
-v ~/.cache/huggingface:/root/.cache/huggingface \
--env "HF_TOKEN=<secret>" \
--ipc=host \
lmsysorg/sglang:latest \
python3 -m sglang.launch_server \
--model-path "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification" \
--host 0.0.0.0 \
--port 30000
# Call the server using curl (OpenAI-compatible API):
curl -X POST "http://localhost:30000/v1/chat/completions" \
-H "Content-Type: application/json" \
--data '{
"model": "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification",
"messages": [
{
"role": "user",
"content": "What is the capital of France?"
}
]
}'How to use kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification with Docker Model Runner:
docker model run hf.co/kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification
This model is a fine-tuned version of meta-llama/Meta-Llama-3.1-8B-Instruct on an suchintikasarkar/sentiment-analysis-for-mental-health dataset.
Get started with the new Llama models and customize Llama-3.1-8B-It to predict various mental health disorders from the text by following the Fine-Tuning Llama 3.1 for Text Classification tutorial.
from transformers import AutoTokenizer,AutoModelForCausalLM,pipeline
import torch
model_id = "kingabzpro/Llama-3.1-8B-Instruct-Mental-Health-Classification"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
return_dict=True,
low_cpu_mem_usage=True,
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=True,
)
text = "I'm trapped in a storm of emotions that I can't control, and it feels like no one understands the chaos inside me"
prompt = f"""Classify the text into Normal, Depression, Anxiety, Bipolar, and return the answer as the corresponding mental health disorder label.
text: {text}
label: """.strip()
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
torch_dtype=torch.float16,
device_map="auto",
)
outputs = pipe(prompt, max_new_tokens=2, do_sample=True, temperature=0.1)
print(outputs[0]["generated_text"].split("label: ")[-1].strip())
# Depression
100%|ββββββββββ| 300/300 [03:24<00:00, 1.47it/s]
Accuracy: 0.913
Accuracy for label Normal: 0.972
Accuracy for label Depression: 0.913
Accuracy for label Anxiety: 0.667
Accuracy for label Bipolar: 0.800
Classification Report:
precision recall f1-score support
Normal 0.92 0.97 0.95 143
Depression 0.93 0.91 0.92 115
Anxiety 0.75 0.67 0.71 27
Bipolar 1.00 0.80 0.89 15
accuracy 0.91 300
macro avg 0.90 0.84 0.87 300
weighted avg 0.91 0.91 0.91 300
Confusion Matrix:
[[139 3 1 0]
[ 5 105 5 0]
[ 6 3 18 0]
[ 1 2 0 12]]