kshahnathwani commited on
Commit
6fe37eb
·
verified ·
1 Parent(s): 086f215

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import os
4
+
5
+ # -------------------------
6
+ # Hugging Face API setup
7
+ # -------------------------
8
+ # Replace with your org/model name
9
+ API_URL = "https://api-inference.huggingface.co/models/DS553-Music-Bot/chord_classifier"
10
+
11
+ # Expect your HF token as an environment variable (set in HF Space secrets or GitHub Actions)
12
+ HF_TOKEN = os.environ.get("HF_TOKEN")
13
+
14
+ if HF_TOKEN is None:
15
+ raise ValueError("❌ HF_TOKEN not set. Please add it to your Hugging Face Space or GitHub secrets.")
16
+
17
+ headers = {"Authorization": f"Bearer {HF_TOKEN}"}
18
+
19
+ # -------------------------
20
+ # Query function
21
+ # -------------------------
22
+ def query_chord(message: str, history: list[tuple[str, str]]):
23
+ """
24
+ Send note input to Hugging Face Inference API, return predicted chord.
25
+ """
26
+ payload = {"inputs": message}
27
+ try:
28
+ response = requests.post(API_URL, headers=headers, json=payload, timeout=30)
29
+ except Exception as e:
30
+ return f"❌ Request failed: {str(e)}"
31
+
32
+ if response.status_code == 200:
33
+ try:
34
+ result = response.json()
35
+ # Expecting the model repo to return {"label": "Cmaj"} style JSON
36
+ if isinstance(result, dict) and "label" in result:
37
+ return f"🎵 Identified chord: **{result['label']}**"
38
+ else:
39
+ return f"⚠️ Unexpected API response: {result}"
40
+ except Exception as e:
41
+ return f"⚠️ Error parsing API response: {str(e)}"
42
+ else:
43
+ return f"❌ Error {response.status_code}: {response.text}"
44
+
45
+ # -------------------------
46
+ # Gradio ChatInterface
47
+ # -------------------------
48
+ chatbot = gr.ChatInterface(
49
+ fn=query_chord,
50
+ title="🎶 Chord Bot (API Version)",
51
+ description="Enter 2+ notes (e.g., C E G or Db F Ab C). "
52
+ "This version queries the Hugging Face Inference API "
53
+ "for chord classification."
54
+ )
55
+
56
+ if __name__ == "__main__":
57
+ chatbot.launch()