Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from chord_identifier import identify_chord
|
| 3 |
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("google/flan-t5-small")
|
| 7 |
|
| 8 |
def predict_chord(notes: str):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
#
|
| 13 |
chord = identify_chord(note_list)
|
| 14 |
if chord != "Unknown Chord":
|
| 15 |
return chord
|
| 16 |
|
| 17 |
-
# Fall back to LLM
|
| 18 |
prompt = f"Identify the musical chord made of notes: {', '.join(note_list)}"
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
|
|
|
| 22 |
demo = gr.Interface(
|
| 23 |
fn=predict_chord,
|
| 24 |
inputs=gr.Textbox(lines=1, placeholder="Enter notes, e.g., C,E,G"),
|
| 25 |
outputs="text",
|
| 26 |
-
title="Chord Identifier (API-based)"
|
|
|
|
| 27 |
)
|
| 28 |
|
| 29 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
|
| 4 |
+
# --- Rule-based chord dictionary ---
|
| 5 |
+
CHORDS = {
|
| 6 |
+
frozenset(["C", "E", "G"]): "C Major",
|
| 7 |
+
frozenset(["A", "C#", "E"]): "A Major",
|
| 8 |
+
frozenset(["A", "C", "E"]): "A Minor",
|
| 9 |
+
frozenset(["D", "F#", "A"]): "D Major",
|
| 10 |
+
frozenset(["E", "G#", "B"]): "E Major",
|
| 11 |
+
frozenset(["G", "B", "D"]): "G Major",
|
| 12 |
+
frozenset(["F", "A", "C"]): "F Major",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def identify_chord(notes):
|
| 16 |
+
"""
|
| 17 |
+
Identify chord name from a list of notes using dictionary lookup.
|
| 18 |
+
"""
|
| 19 |
+
key = frozenset([n.upper() for n in notes])
|
| 20 |
+
return CHORDS.get(key, "Unknown Chord")
|
| 21 |
+
|
| 22 |
+
# --- Hugging Face model client (API-based) ---
|
| 23 |
client = InferenceClient("google/flan-t5-small")
|
| 24 |
|
| 25 |
def predict_chord(notes: str):
|
| 26 |
+
"""
|
| 27 |
+
Takes a comma-separated string of notes and predicts the chord.
|
| 28 |
+
First tries rule-based, then falls back to the LLM.
|
| 29 |
+
"""
|
| 30 |
+
if not notes.strip():
|
| 31 |
+
return "Please enter 2 or more notes."
|
| 32 |
+
|
| 33 |
+
# Clean and split notes
|
| 34 |
+
note_list = [n.strip() for n in notes.split(",") if n.strip()]
|
| 35 |
+
if len(note_list) < 2:
|
| 36 |
+
return "Please enter at least 2 notes."
|
| 37 |
|
| 38 |
+
# Rule-based chord lookup
|
| 39 |
chord = identify_chord(note_list)
|
| 40 |
if chord != "Unknown Chord":
|
| 41 |
return chord
|
| 42 |
|
| 43 |
+
# Fall back to LLM
|
| 44 |
prompt = f"Identify the musical chord made of notes: {', '.join(note_list)}"
|
| 45 |
+
try:
|
| 46 |
+
response = client.text_generation(prompt, max_new_tokens=20)
|
| 47 |
+
return response.strip()
|
| 48 |
+
except Exception as e:
|
| 49 |
+
return f"Error calling Hugging Face API: {e}"
|
| 50 |
|
| 51 |
+
# --- Gradio Interface ---
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=predict_chord,
|
| 54 |
inputs=gr.Textbox(lines=1, placeholder="Enter notes, e.g., C,E,G"),
|
| 55 |
outputs="text",
|
| 56 |
+
title="Chord Identifier (API-based)",
|
| 57 |
+
description="Enter two or more musical notes (comma-separated) and get the chord name."
|
| 58 |
)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|