cnmoro / TangledLlama33m-Reranker-EnPt

huggingface.co
Total runs: 20
24-hour runs: 0
7-day runs: 5
30-day runs: 2
Model's Last Updated: April 02 2025
text-ranking

Introduction of TangledLlama33m-Reranker-EnPt

Model Details of TangledLlama33m-Reranker-EnPt

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

model_id = "cnmoro/TangledLlama33m-Reranker-EnPt"
model = AutoModelForSequenceClassification.from_pretrained(
    model_id,
    num_labels=2
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)

template = "Query: {query}\nSentence: {document}"

def rank(query, documents, normalize_scores=True):
    texts = [template.format(query=query, document=document) for document in documents]

    inputs = tokenizer(
        texts,
        add_special_tokens=True,
        max_length=32768,
        truncation=True,
        padding=True,
        return_tensors="pt",
    )

    input_ids = inputs["input_ids"].to(device)
    attention_mask = inputs["attention_mask"].to(device)

    model.eval()
    with torch.no_grad():
        outputs = model(input_ids, attention_mask=attention_mask)
        logits = outputs.logits
        probabilities = torch.softmax(logits, dim=1)

        # Get the predicted classes and confidence scores
        predicted_classes = torch.argmax(probabilities, dim=1).tolist()
        confidences = probabilities.max(dim=1).values.tolist()

    # Construct the results
    results = [
        {"prediction": pred, "confidence": conf}
        for pred, conf in zip(predicted_classes, confidences)
    ]

    final_results = []
    for document, result in zip(documents, results):
        # If the prediction is 0, then get the score as 1 - confidence
        if result['prediction'] == 0:
            result['confidence'] = 1 - result['confidence']
        final_results.append((document, result['confidence']))
    
    # Sort by the confidence score, descending
    sorted_results = sorted(final_results, key=lambda x: x[1], reverse=True)

    if normalize_scores:
        total_score = sum([result[1] for result in sorted_results])
        sorted_results = [(result[0], result[1] / total_score) for result in sorted_results]

    return sorted_results

# Sample - 1
query = "O que é o Pantanal?"
documents = [
    "É um dos ecossistemas mais ricos em biodiversidade do mundo, abrigando uma grande variedade de espécies animais e vegetais.",
    "Sua beleza natural, com rios e lagos interligados, atrai turistas de todo o mundo.",
    "O Pantanal sofre com impactos ambientais, como a exploração mineral e o desmatamento.",
    "O Pantanal é uma extensa planície alagável localizada na América do Sul, principalmente no Brasil, mas também em partes da Bolívia e Paraguai.",
    "É um local com importância histórica e cultural para as populações locais.",
    "O Pantanal é um importante habitat para diversas espécies de animais, inclusive aves migratórias."
]
rank(query, documents)
# [('O Pantanal é um importante habitat para diversas espécies de animais, inclusive aves migratórias.',
#   0.39881916829816605),
#  ('O Pantanal é uma extensa planície alagável localizada na América do Sul, principalmente no Brasil, mas também em partes da Bolívia e Paraguai.',
#   0.37527216160662785),
#  ('O Pantanal sofre com impactos ambientais, como a exploração mineral e o desmatamento.',
#   0.1491597234932686),
#  ('É um local com importância histórica e cultural para as populações locais.',
#   0.03648153259324298),
#  ('Sua beleza natural, com rios e lagos interligados, atrai turistas de todo o mundo.',
#   0.020711667666201344),
#  ('É um dos ecossistemas mais ricos em biodiversidade do mundo, abrigando uma grande variedade de espécies animais e vegetais.',
#   0.019555746342493203)]

# Sample - 2
query = "What is the speed of light?"
documents = [
    "Isaac Newton's laws of motion and gravity laid the groundwork for classical mechanics.",
    "The theory of relativity, proposed by Albert Einstein, has revolutionized our understanding of space, time, and gravity.",
    "The Earth orbits the Sun at an average distance of about 93 million miles, taking roughly 365.25 days to complete one revolution.",
    "The speed of light in a vacuum is approximately 299,792 kilometers per second (km/s), or about 186,282 miles per second.",
    "Light can be described as both a wave and a particle, a concept known as wave-particle duality."
]
rank(query, documents)
# [('The speed of light in a vacuum is approximately 299,792 kilometers per second (km/s), or about 186,282 miles per second.',
#   0.23310426435074763),
#  ('The Earth orbits the Sun at an average distance of about 93 million miles, taking roughly 365.25 days to complete one revolution.',
#   0.22329693015953184),
#  ("Isaac Newton's laws of motion and gravity laid the groundwork for classical mechanics.",
#   0.20374707681001922),
#  ('The theory of relativity, proposed by Albert Einstein, has revolutionized our understanding of space, time, and gravity.',
#   0.20284618746671068),
#  ('Light can be described as both a wave and a particle, a concept known as wave-particle duality.',
#   0.13700554121299063)]

Runs of cnmoro TangledLlama33m-Reranker-EnPt on huggingface.co

20
Total runs
0
24-hour runs
6
3-day runs
5
7-day runs
2
30-day runs

More Information About TangledLlama33m-Reranker-EnPt huggingface.co Model

More TangledLlama33m-Reranker-EnPt license Visit here:

https://choosealicense.com/licenses/mit

TangledLlama33m-Reranker-EnPt huggingface.co

TangledLlama33m-Reranker-EnPt huggingface.co is an AI model on huggingface.co that provides TangledLlama33m-Reranker-EnPt's model effect (), which can be used instantly with this cnmoro TangledLlama33m-Reranker-EnPt model. huggingface.co supports a free trial of the TangledLlama33m-Reranker-EnPt model, and also provides paid use of the TangledLlama33m-Reranker-EnPt. Support call TangledLlama33m-Reranker-EnPt model through api, including Node.js, Python, http.

TangledLlama33m-Reranker-EnPt huggingface.co Url

https://huggingface.co/cnmoro/TangledLlama33m-Reranker-EnPt

cnmoro TangledLlama33m-Reranker-EnPt online free

TangledLlama33m-Reranker-EnPt huggingface.co is an online trial and call api platform, which integrates TangledLlama33m-Reranker-EnPt's modeling effects, including api services, and provides a free online trial of TangledLlama33m-Reranker-EnPt, you can try TangledLlama33m-Reranker-EnPt online for free by clicking the link below.

cnmoro TangledLlama33m-Reranker-EnPt online free url in huggingface.co:

https://huggingface.co/cnmoro/TangledLlama33m-Reranker-EnPt

TangledLlama33m-Reranker-EnPt install

TangledLlama33m-Reranker-EnPt is an open source model from GitHub that offers a free installation service, and any user can find TangledLlama33m-Reranker-EnPt on GitHub to install. At the same time, huggingface.co provides the effect of TangledLlama33m-Reranker-EnPt install, users can directly use TangledLlama33m-Reranker-EnPt installed effect in huggingface.co for debugging and trial. It also supports api for free installation.

TangledLlama33m-Reranker-EnPt install url in huggingface.co:

https://huggingface.co/cnmoro/TangledLlama33m-Reranker-EnPt

Url of TangledLlama33m-Reranker-EnPt

TangledLlama33m-Reranker-EnPt huggingface.co Url

Provider of TangledLlama33m-Reranker-EnPt huggingface.co

cnmoro
ORGANIZATIONS

Other API from cnmoro

huggingface.co

Total runs: 41
Run Growth: 10
Growth Rate: 24.39%
Updated:April 09 2026
huggingface.co

Total runs: 4
Run Growth: -3
Growth Rate: -75.00%
Updated:January 14 2025