Views
No views yet
1from transformers import T5ForConditionalGeneration, T5Tokenizer
2import json
3
4# Load model and tokenizer
5model = T5ForConditionalGeneration.from_pretrained("afsagag/t5-spotify-features")
6tokenizer = T5Tokenizer.from_pretrained("afsagag/t5-spotify-features")
7
8# Example usage
9prompt = "I want energetic dance music with high energy and danceability"
10input_text = f"prompt: {prompt}"
11
12# Tokenize and generate
13input_ids = tokenizer(input_text, return_tensors="pt", max_length=256, truncation=True).input_ids
14outputs = model.generate(
15 input_ids,
16 max_length=256,
17 num_beams=4,
18 early_stopping=True,
19 do_sample=False
20)
21
22# Decode result
23result = tokenizer.decode(outputs[0], skip_special_tokens=True)
24print(result)
25
26# Parse JSON output
27try:
28 spotify_features = json.loads(result)
29 print("Generated Spotify Features:", spotify_features)
30except json.JSONDecodeError:
31 print("Generated text is not valid JSON")1{
2 "danceability": 0.85,
3 "energy": 0.90,
4 "valence": 0.75,
5 "acousticness": 0.15,
6 "instrumentalness": 0.05,
7 "speechiness": 0.08,
8}config.json: Model configurationpytorch_model.bin: Model weightstokenizer.json: Tokenizer vocabularytokenizer_config.json: Tokenizer configurationspecial_tokens_map.json: Special token mappings