#!/usr/bin/env python # -*- coding: utf-8 -*- """ pipeline_utils.py Utility functions for initializing Hugging Face pipelines and verifying authentication tokens. """ import requests from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline import os def initialize_pipeline(model_name: str, token: str): """ Initialize a text classification pipeline. Args: model_name (str): Name of the Hugging Face model. token (str): Hugging Face access token. Returns: transformers.Pipeline: Text classification pipeline instance. """ tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=token) model = AutoModelForSequenceClassification.from_pretrained(model_name, use_auth_token=token) return pipeline("text-classification", model=model, tokenizer=tokenizer) def verify_token(token: str) -> bool: """ Verifies the Hugging Face token by making a request to the user endpoint. Args: token (str): Hugging Face access token. Returns: bool: True if token is valid, False otherwise. """ try: response = requests.get("https://huggingface.co/api/whoami-v2", headers={"Authorization": f"Bearer {token}"}) response.raise_for_status() return True except requests.RequestException: return False