Update app.py
Browse files
app.py
CHANGED
|
@@ -22,35 +22,6 @@ def classify_mood(input_string):
|
|
| 22 |
if word in input_string:
|
| 23 |
return word, True
|
| 24 |
return None, False
|
| 25 |
-
|
| 26 |
-
def generate(prompt, history, temperature=0.1, max_new_tokens=2048, top_p=0.8, repetition_penalty=1.0):
|
| 27 |
-
temperature = float(temperature)
|
| 28 |
-
if temperature < 1e-2:
|
| 29 |
-
temperature = 1e-2
|
| 30 |
-
top_p = float(top_p)
|
| 31 |
-
|
| 32 |
-
generate_kwargs = dict(
|
| 33 |
-
temperature=temperature,
|
| 34 |
-
max_new_tokens=max_new_tokens,
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
repetition_penalty=repetition_penalty,
|
| 37 |
-
do_sample=True,
|
| 38 |
-
seed=42,
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
formatted_prompt = format_prompt(prompt, history)
|
| 42 |
-
|
| 43 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 44 |
-
output = ""
|
| 45 |
-
|
| 46 |
-
for response in stream:
|
| 47 |
-
output += response.token.text
|
| 48 |
-
mood, is_classified = classify_mood(output)
|
| 49 |
-
if is_classified:
|
| 50 |
-
playlist_message = f"Playing {mood.capitalize()} playlist for you!"
|
| 51 |
-
return playlist_message
|
| 52 |
-
return output
|
| 53 |
-
|
| 54 |
def format_prompt(self, message, history):
|
| 55 |
fixed_prompt = """You are a mood classification chatbot. Your ONLY goal is to accurately classify the user's mood into one of four categories: Happy, Sad, Instrumental, or Party. Follow these rules strictly:
|
| 56 |
|
|
@@ -111,6 +82,35 @@ Remember, NEVER classify a mood without first asking for clarification about the
|
|
| 111 |
prompt += f"User: {message}\nLLM Response:"
|
| 112 |
return prompt
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
async def text_to_speech(text):
|
| 115 |
communicate = edge_tts.Communicate(text)
|
| 116 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
|
|
|
| 22 |
if word in input_string:
|
| 23 |
return word, True
|
| 24 |
return None, False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
def format_prompt(self, message, history):
|
| 26 |
fixed_prompt = """You are a mood classification chatbot. Your ONLY goal is to accurately classify the user's mood into one of four categories: Happy, Sad, Instrumental, or Party. Follow these rules strictly:
|
| 27 |
|
|
|
|
| 82 |
prompt += f"User: {message}\nLLM Response:"
|
| 83 |
return prompt
|
| 84 |
|
| 85 |
+
def generate(prompt, history, temperature=0.1, max_new_tokens=2048, top_p=0.8, repetition_penalty=1.0):
|
| 86 |
+
temperature = float(temperature)
|
| 87 |
+
if temperature < 1e-2:
|
| 88 |
+
temperature = 1e-2
|
| 89 |
+
top_p = float(top_p)
|
| 90 |
+
|
| 91 |
+
generate_kwargs = dict(
|
| 92 |
+
temperature=temperature,
|
| 93 |
+
max_new_tokens=max_new_tokens,
|
| 94 |
+
top_p=top_p,
|
| 95 |
+
repetition_penalty=repetition_penalty,
|
| 96 |
+
do_sample=True,
|
| 97 |
+
seed=42,
|
| 98 |
+
)
|
| 99 |
+
|
| 100 |
+
formatted_prompt = format_prompt(prompt, history)
|
| 101 |
+
|
| 102 |
+
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
| 103 |
+
output = ""
|
| 104 |
+
|
| 105 |
+
for response in stream:
|
| 106 |
+
output += response.token.text
|
| 107 |
+
mood, is_classified = classify_mood(output)
|
| 108 |
+
if is_classified:
|
| 109 |
+
playlist_message = f"Playing {mood.capitalize()} playlist for you!"
|
| 110 |
+
return playlist_message
|
| 111 |
+
return output
|
| 112 |
+
|
| 113 |
+
|
| 114 |
async def text_to_speech(text):
|
| 115 |
communicate = edge_tts.Communicate(text)
|
| 116 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|