import os os.system("apt-get update") os.system("apt-get install -y python3-pip") # Make sure pip is available os.system("pip install transformers") # Restart the kernel here if you have the option (in a notebook setting) import transformers from torch.utils.data import DataLoader import streamlit as st from datasets import load_dataset, Audio from transformers import AutoModelForAudioClassification, AutoFeatureExtractor import torch import os # Install using apt # Load the MInDS-14 dataset dataset = load_dataset("PolyAI/minds14", "en-US", split="train", trust_remote_code=True) # Load pretrained model and feature extractor model = AutoModelForAudioClassification.from_pretrained("facebook/wav2vec2-base") feature_extractor = AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base") # Resample audio to 16kHz dataset = dataset.cast_column("audio", Audio(sampling_rate=16000)) # Preprocessing function def preprocess_function(examples): audio_arrays = [x["array"] for x in examples["audio"]] inputs = feature_extractor( audio_arrays, sampling_rate=16000, padding=True, max_length=100000, truncation=True, ) return inputs dataset = dataset.map(preprocess_function, batched=True) dataset = dataset.rename_column("intent_class", "labels") dataset = dataset.set_format(type="torch", columns=["input_values", "labels"]) # Create DataLoader batch_size = 4 # Adjust as needed dataloader = DataLoader(dataset, batch_size=batch_size) # Set device and move model device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model.to(device) # Training loop (example) num_epochs = 2 # Keep small for testing on Spaces! optimizer = torch.optim.AdamW(model.parameters(), lr=5e-5) for epoch in range(num_epochs): for batch in dataloader: input_values = batch["input_values"].to(device) labels = batch["labels"].to(device) optimizer.zero_grad() outputs = model(input_values, labels=labels) loss = outputs.loss loss.backward() optimizer.step() print(f"Epoch: {epoch+1}, Loss: {loss.item()}") # Streamlit UI st.title("Audio Classification with Minds14") st.write("Training complete!") # You'll want to add more insightful outputs here eventually st.markdown("""
Audio

Resample an audio dataset and get it ready for a model to classify what type of banking issue a speaker is calling about.

Vision

Apply data augmentation to an image dataset and get it ready for a model to diagnose disease in bean plants.

NLP

Tokenize a dataset and get it ready for a model to determine whether a pair of sentences have the same meaning.

Check out Chapter 5 of the Hugging Face course to learn more about other important topics such as loading remote or local datasets, tools for cleaning up a dataset, and creating your own dataset.

""", unsafe_allow_html=True)