kshahnathwani commited on
Commit
8ef8763
·
verified ·
1 Parent(s): f03eabb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -43
app.py CHANGED
@@ -2,56 +2,22 @@ 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
- print("DEBUG:", response.status_code, response.text)
45
 
46
- # -------------------------
47
- # Gradio ChatInterface
48
- # -------------------------
49
  chatbot = gr.ChatInterface(
50
- fn=query_chord,
51
  title="🎶 Chord Bot (API Version)",
52
- description="Enter 2+ notes (e.g., C E G or Db F Ab C). "
53
- "This version queries the Hugging Face Inference API "
54
- "for chord classification."
55
  )
56
 
57
  if __name__ == "__main__":
 
2
  import requests
3
  import os
4
 
5
+ API_URL = "https://api-inference.huggingface.co/models/google/flan-t5-small"
 
 
 
 
 
 
6
  HF_TOKEN = os.environ.get("HF_TOKEN")
 
 
 
 
7
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
8
 
9
+ def chord_api(message: str, history: list[tuple[str,str]]):
10
+ prompt = f"What chord do these notes form? {message}"
11
+ payload = {"inputs": prompt}
12
+ response = requests.post(API_URL, headers=headers, json=payload)
 
 
 
 
 
 
 
 
 
13
  if response.status_code == 200:
14
+ return response.json()[0]["generated_text"]
15
+ return f"❌ Error {response.status_code}: {response.text}"
 
 
 
 
 
 
 
 
 
 
16
 
 
 
 
17
  chatbot = gr.ChatInterface(
18
+ fn=chord_api,
19
  title="🎶 Chord Bot (API Version)",
20
+ description="This version calls Hugging Face’s Inference API (flan-t5-small)."
 
 
21
  )
22
 
23
  if __name__ == "__main__":