Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,111 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
transition: background-color 0.3s ease;
|
| 28 |
-
}
|
| 29 |
-
.gr-button:hover {
|
| 30 |
-
background-color: #45a049;
|
| 31 |
-
}
|
| 32 |
-
.gr-chat {
|
| 33 |
-
font-size: 16px;
|
| 34 |
-
}
|
| 35 |
-
#title {
|
| 36 |
-
text-align: center;
|
| 37 |
-
font-size: 2em;
|
| 38 |
-
margin-bottom: 20px;
|
| 39 |
-
color: #333;
|
| 40 |
-
}
|
| 41 |
-
"""
|
| 42 |
-
|
| 43 |
-
# System prompt specialized for chord bot
|
| 44 |
-
CHORD_SYSTEM_PROMPT = """You are a music theory expert specialized in chord identification.
|
| 45 |
-
Given a list of notes (like "C E G" or "D F# A C"), identify the chord name.
|
| 46 |
-
Always respond with the chord name and a short explanation of the intervals.
|
| 47 |
-
"""
|
| 48 |
-
|
| 49 |
-
# Hugging Face API token (from Space secrets)
|
| 50 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 51 |
-
|
| 52 |
-
# API URL – using GPT-2 as demo, can be swapped for another model
|
| 53 |
-
API_URL = "https://api-inference.huggingface.co/models/distilgpt2"
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
| 57 |
-
if HF_TOKEN is None:
|
| 58 |
-
return "⚠️ No HF_TOKEN found. Please add it in your Space secrets."
|
| 59 |
-
|
| 60 |
-
try:
|
| 61 |
-
# Build prompt
|
| 62 |
-
prompt = f"{system_message}\nUser: {message}\nAnswer:"
|
| 63 |
-
|
| 64 |
-
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
|
| 65 |
-
payload = {
|
| 66 |
-
"inputs": prompt,
|
| 67 |
-
"parameters": {
|
| 68 |
-
"max_new_tokens": max_tokens,
|
| 69 |
-
"temperature": temperature,
|
| 70 |
-
"top_p": top_p,
|
| 71 |
-
},
|
| 72 |
-
}
|
| 73 |
-
|
| 74 |
-
# Call Hugging Face Inference API
|
| 75 |
-
response = requests.post(API_URL, headers=headers, json=payload)
|
| 76 |
-
|
| 77 |
-
if response.status_code != 200:
|
| 78 |
-
return f"⚠️ API Error {response.status_code}: {response.text}"
|
| 79 |
-
|
| 80 |
-
data = response.json()
|
| 81 |
-
|
| 82 |
-
# Extract generated text
|
| 83 |
-
if isinstance(data, list) and len(data) > 0 and "generated_text" in data[0]:
|
| 84 |
-
return data[0]["generated_text"].strip()
|
| 85 |
-
else:
|
| 86 |
-
return str(data)
|
| 87 |
-
|
| 88 |
-
except Exception as e:
|
| 89 |
-
tb = traceback.format_exc()
|
| 90 |
-
return f"⚠️ Error: {str(e)}\n\nTraceback:\n{tb}"
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# Gradio ChatInterface
|
| 94 |
-
chatbot = gr.ChatInterface(
|
| 95 |
-
fn=respond,
|
| 96 |
-
additional_inputs=[
|
| 97 |
-
gr.Textbox(value=CHORD_SYSTEM_PROMPT, label="System message"),
|
| 98 |
-
gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Max new tokens"),
|
| 99 |
-
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 100 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 101 |
-
],
|
| 102 |
-
type="messages",
|
| 103 |
)
|
| 104 |
|
| 105 |
-
# Layout
|
| 106 |
-
with gr.Blocks(css=fancy_css) as demo:
|
| 107 |
-
gr.Markdown("<h1 id='title'>🎶 Chord Bot (API-based) 🎶</h1>")
|
| 108 |
-
chatbot.render()
|
| 109 |
-
|
| 110 |
if __name__ == "__main__":
|
| 111 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from chord_identifier import identify_chord
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Load Hugging Face API client (example: flan-t5-small)
|
| 6 |
+
client = InferenceClient("google/flan-t5-small")
|
| 7 |
+
|
| 8 |
+
def predict_chord(notes: str):
|
| 9 |
+
# Split comma-separated input
|
| 10 |
+
note_list = [n.strip() for n in notes.split(",")]
|
| 11 |
+
|
| 12 |
+
# Try rule-based
|
| 13 |
+
chord = identify_chord(note_list)
|
| 14 |
+
if chord != "Unknown Chord":
|
| 15 |
+
return chord
|
| 16 |
+
|
| 17 |
+
# Fall back to LLM for reasoning
|
| 18 |
+
prompt = f"Identify the musical chord made of notes: {', '.join(note_list)}"
|
| 19 |
+
response = client.text_generation(prompt, max_new_tokens=20)
|
| 20 |
+
return response.strip()
|
| 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__":
|
| 30 |
demo.launch()
|