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

using prof template

Browse files
Files changed (1) hide show
  1. app.py +88 -29
app.py CHANGED
@@ -1,41 +1,100 @@
1
  import gradio as gr
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__":
41
- chatbot.launch()
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
 
3
 
4
+ fancy_css = """
5
+ #main-container {
6
+ background-color: #f0f0f0;
7
+ font-family: 'Arial', sans-serif;
8
+ }
9
+ .gradio-container {
10
+ max-width: 700px;
11
+ margin: 0 auto;
12
+ padding: 20px;
13
+ background: white;
14
+ box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
15
+ border-radius: 10px;
16
+ }
17
+ .gr-button {
18
+ background-color: #4CAF50;
19
+ color: white;
20
+ border: none;
21
+ border-radius: 5px;
22
+ padding: 10px 20px;
23
+ cursor: pointer;
24
+ transition: background-color 0.3s ease;
25
+ }
26
+ .gr-button:hover {
27
+ background-color: #45a049;
28
+ }
29
+ .gr-chat {
30
+ font-size: 16px;
31
+ }
32
+ #title {
33
+ text-align: center;
34
+ font-size: 2em;
35
+ margin-bottom: 20px;
36
+ color: #333;
37
+ }
38
+ """
39
 
40
+ CHORD_SYSTEM_PROMPT = """You are a music theory expert specialized in chord identification.
41
+ Given a list of notes (like "C E G" or "D F# A C"), identify the chord name.
42
+ Always respond with the chord name and a short explanation of the intervals.
43
+ """
44
 
45
+ def respond(
46
+ message,
47
+ history: list[dict[str, str]],
48
+ system_message,
49
+ max_tokens,
50
+ temperature,
51
+ top_p,
52
+ hf_token: gr.OAuthToken,
53
+ ):
54
+ if hf_token is None or not getattr(hf_token, "token", None):
55
+ yield "⚠️ Please log in with your Hugging Face account first."
56
+ return
57
 
58
+ client = InferenceClient(
59
+ token=hf_token.token,
60
+ model="google/flan-t5-small", # change if you want to call your own model repo
61
+ )
 
62
 
63
+ # Build structured messages → turn into a prompt
64
+ messages = [{"role": "system", "content": system_message}]
65
+ messages.extend(history)
66
+ messages.append({"role": "user", "content": f"What chord is {message}?"})
67
+
68
+ prompt = "\n".join([f"{m['role']}: {m['content']}" for m in messages]) + "\nAnswer:"
69
+
70
+ response = ""
71
+ for chunk in client.text_generation(
72
+ prompt,
73
+ max_new_tokens=max_tokens,
74
+ temperature=temperature,
75
+ top_p=top_p,
76
+ stream=True,
77
+ ):
78
+ response += chunk
79
+ yield response.strip()
80
 
 
81
 
 
82
  chatbot = gr.ChatInterface(
83
+ fn=respond,
84
+ additional_inputs=[
85
+ gr.Textbox(value=CHORD_SYSTEM_PROMPT, label="System message"),
86
+ gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Max new tokens"),
87
+ gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
88
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
89
+ ],
90
+ type="messages",
91
  )
92
 
93
+ with gr.Blocks(css=fancy_css) as demo:
94
+ with gr.Row():
95
+ gr.Markdown("<h1 id='title'>🎶 Chord Bot (API-based) 🎶</h1>")
96
+ gr.LoginButton()
97
+ chatbot.render()
98
+
99
  if __name__ == "__main__":
100
+ demo.launch()