Ubuntu commited on
Commit
2de8849
·
1 Parent(s): a62f55f
Files changed (5) hide show
  1. app.py +0 -87
  2. configs/config.yaml +9 -10
  3. configs/gpt2_spotify.yaml +0 -26
  4. configs/spotify.yaml +0 -27
  5. run.sh +22 -69
app.py DELETED
@@ -1,87 +0,0 @@
1
- import gradio as gr
2
- import torch
3
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
4
- from pathlib import Path
5
-
6
- # Load model - check both local and deployed paths
7
- MODEL_PATHS = ["outputs/final_model", "model", "/tmp/model"]
8
-
9
- tokenizer = None
10
- model = None
11
- MODEL_LOADED = False
12
-
13
- for model_path in MODEL_PATHS:
14
- if Path(model_path).exists():
15
- try:
16
- tokenizer = AutoTokenizer.from_pretrained(model_path)
17
- model = AutoModelForSequenceClassification.from_pretrained(model_path)
18
- model.eval()
19
- MODEL_LOADED = True
20
- print(f"✓ Model loaded from: {model_path}")
21
- break
22
- except Exception as e:
23
- print(f"Failed to load from {model_path}: {e}")
24
-
25
- if not MODEL_LOADED:
26
- print("⚠ Model not found. Using demo mode.")
27
-
28
- def predict_genre(track_name):
29
- """Predict genre for a track name"""
30
- if not MODEL_LOADED:
31
- return "Model not found. Please train first."
32
-
33
- if not track_name:
34
- return "Please enter a track name"
35
-
36
- # Tokenize
37
- inputs = tokenizer(track_name, return_tensors='pt', padding=True, truncation=True, max_length=256)
38
-
39
- # Predict
40
- with torch.no_grad():
41
- outputs = model(**inputs)
42
- probs = torch.softmax(outputs.logits, dim=-1)
43
- pred_id = torch.argmax(probs, dim=-1).item()
44
- confidence = probs[0, pred_id].item()
45
-
46
- # Get label
47
- pred_label = model.config.id2label.get(pred_id, f"Class_{pred_id}")
48
-
49
- return f"**Genre:** {pred_label}\n\n**Confidence:** {confidence:.2%}"
50
-
51
- # Create Gradio interface
52
- with gr.Blocks(title="Spotify Genre Classifier", theme=gr.themes.Soft()) as demo:
53
- gr.Markdown("# 🎵 Spotify Genre Classifier")
54
- gr.Markdown("Enter a song track name to predict its genre using a fine-tuned GPT-2 model.")
55
-
56
- with gr.Row():
57
- with gr.Column():
58
- track_input = gr.Textbox(
59
- label="Track Name",
60
- placeholder="e.g., Bohemian Rhapsody",
61
- lines=1
62
- )
63
- predict_btn = gr.Button("🔮 Predict Genre", variant="primary")
64
-
65
- with gr.Column():
66
- output = gr.Textbox(label="Prediction")
67
-
68
- # Examples
69
- gr.Examples(
70
- examples=[
71
- "Bohemian Rhapsody",
72
- "Shape of You",
73
- "Old Town Road",
74
- "Blinding Lights",
75
- "Bad Guy",
76
- "Stairway to Heaven",
77
- "Smells Like Teen Spirit",
78
- "Billie Jean",
79
- ],
80
- inputs=track_input
81
- )
82
-
83
- predict_btn.click(fn=predict_genre, inputs=track_input, outputs=output)
84
- track_input.submit(fn=predict_genre, inputs=track_input, outputs=output)
85
-
86
- if __name__ == "__main__":
87
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
configs/config.yaml CHANGED
@@ -1,19 +1,18 @@
1
- # Default Configuration
2
 
3
  model:
4
- name: "bert-base-uncased"
5
 
6
  dataset:
7
- name: "spotify"
8
- config: null
9
- text_column: "text"
10
- label_column: "label"
11
- max_length: 512
12
 
13
  training:
14
- epochs: 3
15
- batch_size: 32
16
- learning_rate: 2e-5
17
  weight_decay: 0.01
18
  warmup_ratio: 0.1
19
 
 
1
+ # GPT-2 on Spotify Dataset
2
 
3
  model:
4
+ name: "gpt2"
5
 
6
  dataset:
7
+ name: "maharshipandya/spotify-tracks-dataset"
8
+ text_column: "track_name"
9
+ label_column: "track_genre"
10
+ max_length: 256
 
11
 
12
  training:
13
+ epochs: 5
14
+ batch_size: 8
15
+ learning_rate: 5e-5
16
  weight_decay: 0.01
17
  warmup_ratio: 0.1
18
 
configs/gpt2_spotify.yaml DELETED
@@ -1,26 +0,0 @@
1
- # GPT-2 on Spotify Dataset
2
-
3
- model:
4
- name: "gpt2"
5
-
6
- dataset:
7
- name: "maharshipandya/spotify-tracks-dataset"
8
- text_column: "track_name"
9
- label_column: "track_genre"
10
- max_length: 256
11
-
12
- training:
13
- epochs: 5
14
- batch_size: 8
15
- learning_rate: 5e-5
16
-
17
- hardware:
18
- mixed_precision: "fp16"
19
-
20
- output:
21
- dir: "./outputs"
22
-
23
- evaluation:
24
- metrics:
25
- - "accuracy"
26
- - "f1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
configs/spotify.yaml DELETED
@@ -1,27 +0,0 @@
1
- # Spotify Dataset Configuration
2
-
3
- model:
4
- name: "bert-base-uncased"
5
-
6
- dataset:
7
- name: "maharshipandya/spotify-tracks-dataset"
8
- text_column: "track_name"
9
- label_column: "track_genre"
10
- max_length: 256
11
-
12
- training:
13
- epochs: 5
14
- batch_size: 16
15
- learning_rate: 3e-5
16
-
17
- hardware:
18
- mixed_precision: "fp16"
19
-
20
- output:
21
- dir: "./outputs"
22
- save_strategy: "epoch"
23
-
24
- evaluation:
25
- metrics:
26
- - "accuracy"
27
- - "f1"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
run.sh CHANGED
@@ -1,61 +1,33 @@
1
  #!/bin/bash
2
- #
3
- # HF Training Pipeline - Run Script
4
- # Usage: ./run.sh [config_name]
5
- # ./run.sh test [model_path]
6
- #
7
- # Examples:
8
- # ./run.sh # Run with default config
9
- # ./run.sh spotify # Run with Spotify config
10
- # ./run.sh gpt2_spotify # Run with GPT-2 model
11
- # ./run.sh test # Test trained model
12
- #
13
 
14
  set -e
15
 
16
- # Colors
17
- RED='\033[0;31m'
18
  GREEN='\033[0;32m'
19
  YELLOW='\033[1;33m'
20
  BLUE='\033[0;34m'
21
  NC='\033[0m'
22
 
23
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
24
- cd "$SCRIPT_DIR"
25
 
26
- # Handle test command
27
- if [ "$1" = "test" ]; then
28
- MODEL_PATH="${2:-outputs/final_model}"
29
- echo -e "${BLUE}========================================${NC}"
30
- echo -e "${BLUE} Testing Model${NC}"
31
- echo -e "${BLUE} Path: ${YELLOW}${MODEL_PATH}${NC}"
32
- echo -e "${BLUE}========================================${NC}"
33
- echo ""
34
- python3 test_model.py "$MODEL_PATH"
35
- exit 0
36
  fi
37
 
38
- CONFIG_NAME="${1:-spotify}"
 
 
39
 
40
- echo -e "${BLUE}========================================${NC}"
41
- echo -e "${BLUE} Hugging Face Training Pipeline${NC}"
42
- echo -e "${BLUE}========================================${NC}"
43
- echo ""
44
 
45
- # Activate venv if exists
46
- if [ -f ".venv/bin/activate" ]; then
47
- echo -e "${GREEN}✓ Activating virtual environment...${NC}"
48
- source .venv/bin/activate
49
- fi
50
-
51
- # Check Python
52
- if ! command -v python3 &> /dev/null; then
53
- echo -e "${RED}❌ Python3 not found${NC}"
54
- exit 1
55
  fi
56
 
57
- echo -e "${GREEN}✓ Python found: $(python3 --version)${NC}"
58
-
59
  # Check GPU
60
  echo ""
61
  echo -e "${YELLOW}Checking GPU availability...${NC}"
@@ -69,38 +41,19 @@ else:
69
  print('⚠ No CUDA available - training will use CPU (slower)')
70
  "
71
 
72
- # Check .env
73
- echo ""
74
- if [ -f ".env" ]; then
75
- echo -e "${GREEN}✓ .env file found${NC}"
76
- else
77
- echo -e "${YELLOW}⚠ .env not found. Creating...${NC}"
78
- cp .env.example .env
79
- fi
80
-
81
- # Run training
82
- echo ""
83
  echo -e "${BLUE}========================================${NC}"
84
- echo -e "${BLUE} Starting Training${NC}"
85
- echo -e "${BLUE} Config: ${YELLOW}${CONFIG_NAME}${NC}"
86
  echo -e "${BLUE}========================================${NC}"
87
- echo ""
88
 
89
- python3 src/training_pipeline.py "$CONFIG_NAME"
90
 
91
  echo ""
92
- echo -e "${GREEN}========================================${NC}"
93
- echo -e "${GREEN} Training Complete!${NC}"
94
- echo -e "${GREEN}========================================${NC}"
95
- echo ""
96
  echo -e "Model saved to: ${YELLOW}outputs/final_model${NC}"
97
  echo ""
98
 
99
- # Ask to run test
100
- echo -e "${YELLOW}Would you like to test the model? (y/n)${NC}"
101
- read -r -n 1 -s response
102
- echo ""
103
- if [[ $response =~ ^[Yy]$ ]]; then
104
- echo ""
105
- python3 test_model.py outputs/final_model
106
- fi
 
1
  #!/bin/bash
2
+ # Usage: ./run.sh
 
 
 
 
 
 
 
 
 
 
3
 
4
  set -e
5
 
 
 
6
  GREEN='\033[0;32m'
7
  YELLOW='\033[1;33m'
8
  BLUE='\033[0;34m'
9
  NC='\033[0m'
10
 
11
+ cd "$(dirname "${BASH_SOURCE[0]}")"
 
12
 
13
+ # Install uv if not exists
14
+ if ! command -v uv &> /dev/null; then
15
+ echo -e "${YELLOW}Installing uv...${NC}"
16
+ curl -LsSf https://astral.sh/uv/install.sh | sh
 
 
 
 
 
 
17
  fi
18
 
19
+ # Sync dependencies (creates .venv if needed)
20
+ echo -e "${GREEN}✓ Syncing dependencies with uv...${NC}"
21
+ uv sync
22
 
23
+ # Activate venv
24
+ source .venv/bin/activate
 
 
25
 
26
+ # Check .env
27
+ if [ ! -f ".env" ]; then
28
+ cp .env.example .env
 
 
 
 
 
 
 
29
  fi
30
 
 
 
31
  # Check GPU
32
  echo ""
33
  echo -e "${YELLOW}Checking GPU availability...${NC}"
 
41
  print('⚠ No CUDA available - training will use CPU (slower)')
42
  "
43
 
 
 
 
 
 
 
 
 
 
 
 
44
  echo -e "${BLUE}========================================${NC}"
45
+ echo -e "${BLUE} Training GPT-2 on Spotify Dataset${NC}"
 
46
  echo -e "${BLUE}========================================${NC}"
 
47
 
48
+ python3 src/training_pipeline.py config
49
 
50
  echo ""
51
+ echo -e "${GREEN}✓ Training Complete!${NC}"
 
 
 
52
  echo -e "Model saved to: ${YELLOW}outputs/final_model${NC}"
53
  echo ""
54
 
55
+ echo -e "${BLUE}========================================${NC}"
56
+ echo -e "${BLUE} Testing Model${NC}"
57
+ echo -e "${BLUE}========================================${NC}"
58
+
59
+ python3 test_model.py outputs/final_model