Building your bot with hugging face models

luckyStars
2 min readSep 17, 2024

--

  1. What’s huggingface.com

Hugging Face stands out for its open-source platform, which includes 120,000 pre-trained models and collaborations with major AI players like Google Cloud. In contrast, OpenAI leads with the most popular language models like GPT-4, while Anthropic focuses on safety in AI development. Cohere is known for its enterprise NLP offerings. Hugging Face’s user base includes over 10,000 companies, positioning it as a central hub for AI model development and deployment.

2. Interact with hugging face.model

pip install transformers
pip install torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch

# Load pre-trained model and tokenizer from Hugging Face
model_name = "nlptown/bert-base-multilingual-uncased-sentiment" # Example model for sentiment analysis
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

# Text to classify
text = "I love using Hugging Face transformers!"

# Tokenize the input text
inputs = tokenizer(text, return_tensors="pt")

# Run the text through the model and get the output
with torch.no_grad():
outputs = model(**inputs)

# Get the predicted class (index of the highest score)
predictions = torch.argmax(outputs.logits, dim=-1)

# Output the result
print(f"Predicted sentiment class: {predictions.item()}")

--

--