Ghostdevol commited on
Commit
a7bee23
·
1 Parent(s): 14c0255

Add application file

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import GPT2LMHeadModel, GPT2Tokenizer
4
+ import librosa
5
+ import numpy as np
6
+
7
+ # -------- Load Model (small) --------
8
+ MODEL_NAME = "distilgpt2"
9
+ tokenizer = GPT2Tokenizer.from_pretrained(MODEL_NAME)
10
+ model = GPT2LMHeadModel.from_pretrained(MODEL_NAME)
11
+
12
+ # -------- Session State --------
13
+ class SessionState:
14
+ def __init__(self):
15
+ self.tempo = None
16
+ self.energy = None
17
+ self.lyrics = []
18
+
19
+ state = SessionState()
20
+
21
+ # -------- Beat Analysis (lightweight) --------
22
+ def analyze_beat(audio):
23
+ y, sr = librosa.load(audio, sr=16000, mono=True)
24
+ tempo, _ = librosa.beat.beat_track(y=y, sr=sr)
25
+ energy = float(np.mean(np.abs(y)))
26
+ return int(tempo), round(energy, 3)
27
+
28
+ # -------- Generate Lyrics --------
29
+ def generate_lines(mood, lines=4, regenerate=False):
30
+ global state
31
+
32
+ if state.tempo is None:
33
+ return "Upload a beat first."
34
+
35
+ # Remove last 2 lines if regenerating
36
+ if regenerate and len(state.lyrics) >= 2:
37
+ state.lyrics = state.lyrics[:-2]
38
+
39
+ context = "\n".join(state.lyrics)
40
+
41
+ prompt = (
42
+ f"{context}\n"
43
+ f"Rap lyrics for a {mood} beat at {state.tempo} BPM "
44
+ f"with energy {state.energy}:\n"
45
+ )
46
+
47
+ inputs = tokenizer.encode(prompt, return_tensors="pt")
48
+
49
+ output = model.generate(
50
+ inputs,
51
+ max_length=inputs.shape[1] + lines * 12,
52
+ do_sample=True,
53
+ temperature=0.9,
54
+ top_p=0.95,
55
+ pad_token_id=tokenizer.eos_token_id
56
+ )
57
+
58
+ text = tokenizer.decode(output[0], skip_special_tokens=True)
59
+ new_part = text.replace(prompt, "").strip().split("\n")
60
+
61
+ clean_lines = [l.strip() for l in new_part if l.strip()][:lines]
62
+
63
+ state.lyrics.extend(clean_lines)
64
+
65
+ return "\n".join(state.lyrics)
66
+
67
+ # -------- Upload Handler --------
68
+ def handle_upload(audio):
69
+ global state
70
+ tempo, energy = analyze_beat(audio)
71
+ state.tempo = tempo
72
+ state.energy = energy
73
+ state.lyrics = []
74
+ return f"Beat analyzed: {tempo} BPM | Energy: {energy}"
75
+
76
+ # -------- UI --------
77
+ with gr.Blocks() as demo:
78
+ gr.Markdown("## 🎵 Beat-to-Lyrics Generator (Free CPU Optimized)")
79
+
80
+ audio_input = gr.Audio(type="filepath")
81
+ upload_btn = gr.Button("Analyze Beat")
82
+ beat_info = gr.Textbox(label="Beat Info")
83
+
84
+ mood = gr.Dropdown(
85
+ ["Chill", "Hype", "Trap", "Lo-fi", "Boom-bap"],
86
+ value="Chill",
87
+ label="Mood"
88
+ )
89
+
90
+ lyrics_output = gr.Textbox(lines=20, label="Lyrics")
91
+
92
+ generate_btn = gr.Button("Generate Lines")
93
+ regenerate_btn = gr.Button("Regenerate Last 2 Lines")
94
+
95
+ upload_btn.click(handle_upload, audio_input, beat_info)
96
+ generate_btn.click(lambda m: generate_lines(m, 6, False), mood, lyrics_output)
97
+ regenerate_btn.click(lambda m: generate_lines(m, 4, True), mood, lyrics_output)
98
+
99
+ demo.launch()