Instructions to use botezx/flagtldr with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Grok
How to use botezx/flagtldr with Grok:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
File size: 2,022 Bytes
043267b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """Embedding + trainable head: the "few labelled examples" path that trains in seconds.
Encode examples + label rubrics (as extra pseudo-examples) with a multilingual sentence
encoder, fit a logistic-regression head, predict calibrated-ish probabilities.
Requires the `embed` extra: uv sync --extra embed
Default model is multilingual and small; swap via --embed-model.
"""
from __future__ import annotations
import hashlib
import json
from ..spec import LabelSet, Post, Result
DEFAULT_MODEL = "sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2"
class EmbedBackend:
name = "embed"
def __init__(self, model: str = DEFAULT_MODEL):
from sentence_transformers import SentenceTransformer # lazy: heavy import
self.encoder = SentenceTransformer(model)
self._heads: dict[str, object] = {}
def _key(self, ls: LabelSet) -> str:
return hashlib.sha1(json.dumps(ls.model_dump(), sort_keys=True).encode()).hexdigest()
def _head(self, ls: LabelSet):
k = self._key(ls)
if k not in self._heads:
from sklearn.linear_model import LogisticRegression
texts = [e.text for e in ls.examples] + [f"{l.key}: {l.rubric}" for l in ls.labels]
ys = [e.label for e in ls.examples] + [l.key for l in ls.labels]
X = self.encoder.encode(texts, normalize_embeddings=True)
self._heads[k] = LogisticRegression(max_iter=1000, C=4.0).fit(X, ys)
return self._heads[k]
async def classify(self, ls: LabelSet, posts: list[Post]) -> dict[str, Result]:
head = self._head(ls)
X = self.encoder.encode([p.text for p in posts], normalize_embeddings=True)
out: dict[str, Result] = {}
for p, row in zip(posts, head.predict_proba(X)):
probs = {str(c): float(v) for c, v in zip(head.classes_, row)}
best = max(probs, key=probs.get)
out[p.id] = Result(label=best, probabilities=probs, confidence=probs[best], backend=self.name)
return out
|