MsAsh commited on
Commit
c89ed51
·
verified ·
1 Parent(s): 0bb200d

reboot to lesson 10 and made changes

Browse files
Files changed (1) hide show
  1. app.py +41 -72
app.py CHANGED
@@ -3,75 +3,44 @@ from huggingface_hub import InferenceClient
3
 
4
  client = InferenceClient("Qwen/Qwen2.5-7B-Instruct")
5
 
6
- songs = [
7
- {
8
- "title": "Blinding Lights",
9
- "artist": "The Weeknd",
10
- "description": "An energetic pop song with retro synth vibes, great for night drives."
11
- },
12
- {
13
- "title": "Someone Like You",
14
- "artist": "Adele",
15
- "description": "A deeply emotional ballad about heartbreak and lost love."
16
- },
17
- {
18
- "title": "Lose Yourself",
19
- "artist": "Eminem",
20
- "description": "An intense and motivational rap song about seizing opportunities."
21
- },
22
- {
23
- "title": "Clair de Lune",
24
- "artist": "Debussy",
25
- "description": "A calm and soothing classical piano piece, perfect for relaxation."
26
- },
27
- ]
28
-
29
- from sentence_transformers import SentenceTransformer, util
30
-
31
- embedder = SentenceTransformer("all-MiniLM-L6-v2")
32
-
33
- song_texts = [s["description"] for s in songs]
34
- song_embeddings = embedder.encode(song_texts, convert_to_tensor=True)
35
-
36
- def retrieve_songs(query, top_k=2):
37
- query_embedding = embedder.encode(query, convert_to_tensor=True)
38
- scores = util.cos_sim(query_embedding, song_embeddings)[0]
39
-
40
- top_results = scores.topk(k=top_k)
41
-
42
- results = []
43
- for idx in top_results.indices:
44
- results.append(songs[idx])
45
-
46
- return results
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()