Spaces:
Sleeping
Sleeping
reboot to lesson 10 and made changes
Browse files
app.py
CHANGED
|
@@ -3,75 +3,44 @@ from huggingface_hub import InferenceClient
|
|
| 3 |
|
| 4 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
"
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
"
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
def generate_response(query, retrieved_songs):
|
| 49 |
-
context = "\n".join([
|
| 50 |
-
f"{s['title']} by {s['artist']}: {s['description']}"
|
| 51 |
-
for s in retrieved_songs
|
| 52 |
-
])
|
| 53 |
-
|
| 54 |
-
prompt = f"""
|
| 55 |
-
User preference: {query}
|
| 56 |
-
|
| 57 |
-
Here are some songs:
|
| 58 |
-
{context}
|
| 59 |
-
|
| 60 |
-
Recommend one or two songs and explain why they fit the user's mood.
|
| 61 |
-
"""
|
| 62 |
-
|
| 63 |
-
output = generator(prompt, max_length=150, num_return_sequences=1)
|
| 64 |
-
return output[0]["generated_text"]
|
| 65 |
-
|
| 66 |
-
print("🎵 Music Bot (RAG powered). Type 'exit' to quit.\n")
|
| 67 |
-
|
| 68 |
-
while True:
|
| 69 |
-
user_input = input("You: ")
|
| 70 |
-
|
| 71 |
-
if user_input.lower() == "exit":
|
| 72 |
-
break
|
| 73 |
-
|
| 74 |
-
retrieved = retrieve_songs(user_input)
|
| 75 |
-
response = generate_response(user_input, retrieved)
|
| 76 |
-
|
| 77 |
-
print("\nBot:", response, "\n")
|
|
|
|
| 3 |
|
| 4 |
client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
|
| 5 |
|
| 6 |
+
def respond(message, history):
|
| 7 |
+
|
| 8 |
+
messages = [{
|
| 9 |
+
"role": "system",
|
| 10 |
+
"content": (
|
| 11 |
+
"You are a warm, friendly chatbot who believes all users are sharks. "
|
| 12 |
+
"Always include a short, uplifting positive affirmation in every response. "
|
| 13 |
+
"Keep responses supportive, encouraging, and gentle."
|
| 14 |
+
)
|
| 15 |
+
}]
|
| 16 |
+
|
| 17 |
+
def add_affirmation_if_missing(text):
|
| 18 |
+
affirmations = [
|
| 19 |
+
"Remember, you are capable and strong.",
|
| 20 |
+
"You deserve kindness and good things.",
|
| 21 |
+
"You’re doing a great job, even on tough days.",
|
| 22 |
+
"You are a necessary part of the world."
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
if any(phrase in text.lower() for phrase in ["you are", "you're", "you’ve got", "you can"]):
|
| 26 |
+
return text
|
| 27 |
+
else:
|
| 28 |
+
return text + " " + affirmations[0]
|
| 29 |
+
if history:
|
| 30 |
+
for user_msg, bot_msg in history:
|
| 31 |
+
messages.append({"role": "user", "content": user_msg})
|
| 32 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 33 |
+
|
| 34 |
+
messages.append({"role": "user", "content": message})
|
| 35 |
+
|
| 36 |
+
response = client.chat_completion(
|
| 37 |
+
messages,
|
| 38 |
+
max_tokens=110
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
reply = response.choices[0].message.content.strip()
|
| 42 |
+
return add_affirmation_if_missing(reply)
|
| 43 |
+
|
| 44 |
+
chatbot = gr.ChatInterface(respond)
|
| 45 |
+
|
| 46 |
+
chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|