chiennv commited on
Commit
28ab986
·
verified ·
1 Parent(s): 35cf59e

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +21 -2
README.md CHANGED
@@ -53,10 +53,18 @@ def fast_detect_unknown(text: str) -> bool:
53
  ### Option A: Pipeline
54
 
55
  ```python
 
56
  from transformers import pipeline
57
 
58
  model_id = "chiennv/langid-mmbert-small-8gpu"
59
- clf = pipeline("text-classification", model=model_id, tokenizer=model_id, top_k=1)
 
 
 
 
 
 
 
60
 
61
  text = "Bonjour tout le monde"
62
  if fast_detect_unknown(text):
@@ -74,7 +82,11 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer
74
 
75
  model_id = "chiennv/langid-mmbert-small-8gpu"
76
  tokenizer = AutoTokenizer.from_pretrained(model_id)
77
- model = AutoModelForSequenceClassification.from_pretrained(model_id)
 
 
 
 
78
  model.eval()
79
 
80
  text = "Bonjour tout le monde"
@@ -82,6 +94,7 @@ if fast_detect_unknown(text):
82
  print({"label": "UNKNOWN", "score": 1.0})
83
  else:
84
  inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
 
85
  with torch.no_grad():
86
  logits = model(**inputs).logits
87
  probs = torch.softmax(logits, dim=-1).squeeze(0)
@@ -96,3 +109,9 @@ else:
96
  ```bash
97
  python infer.py
98
  ```
 
 
 
 
 
 
 
53
  ### Option A: Pipeline
54
 
55
  ```python
56
+ import torch
57
  from transformers import pipeline
58
 
59
  model_id = "chiennv/langid-mmbert-small-8gpu"
60
+ device = 0 if torch.cuda.is_available() else -1
61
+ clf = pipeline(
62
+ "text-classification",
63
+ model=model_id,
64
+ tokenizer=model_id,
65
+ top_k=1,
66
+ device=device, # GPU id (0,1,...) or -1 for CPU
67
+ )
68
 
69
  text = "Bonjour tout le monde"
70
  if fast_detect_unknown(text):
 
82
 
83
  model_id = "chiennv/langid-mmbert-small-8gpu"
84
  tokenizer = AutoTokenizer.from_pretrained(model_id)
85
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
86
+
87
+ # Use FP16 on GPU for faster inference and lower memory.
88
+ dtype = torch.float16 if device.type == "cuda" else torch.float32
89
+ model = AutoModelForSequenceClassification.from_pretrained(model_id, torch_dtype=dtype).to(device)
90
  model.eval()
91
 
92
  text = "Bonjour tout le monde"
 
94
  print({"label": "UNKNOWN", "score": 1.0})
95
  else:
96
  inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
97
+ inputs = {k: v.to(device) for k, v in inputs.items()}
98
  with torch.no_grad():
99
  logits = model(**inputs).logits
100
  probs = torch.softmax(logits, dim=-1).squeeze(0)
 
109
  ```bash
110
  python infer.py
111
  ```
112
+
113
+ ## GPU Notes
114
+
115
+ - Check CUDA availability:
116
+ - `python -c "import torch; print(torch.cuda.is_available(), torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'no-gpu')"`
117
+ - The AutoModel example above automatically uses GPU + FP16 when CUDA is available.