Views
No views yet
"danceability": 0.9, "energy": 0.9, "valence": 0.9.1from transformers import T5ForConditionalGeneration, T5Tokenizer
2import torch
3
4# Load model and tokenizer
5model = T5ForConditionalGeneration.from_pretrained("afsagag/t5-spotify-features-generator")
6tokenizer = T5Tokenizer.from_pretrained("afsagag/t5-spotify-features-generator")
7
8def generate_spotify_features(prompt, model, tokenizer):
9 input_text = f"prompt: {prompt}"
10 input_ids = tokenizer(input_text, return_tensors="pt", max_length=256, truncation=True).input_ids
11
12 with torch.no_grad():
13 outputs = model.generate(
14 input_ids,
15 max_length=256,
16 num_beams=4,
17 early_stopping=True,
18 do_sample=False,
19 pad_token_id=tokenizer.pad_token_id,
20 eos_token_id=tokenizer.eos_token_id
21 )
22
23 result = tokenizer.decode(outputs[0], skip_special_tokens=True)
24 return result
25
26# Example usage
27prompt = "I need energetic dance music for a party"
28features = generate_spotify_features(prompt, model, tokenizer)
29print(features) # Output: "danceability": 0.9, "energy": 0.9, "valence": 0.9| Prompt | Generated Features |
|---|---|
| "I need energetic dance music for a party" | "danceability": 0.9, "energy": 0.9, "valence": 0.9 |
| "Play calm acoustic songs for studying" | "acousticness": 0.8, "energy": 0.2, "valence": 0.2 |
| "Upbeat music for working out" | "danceability": 0.7, "energy": 0.8, "valence": 0.7 |
| "Relaxing instrumental background music" | "acousticness": 0.3, "energy": 0.2, "instrumentalness": 0.8, "valence": 0.2 |
| "Happy pop music for driving" | "danceability": 0.8, "energy": 0.8, "valence": 0.8 |
"prompt: {natural_language_description}"1@misc{t5-spotify-features-generator,
2 author = {afsagag},
3 title = {T5 Spotify Features Generator: Fine-tuned T5 for Music Feature Prediction from Natural Language},
4 year = {2025},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/afsagag/t5-spotify-features-generator}}
7}