finbert-finetuned / utils.py
Boris Ustyugov
model load from hf model repo
5553133
import os
import yaml
from transformers import pipeline
def load_pipeline_from_huggingface():
"""
Load sentiment analysis pipeline from Hugging Face repository.
The repository name is specified by model_path in config.yaml.
Returns:
pipeline: Sentiment analysis pipeline loaded from Hugging Face
"""
# Read config to get model_path
config_path = "config.yaml"
if not os.path.exists(config_path):
raise FileNotFoundError(f"Config file not found: {config_path}")
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
model_path = config.get('model_path')
if not model_path:
raise ValueError("model_path not found in config.yaml")
# Load pipeline from Hugging Face repository
try:
sentiment_pipeline = pipeline(
"sentiment-analysis",
model=model_path,
return_all_scores=True
)
return sentiment_pipeline
except Exception as e:
raise RuntimeError(f"Failed to load pipeline from Hugging Face repository {model_path}: {str(e)}")