This application provides a flexible interface for loading and performing inference with any Hugging Face model. It supports various types of models including text, image, and audio models.
- Clone this repository
- Install the required dependencies:
pip install -r requirements.txtfrom model_inference import ModelInference
# Initialize a model for a specific task
model = ModelInference(
model_name="distilbert-base-uncased-finetuned-sst-2-english",
task="sentiment-analysis"
)
# Perform inference
result = model.infer("I love this application!")
print(result)You can view all supported tasks using:
ModelInference.list_supported_tasks()- Text Classification:
classifier = ModelInference("distilbert-base-uncased-finetuned-sst-2-english",
task="sentiment-analysis")
result = classifier.infer("I love using Hugging Face models!")- Text Generation:
generator = ModelInference("gpt2", task="text-generation")
result = generator.infer("Once upon a time",
max_length=50,
num_return_sequences=1)- Image Classification:
from PIL import Image
classifier = ModelInference("google/vit-base-patch16-224",
task="image-classification")
image = Image.open("path/to/image.jpg")
result = classifier.infer(image)- Automatic device selection (CPU/GPU)
- Support for multiple model types
- Error handling and logging
- Flexible input handling
- Easy-to-use interface
- Python 3.7+
- PyTorch
- Transformers
- Other dependencies listed in requirements.txt
Make sure you have enough system resources to load the models. Some large models may require significant RAM or GPU memory.