Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,42 +13,26 @@ recommendations_db = {
|
|
| 13 |
client = InferenceClient("microsoft/phi-4")
|
| 14 |
|
| 15 |
def respond(message, history):
|
|
|
|
| 16 |
messages = [{"role": "system", "content": "You are a friendly K-pop expert chatbot."}]
|
| 17 |
-
|
| 18 |
-
messages.
|
|
|
|
|
|
|
| 19 |
messages.append({"role": "user", "content": message})
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
max_tokens=500
|
| 24 |
-
)
|
| 25 |
bot_reply = response['choices'][0]['message']['content'].strip()
|
| 26 |
|
| 27 |
-
#
|
| 28 |
for key, link in recommendations_db.items():
|
| 29 |
if key.lower() in message.lower():
|
| 30 |
bot_reply += f"\n\nπ΅ Listen on Spotify: {link}"
|
| 31 |
break
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
# ===== Information text =====
|
| 36 |
-
title = "# π€ K-pop Chatbot"
|
| 37 |
-
topics = """
|
| 38 |
-
π Hey K-pop fan!
|
| 39 |
-
Welcome to the ultimate K-pop experience! π
|
| 40 |
-
|
| 41 |
-
π **Ask me** about your favorite artists, songs, and groups.
|
| 42 |
-
π§ **Get instant Spotify links** to songs and albums.
|
| 43 |
-
π **Discover** your next bias or group!
|
| 44 |
-
|
| 45 |
-
---
|
| 46 |
-
|
| 47 |
-
### How to use:
|
| 48 |
-
1. Go to the **Chat** tab.
|
| 49 |
-
2. Type your question or click a pre-written one.
|
| 50 |
-
3. Enjoy your K-pop journey! πΆ
|
| 51 |
-
"""
|
| 52 |
|
| 53 |
# ===== Theme =====
|
| 54 |
chat_theme = gr.themes.Soft(
|
|
@@ -63,8 +47,42 @@ chat_theme = gr.themes.Soft(
|
|
| 63 |
with gr.Blocks(theme=chat_theme) as demo:
|
| 64 |
# --- Tab 1: Information ---
|
| 65 |
with gr.Tab("Information"):
|
| 66 |
-
gr.Markdown(
|
| 67 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
# --- Tab 2: Chat ---
|
| 70 |
with gr.Tab("Chat"):
|
|
@@ -74,16 +92,24 @@ with gr.Blocks(theme=chat_theme) as demo:
|
|
| 74 |
|
| 75 |
# Pre-written question buttons
|
| 76 |
with gr.Row():
|
| 77 |
-
btn1 = gr.Button("
|
| 78 |
-
btn2 = gr.Button("
|
| 79 |
-
btn3 = gr.Button("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
#
|
| 82 |
-
msg.submit(respond, [msg, state], [
|
| 83 |
|
| 84 |
# Buttons send their question
|
| 85 |
-
btn1.click(lambda: "
|
| 86 |
-
btn2.click(lambda: "
|
| 87 |
-
btn3.click(lambda: "
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
demo.launch(ssr_mode=False)
|
|
|
|
| 13 |
client = InferenceClient("microsoft/phi-4")
|
| 14 |
|
| 15 |
def respond(message, history):
|
| 16 |
+
# Convert history into API format
|
| 17 |
messages = [{"role": "system", "content": "You are a friendly K-pop expert chatbot."}]
|
| 18 |
+
for user_msg, bot_msg in history:
|
| 19 |
+
messages.append({"role": "user", "content": user_msg})
|
| 20 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 21 |
+
|
| 22 |
messages.append({"role": "user", "content": message})
|
| 23 |
|
| 24 |
+
# Get model reply
|
| 25 |
+
response = client.chat_completion(messages, max_tokens=500)
|
|
|
|
|
|
|
| 26 |
bot_reply = response['choices'][0]['message']['content'].strip()
|
| 27 |
|
| 28 |
+
# Check for Spotify link
|
| 29 |
for key, link in recommendations_db.items():
|
| 30 |
if key.lower() in message.lower():
|
| 31 |
bot_reply += f"\n\nπ΅ Listen on Spotify: {link}"
|
| 32 |
break
|
| 33 |
|
| 34 |
+
history.append((message, bot_reply))
|
| 35 |
+
return history, ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
# ===== Theme =====
|
| 38 |
chat_theme = gr.themes.Soft(
|
|
|
|
| 47 |
with gr.Blocks(theme=chat_theme) as demo:
|
| 48 |
# --- Tab 1: Information ---
|
| 49 |
with gr.Tab("Information"):
|
| 50 |
+
gr.Markdown("# π€ Welcome to the Ultimate K-pop Chatbot πΆ")
|
| 51 |
+
gr.Markdown("""
|
| 52 |
+
Whether you're a **newbie** to K-pop or a seasoned stan, this chatbot is your ultimate companion for discovering music, learning about artists, and exploring the vibrant world of Korean pop culture.
|
| 53 |
+
|
| 54 |
+
---
|
| 55 |
+
## π Popular Groups You Can Ask Me About
|
| 56 |
+
""")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
gr.Image("https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/BTS_logo_%28BTS%29.png/800px-BTS_logo_%28BTS%29.png", label="BTS Logo", scale=1)
|
| 61 |
+
gr.Markdown("**BTS** β The global phenomenon that broke records worldwide. [Listen on Spotify](https://open.spotify.com/artist/3Nrfpe0tUJi4K4DXYWgMUX)")
|
| 62 |
+
with gr.Column():
|
| 63 |
+
gr.Image("https://upload.wikimedia.org/wikipedia/commons/5/5c/Blackpink_logo.svg", label="BLACKPINK Logo", scale=1)
|
| 64 |
+
gr.Markdown("**BLACKPINK** β Queens of K-pop and chart-toppers. [Listen on Spotify](https://open.spotify.com/artist/41MozSoPIsD1dJM0CLPjZF)")
|
| 65 |
+
with gr.Column():
|
| 66 |
+
gr.Image("https://upload.wikimedia.org/wikipedia/commons/f/fd/NewJeans_logo.svg", label="NewJeans Logo", scale=1)
|
| 67 |
+
gr.Markdown("**NewJeans** β The fresh and trendy group taking over charts. [Listen on Spotify](https://open.spotify.com/artist/6HvZYsbFfjnjFrWF950C9d)")
|
| 68 |
+
|
| 69 |
+
gr.Markdown("""
|
| 70 |
+
---
|
| 71 |
+
## π‘ How to Use the Chatbot
|
| 72 |
+
1. Go to the **Chat** tab.
|
| 73 |
+
2. Ask me anything about your favorite K-pop groups, songs, or history.
|
| 74 |
+
3. Get **Spotify links** instantly for music recommendations.
|
| 75 |
+
4. Click a pre-written question to try it out fast.
|
| 76 |
+
|
| 77 |
+
---
|
| 78 |
+
## π― Example Questions
|
| 79 |
+
- *"What is K-pop?"*
|
| 80 |
+
- *"How to find your bias?"*
|
| 81 |
+
- *"Tell me about BTS"*
|
| 82 |
+
- *"Tell me about BLACKPINK"*
|
| 83 |
+
- *"Recommend a K-pop song"*
|
| 84 |
+
- *"Who are the most popular idols right now?"*
|
| 85 |
+
""")
|
| 86 |
|
| 87 |
# --- Tab 2: Chat ---
|
| 88 |
with gr.Tab("Chat"):
|
|
|
|
| 92 |
|
| 93 |
# Pre-written question buttons
|
| 94 |
with gr.Row():
|
| 95 |
+
btn1 = gr.Button("What is K-pop?")
|
| 96 |
+
btn2 = gr.Button("How to find your bias?")
|
| 97 |
+
btn3 = gr.Button("Tell me about BTS")
|
| 98 |
+
|
| 99 |
+
with gr.Row():
|
| 100 |
+
btn4 = gr.Button("Tell me about BLACKPINK")
|
| 101 |
+
btn5 = gr.Button("Recommend a K-pop song")
|
| 102 |
+
btn6 = gr.Button("Who are the most popular idols right now?")
|
| 103 |
|
| 104 |
+
# Normal message submit
|
| 105 |
+
msg.submit(respond, [msg, state], [chatbot_ui, msg])
|
| 106 |
|
| 107 |
# Buttons send their question
|
| 108 |
+
btn1.click(lambda: "What is K-pop?", None, msg).then(respond, [msg, state], [chatbot_ui, msg])
|
| 109 |
+
btn2.click(lambda: "How to find your bias?", None, msg).then(respond, [msg, state], [chatbot_ui, msg])
|
| 110 |
+
btn3.click(lambda: "Tell me about BTS", None, msg).then(respond, [msg, state], [chatbot_ui, msg])
|
| 111 |
+
btn4.click(lambda: "Tell me about BLACKPINK", None, msg).then(respond, [msg, state], [chatbot_ui, msg])
|
| 112 |
+
btn5.click(lambda: "Recommend a K-pop song", None, msg).then(respond, [msg, state], [chatbot_ui, msg])
|
| 113 |
+
btn6.click(lambda: "Who are the most popular idols right now?", None, msg).then(respond, [msg, state], [chatbot_ui, msg])
|
| 114 |
|
| 115 |
demo.launch(ssr_mode=False)
|