Spaces:
Runtime error
Runtime error
Krish Shah-Nathwani commited on
Commit ·
5a36461
1
Parent(s): 2cae428
better prompting for chord naming
Browse files
app.py
CHANGED
|
@@ -6,45 +6,54 @@ generator = pipeline("text2text-generation", model="google/flan-t5-small")
|
|
| 6 |
|
| 7 |
def chord_bot(prompt: str) -> str:
|
| 8 |
"""
|
| 9 |
-
Use FLAN-T5 with few-shot prompting to
|
| 10 |
"""
|
| 11 |
chord_prompt = f"""
|
| 12 |
-
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
Output: This is a D major 7 chord.
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Generate explanation
|
| 28 |
response = generator(
|
| 29 |
chord_prompt,
|
| 30 |
-
max_new_tokens=
|
| 31 |
-
temperature=0.
|
| 32 |
top_p=0.95
|
| 33 |
)
|
| 34 |
|
| 35 |
# Post-process to clean output
|
| 36 |
raw_text = response[0]["generated_text"]
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
return answer
|
| 39 |
|
| 40 |
-
# Gradio UI
|
| 41 |
-
|
| 42 |
fn=chord_bot,
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
title="🎶 Locally-Hosted Chord Bot",
|
| 46 |
-
description="This version runs FLAN-T5 locally with few-shot prompting to explain chord progressions."
|
| 47 |
)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
|
|
|
|
| 6 |
|
| 7 |
def chord_bot(prompt: str) -> str:
|
| 8 |
"""
|
| 9 |
+
Use FLAN-T5 with few-shot prompting to identify chords given a list of notes.
|
| 10 |
"""
|
| 11 |
chord_prompt = f"""
|
| 12 |
+
You are a chord identifier. Given 3 or more notes, return the chord name.
|
| 13 |
|
| 14 |
+
Examples:
|
| 15 |
+
Input: C E G
|
| 16 |
+
Output: C major
|
| 17 |
|
| 18 |
+
Input: D F# A
|
| 19 |
+
Output: D major
|
|
|
|
| 20 |
|
| 21 |
+
Input: D F# A C
|
| 22 |
+
Output: D7 (dominant 7)
|
| 23 |
+
|
| 24 |
+
Input: C Eb G
|
| 25 |
+
Output: C minor
|
| 26 |
+
|
| 27 |
+
Input: C Eb G Bb
|
| 28 |
+
Output: Cm7 (minor 7)
|
| 29 |
+
|
| 30 |
+
Now, identify this chord:
|
| 31 |
+
Input: {prompt}
|
| 32 |
+
Output:
|
| 33 |
+
"""
|
| 34 |
|
| 35 |
# Generate explanation
|
| 36 |
response = generator(
|
| 37 |
chord_prompt,
|
| 38 |
+
max_new_tokens=32,
|
| 39 |
+
temperature=0.0, # deterministic output
|
| 40 |
top_p=0.95
|
| 41 |
)
|
| 42 |
|
| 43 |
# Post-process to clean output
|
| 44 |
raw_text = response[0]["generated_text"]
|
| 45 |
+
if "Output:" in raw_text:
|
| 46 |
+
answer = raw_text.split("Output:")[-1].strip()
|
| 47 |
+
else:
|
| 48 |
+
answer = raw_text.strip()
|
| 49 |
return answer
|
| 50 |
|
| 51 |
+
# Gradio Chat UI
|
| 52 |
+
chatbot = gr.ChatInterface(
|
| 53 |
fn=chord_bot,
|
| 54 |
+
title="🎶 FLAN-T5 Chord Bot",
|
| 55 |
+
description="Type 3+ notes (e.g., C E G or Db F Ab C) and I'll identify the chord."
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
if __name__ == "__main__":
|
| 59 |
+
chatbot.launch()
|