Update app.py
Browse files
app.py
CHANGED
|
@@ -51,60 +51,33 @@ def generate(prompt, history, temperature=0.1, max_new_tokens=2048, top_p=0.8, r
|
|
| 51 |
playlist_message = f"Playing {mood.capitalize()} playlist for you!"
|
| 52 |
return playlist_message
|
| 53 |
return output
|
| 54 |
-
def format_prompt(
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
2. You must classify the mood within 2-3 exchanges maximum.
|
| 59 |
-
3. ALWAYS respond with a question about the user's current emotional state unless you can confidently classify the mood based on their explicit statement of feeling.
|
| 60 |
-
4. Once you've determined the mood with confidence, respond ONLY with the mood category: Happy, Sad, Instrumental, or Party.
|
| 61 |
-
5. Use this mood guide strictly:
|
| 62 |
-
- Happy: user explicitly expresses positive emotions, joy, excitement, contentment
|
| 63 |
-
- Sad: user explicitly expresses negative emotions, feeling down, tired, upset, stressed
|
| 64 |
-
- Instrumental: user explicitly expresses need for focus, concentration, calmness, relaxation
|
| 65 |
-
- Party: user explicitly expresses desire to celebrate, dance, high energy, excitement for social events
|
| 66 |
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
| 70 |
-
User: I need a coffee.
|
| 71 |
-
Assistant: I understand you need a coffee. How are you feeling emotionally right now? Are you tired, excited, or perhaps stressed?
|
| 72 |
|
| 73 |
-
|
| 74 |
-
Assistant: I see the coffee might make you feel physically warm, but I'm more interested in your emotional state. Can you describe how you're feeling inside? Are you happy, anxious, or maybe looking forward to something?
|
| 75 |
-
|
| 76 |
-
User: I feel weird about technology.
|
| 77 |
-
Assistant: When you say you feel weird about technology, can you elaborate on that emotion? Are you feeling anxious, excited, or perhaps confused? I'm trying to understand your specific emotional state.
|
| 78 |
-
|
| 79 |
-
User: I'm excited about my upcoming vacation!
|
| 80 |
-
Assistant: Happy
|
| 81 |
-
|
| 82 |
-
User: I need to focus on my work, I'm feeling very distracted.
|
| 83 |
-
Assistant: Instrumental
|
| 84 |
-
|
| 85 |
-
User: I'm feeling really down and upset today.
|
| 86 |
-
Assistant: Sad
|
| 87 |
-
|
| 88 |
-
User: Let's have a big celebration tonight! I'm so pumped!
|
| 89 |
-
Assistant: Party
|
| 90 |
-
|
| 91 |
-
Remember, NEVER classify a mood without first asking for clarification about the user's emotional state. Your job is to accurately classify the user's mood as quickly as possible, but ONLY after you've received a clear indication of their emotional state."""
|
| 92 |
-
|
| 93 |
-
conversation = [
|
| 94 |
-
{"role": "system", "content": system_prompt}
|
| 95 |
-
]
|
| 96 |
|
|
|
|
|
|
|
|
|
|
| 97 |
for user_prompt, bot_response in history:
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
{"role": "assistant", "content": bot_response}
|
| 101 |
-
])
|
| 102 |
-
|
| 103 |
-
conversation.append({"role": "user", "content": message})
|
| 104 |
-
|
| 105 |
-
prompt = self.tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
| 106 |
-
|
| 107 |
return prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
prompt = f"{fixed_prompt}\n"
|
| 109 |
for user_prompt, bot_response in history:
|
| 110 |
prompt += f"User: {user_prompt}\nLLM Response: {bot_response}\n"
|
|
|
|
| 51 |
playlist_message = f"Playing {mood.capitalize()} playlist for you!"
|
| 52 |
return playlist_message
|
| 53 |
return output
|
| 54 |
+
def format_prompt(message, history):
|
| 55 |
+
"""Formats the prompt including fixed instructions and conversation history."""
|
| 56 |
+
fixed_prompt = """
|
| 57 |
+
You are a smart mood analyser, who determines user mood. Based on the user input, classify the mood of the user into one of the four moods {Happy, Sad, Instrumental, Party}. If you are finding it difficult to classify into one of these four moods, keep the conversation going on until we classify the user's mood. Return a single-word reply from one of the options if you have classified. Suppose you classify a sentence as happy, then just respond with "happy".
|
| 58 |
|
| 59 |
+
Note: Do not write anything else other than the classified mood if classified.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
Note: If any question or any user text cannot be classified, follow up with a question to know the user's mood until you classify the mood.
|
| 62 |
|
| 63 |
+
Note: Mood should be classified only from any of these 4 classes {Happy, Sad, Instrumental, Party}, if not any of these 4 then continue with a follow-up question until you classify the mood.
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
Note: if user asks something like i need a coffee then do not classify the mood directly and ask more follow-up questions as asked in examples.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
[Examples omitted for brevity]
|
| 68 |
+
"""
|
| 69 |
+
prompt = f"{fixed_prompt}\n"
|
| 70 |
for user_prompt, bot_response in history:
|
| 71 |
+
prompt += f"User: {user_prompt}\nLLM Response: {bot_response}\n"
|
| 72 |
+
prompt += f"User: {message}\nLLM Response:"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
return prompt
|
| 74 |
+
|
| 75 |
+
async def text_to_speech(text):
|
| 76 |
+
communicate = edge_tts.Communicate(text)
|
| 77 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 78 |
+
tmp_path = tmp_file.name
|
| 79 |
+
await communicate.save(tmp_path)
|
| 80 |
+
return tmp_path
|
| 81 |
prompt = f"{fixed_prompt}\n"
|
| 82 |
for user_prompt, bot_response in history:
|
| 83 |
prompt += f"User: {user_prompt}\nLLM Response: {bot_response}\n"
|