KWK25 commited on
Commit
e930fe8
·
verified ·
1 Parent(s): b935fbd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -48
app.py CHANGED
@@ -1,36 +1,14 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- import requests
4
 
5
  # Hugging Face model
6
  client = InferenceClient("microsoft/phi-4")
7
 
8
- # -------------------------
9
- # Spotify Search Function
10
- # -------------------------
11
- SPOTIFY_TOKEN = "YOUR_SPOTIFY_ACCESS_TOKEN" # Add your token here
12
-
13
- def search_spotify(query):
14
- url = "https://api.spotify.com/v1/search"
15
- headers = {"Authorization": f"Bearer {SPOTIFY_TOKEN}"}
16
- params = {"q": query, "type": "track", "limit": 5}
17
- r = requests.get(url, headers=headers, params=params)
18
- if r.status_code != 200:
19
- return []
20
- data = r.json()
21
- tracks = []
22
- for item in data.get("tracks", {}).get("items", []):
23
- track_name = item["name"]
24
- artist_name = item["artists"][0]["name"]
25
- link = item["external_urls"]["spotify"]
26
- tracks.append(f"{track_name} by {artist_name} — [Listen on Spotify]({link})")
27
- return tracks
28
-
29
  # -------------------------
30
  # Chatbot Response Function
31
  # -------------------------
32
  def respond(message, history):
33
- style_prompt = "Answer briefly unless the user asks for more details. If recommending songs, provide working Spotify links."
34
 
35
  messages = [{"role": "system", "content": f"You are a friendly Kpop expert chatbot. {style_prompt}"}]
36
 
@@ -39,17 +17,11 @@ def respond(message, history):
39
 
40
  messages.append({"role": "user", "content": message})
41
 
42
- # If asking for recommendations
43
- if "recommend" in message.lower() and "song" in message.lower():
44
- songs = search_spotify("kpop")
45
- if songs:
46
- return "Here are some Kpop songs I recommend:\n" + "\n".join(songs)
47
-
48
  response = client.chat_completion(messages, max_tokens=500)
49
  return response['choices'][0]['message']['content'].strip()
50
 
51
  # -------------------------
52
- # Information Page Content
53
  # -------------------------
54
  intro_text = """
55
  # 🎤 Welcome to Your Ultimate Kpop Chatbot 🎶
@@ -64,25 +36,24 @@ Whether you’re new to the fandom or a longtime stan, I’m here to spill all t
64
  ---
65
 
66
  ## 📊 Top 10 Popular Kpop Groups (2025)
67
- 1. BTS
68
- 2. BLACKPINK
69
- 3. SEVENTEEN
70
- 4. Stray Kids
71
- 5. TWICE
72
- 6. NewJeans
73
- 7. ATEEZ
74
- 8. (G)I-DLE
75
- 9. ENHYPEN
76
- 10. IVE
77
 
78
  ---
79
 
80
  ## 💡 How to Use the Chatbot
81
- 1. Click on one of the preset questions or type your own.
82
  2. If you want **song recommendations**, just ask!
83
  3. I’ll give **short answers** unless you ask for more detail.
84
- 4. All song recommendations come with **working Spotify links**.
85
- 5. You can explore idols, history, and Kpop trends anytime.
86
  """
87
 
88
  # -------------------------
@@ -122,10 +93,20 @@ with gr.Blocks(theme=chat_theme) as demo:
122
  gr.Markdown(intro_text)
123
 
124
  with gr.Tab("💬 Chat"):
125
- with gr.Column():
126
- with gr.Row():
127
- for q in preset_questions:
128
- gr.Button(q).click(fn=respond, inputs=gr.Textbox(value=q, visible=False), outputs=None)
129
- chatbot_ui = gr.ChatInterface(fn=respond, type="messages")
 
 
 
 
 
 
 
 
 
 
130
 
131
  demo.launch()
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
 
3
 
4
  # Hugging Face model
5
  client = InferenceClient("microsoft/phi-4")
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # -------------------------
8
  # Chatbot Response Function
9
  # -------------------------
10
  def respond(message, history):
11
+ style_prompt = "Answer briefly unless the user asks for more details."
12
 
13
  messages = [{"role": "system", "content": f"You are a friendly Kpop expert chatbot. {style_prompt}"}]
14
 
 
17
 
18
  messages.append({"role": "user", "content": message})
19
 
 
 
 
 
 
 
20
  response = client.chat_completion(messages, max_tokens=500)
21
  return response['choices'][0]['message']['content'].strip()
22
 
23
  # -------------------------
24
+ # Information Page Content with Clickable Spotify Links
25
  # -------------------------
26
  intro_text = """
27
  # 🎤 Welcome to Your Ultimate Kpop Chatbot 🎶
 
36
  ---
37
 
38
  ## 📊 Top 10 Popular Kpop Groups (2025)
39
+ 1. [BTS](https://open.spotify.com/artist/3Nrfpe0tUJi4K4DXYWgMUX)
40
+ 2. [BLACKPINK](https://open.spotify.com/artist/41MozSoPIsD1dJM0CLPjZF)
41
+ 3. [SEVENTEEN](https://open.spotify.com/artist/7n2Ycct7Beij7Dj7meI4X0)
42
+ 4. [Stray Kids](https://open.spotify.com/artist/2dIgFjalVxs4ThymZ67YCE)
43
+ 5. [TWICE](https://open.spotify.com/artist/7n2Ycct7Beij7Dj7meI4X0)
44
+ 6. [NewJeans](https://open.spotify.com/artist/6HvZYsbFfjnjFrWF950C9d)
45
+ 7. [ATEEZ](https://open.spotify.com/artist/68KmkJeZGfwe1OUaivBa2L)
46
+ 8. [(G)I-DLE](https://open.spotify.com/artist/6OwKE9Ez6ALxpTaKcT5ayv)
47
+ 9. [ENHYPEN](https://open.spotify.com/artist/5t5FqBwTcgKTaWmfEbwQY9)
48
+ 10. [IVE](https://open.spotify.com/artist/6RHTUrRF63xao58xh9FXYJ)
49
 
50
  ---
51
 
52
  ## 💡 How to Use the Chatbot
53
+ 1. Click on one of the preset questions below in the **Chat** tab or type your own question.
54
  2. If you want **song recommendations**, just ask!
55
  3. I’ll give **short answers** unless you ask for more detail.
56
+ 4. You can explore idols, history, and Kpop trends anytime.
 
57
  """
58
 
59
  # -------------------------
 
93
  gr.Markdown(intro_text)
94
 
95
  with gr.Tab("💬 Chat"):
96
+ chatbot_ui = gr.Chatbot()
97
+ user_input = gr.Textbox(placeholder="Type your message here...")
98
+ send_btn = gr.Button("Send")
99
+
100
+ # Row of preset question buttons
101
+ with gr.Row():
102
+ for q in preset_questions:
103
+ gr.Button(q).click(
104
+ lambda x=q, h=[]: (x, respond(x, h)),
105
+ inputs=[],
106
+ outputs=[user_input, chatbot_ui]
107
+ )
108
+
109
+ send_btn.click(fn=respond, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
110
+ user_input.submit(fn=respond, inputs=[user_input, chatbot_ui], outputs=chatbot_ui)
111
 
112
  demo.launch()