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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -2,22 +2,39 @@ import gradio as gr
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__":
 
2
  import requests
3
  import os
4
 
5
+ # Hugging Face Inference API endpoint (supported free model)
6
+ API_URL = "https://api-inference.huggingface.co/models/t5-small"
7
+
8
+ # Use your HF token (store in Space secrets as HF_TOKEN)
9
  HF_TOKEN = os.environ.get("HF_TOKEN")
10
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
11
 
12
+ def chord_api(message: str, history: list[tuple[str, str]]):
13
+ """
14
+ Calls Hugging Face Inference API (t5-small) to classify chords.
15
+ """
16
  prompt = f"What chord do these notes form? {message}"
17
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
18
+
19
  if response.status_code == 200:
20
+ try:
21
+ return response.json()[0]["generated_text"]
22
+ except Exception:
23
+ return f"⚠️ Unexpected response: {response.text}"
24
+
25
+ elif response.status_code == 503:
26
+ return "⏳ Model is loading, please try again in a moment."
27
+
28
  return f"❌ Error {response.status_code}: {response.text}"
29
 
30
+ # Gradio chat interface
31
  chatbot = gr.ChatInterface(
32
  fn=chord_api,
33
  title="🎶 Chord Bot (API Version)",
34
+ description=(
35
+ "This version queries Hugging Face’s free Inference API (`t5-small`). "
36
+ "Enter notes like `C E G` to see what chord they form."
37
+ ),
38
  )
39
 
40
  if __name__ == "__main__":