Skip to content

A collection of Python scripts demonstrating how to run various AI tasks locally using models from the Hugging Face Hub and the transformers library (along with related libraries like datasets, sentence-transformers, etc.). These examples cover a range of modalities including text, vision, and audio, showcasing different models and pipelines.

Notifications You must be signed in to change notification settings

Sleepparalysis1/NLP-Examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NLP Examples: A Collection of AI Scripts 🤖

NLP Examples

Welcome to the NLP Examples repository! This project features a variety of Python scripts that demonstrate how to run various AI tasks locally. We utilize models from the Hugging Face Hub and the transformers library, along with related libraries like datasets and sentence-transformers. Our examples cover a range of modalities, including text, vision, and audio, showcasing different models and pipelines.

Table of Contents

  1. Features
  2. Installation
  3. Usage
  4. Examples
  5. Contributing
  6. License
  7. Links

Features

  • Text Processing: Utilize state-of-the-art NLP models like BERT for tasks such as text classification and sentiment analysis.
  • Audio Processing: Explore automatic speech recognition (ASR) models to transcribe audio files into text.
  • Vision Tasks: Implement models like DETR for object detection in images.
  • Comprehensive Examples: Each script is self-contained and includes detailed comments to guide you through the code.

Installation

To get started, you'll need to set up your environment. Follow these steps:

  1. Clone the Repository:

    git clone https://github.com/Sleepparalysis1/NLP-Examples.git
    cd NLP-Examples
  2. Create a Virtual Environment (optional but recommended):

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
  3. Install Dependencies:

    Use pip to install the required libraries.

    pip install -r requirements.txt

Usage

Each script in this repository serves a specific purpose. You can run them directly from the command line. For example, to run a text classification script, use:

python text_classification.py --input "Your text here"

Make sure to check the script for additional options.

Examples

Text Classification with BERT

This example shows how to use a BERT model for text classification.

from transformers import BertTokenizer, BertForSequenceClassification
import torch

# Load pre-trained model and tokenizer
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForSequenceClassification.from_pretrained('bert-base-uncased')

# Prepare input
inputs = tokenizer("Hello, world!", return_tensors="pt")

# Get predictions
with torch.no_grad():
    outputs = model(**inputs)
    predictions = torch.argmax(outputs.logits, dim=-1)

print(f"Predicted class: {predictions.item()}")

Automatic Speech Recognition

This example demonstrates how to transcribe audio using an ASR model.

from transformers import Wav2Vec2ForCTC, Wav2Vec2Tokenizer
import torch

# Load pre-trained model and tokenizer
tokenizer = Wav2Vec2Tokenizer.from_pretrained('facebook/wav2vec2-base-960h')
model = Wav2Vec2ForCTC.from_pretrained('facebook/wav2vec2-base-960h')

# Load audio file
audio_input = "path/to/audio.wav"

# Transcribe audio
input_values = tokenizer(audio_input, return_tensors="pt").input_values
with torch.no_grad():
    logits = model(input_values).logits

# Get predicted ids
predicted_ids = torch.argmax(logits, dim=-1)

# Decode the ids to text
transcription = tokenizer.batch_decode(predicted_ids)
print(f"Transcription: {transcription[0]}")

Object Detection with DETR

This example illustrates how to use the DETR model for object detection.

from transformers import DetrImageProcessor, DetrForObjectDetection
import torch
from PIL import Image

# Load pre-trained model and processor
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")

# Load image
image = Image.open("path/to/image.jpg")

# Prepare input
inputs = processor(images=image, return_tensors="pt")

# Get predictions
with torch.no_grad():
    outputs = model(**inputs)

# Process outputs
# (Add code to visualize results)

Contributing

We welcome contributions to this repository! If you have an idea for a new example or improvement, please follow these steps:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes and commit them (git commit -m 'Add new feature').
  4. Push to your branch (git push origin feature/YourFeature).
  5. Open a pull request.

Please ensure your code adheres to the existing style and includes comments for clarity.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Links

For the latest updates and releases, visit the Releases section. Download and execute the files to explore the examples.

You can also check the "Releases" section for additional resources and updates.

About

A collection of Python scripts demonstrating how to run various AI tasks locally using models from the Hugging Face Hub and the transformers library (along with related libraries like datasets, sentence-transformers, etc.). These examples cover a range of modalities including text, vision, and audio, showcasing different models and pipelines.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages