Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,32 +3,43 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
|
| 4 |
client = InferenceClient("microsoft/phi-4")
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
def respond(message, history):
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
if history:
|
| 9 |
messages.extend(history)
|
|
|
|
| 10 |
messages.append({"role": "user", "content": message})
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
return response['choices'][0]['message']['content'].strip()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
<div style="max-width: 720px; margin: 12px auto;">
|
| 16 |
-
<iframe
|
| 17 |
-
data-testid="embed-iframe"
|
| 18 |
-
style="border-radius:12px"
|
| 19 |
-
src="https://open.spotify.com/embed/playlist/2EoheVFjqIxgJMb8VnDRtZ?utm_source=generator&theme=0"
|
| 20 |
-
width="100%"
|
| 21 |
-
height="352"
|
| 22 |
-
frameborder="0"
|
| 23 |
-
allow="autoplay; clipboard-write; encrypted-media; fullscreen; picture-in-picture"
|
| 24 |
-
loading="lazy">
|
| 25 |
-
</iframe>
|
| 26 |
-
</div>
|
| 27 |
-
"""
|
| 28 |
-
|
| 29 |
-
with gr.Blocks() as demo:
|
| 30 |
-
chatbot = gr.ChatInterface(respond, type="messages", title="Kpop Song Hunters")
|
| 31 |
-
gr.HTML(spotify_iframe)
|
| 32 |
-
|
| 33 |
-
demo.launch()
|
| 34 |
|
|
|
|
|
|
| 3 |
|
| 4 |
client = InferenceClient("microsoft/phi-4")
|
| 5 |
|
| 6 |
+
# Step 1: Add a function that checks for genres in the user message
|
| 7 |
+
def recommend_songs(message):
|
| 8 |
+
genre_map = {
|
| 9 |
+
"pop": ["'Feel Special' - TWICE", "'Dynamite' - BTS"],
|
| 10 |
+
"r&b": ["'Instagram' - DEAN", "'You, Clouds, Rain' - Heize"],
|
| 11 |
+
"hip hop": ["'Daechwita' - Agust D", "'ON' - BTS"],
|
| 12 |
+
"dance": ["'Next Level' - aespa", "'Hot' - Seventeen"],
|
| 13 |
+
"rock": ["'Time of Our Life' - DAY6"]
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
for genre, songs in genre_map.items():
|
| 17 |
+
if genre in message.lower():
|
| 18 |
+
return f"Here are some {genre} K-pop songs you might like:\n" + "\n".join(songs)
|
| 19 |
+
return None
|
| 20 |
+
|
| 21 |
+
# Step 2: Update your respond function to check for recommendations first
|
| 22 |
def respond(message, history):
|
| 23 |
+
# Check if we can recommend songs based on genre keywords
|
| 24 |
+
quick_rec = recommend_songs(message)
|
| 25 |
+
if quick_rec:
|
| 26 |
+
return quick_rec # Return genre-based recommendations immediately
|
| 27 |
+
|
| 28 |
+
# Otherwise, call the language model for a general response
|
| 29 |
+
messages = [{"role": "system", "content": "You are a friendly chatbot who specializes in K-pop. Recommend songs, explain K-pop terms, and introduce groups based on user preferences."}]
|
| 30 |
+
|
| 31 |
if history:
|
| 32 |
messages.extend(history)
|
| 33 |
+
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
response = client.chat_completion(
|
| 37 |
+
messages,
|
| 38 |
+
max_tokens=150 # you can increase tokens if you want longer replies
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
return response['choices'][0]['message']['content'].strip()
|
| 42 |
|
| 43 |
+
chatbot = gr.ChatInterface(respond, type="messages", title="KpopBot")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
chatbot.launch()
|