Spaces:
Sleeping
Sleeping
Rob Learsch commited on
Commit ·
c6204c3
1
Parent(s): 72171c0
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,13 +27,13 @@ def return_image(artist):
|
|
| 27 |
return "gemma.png"
|
| 28 |
return "radiohead.png"
|
| 29 |
|
| 30 |
-
def find_most_relevant_lyric(lyrics, user_input):
|
| 31 |
-
user_doc = nlp(user_input)
|
| 32 |
-
best_match = max(lyrics, key=lambda lyric: user_doc.similarity(nlp(lyric)))
|
| 33 |
-
return best_match
|
| 34 |
-
|
| 35 |
-
def stitch_lyrics(lyrics, line_number=1):
|
| 36 |
-
return [lyrics[i] + " " + lyrics[i + line_number] for i in range(len(lyrics) - line_number)]
|
| 37 |
|
| 38 |
# Load lyrics from a text file
|
| 39 |
def load_lyrics(filename):
|
|
@@ -137,14 +137,26 @@ def chat_with_musician(user_input, history, artist):
|
|
| 137 |
return f"Error: {str(e)}"
|
| 138 |
|
| 139 |
lyric_response = artist_response(gemma_response, artist)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
history.append({"role": "user", "content": user_input})
|
| 142 |
history.append({"role": "assistant", "content": lyric_response})
|
| 143 |
artist_history.append(artist) # Store the previous artist for the next call
|
| 144 |
artist_history[:] = artist_history[-10:] # Keep only last 10 entries
|
| 145 |
-
|
| 146 |
-
#if artist == "Google Gemma":
|
| 147 |
-
# lyric_response = gemma_response
|
| 148 |
return lyric_response
|
| 149 |
|
| 150 |
HF_API_KEY = os.environ["HF_API_KEY"]
|
|
@@ -188,6 +200,7 @@ client = InferenceClient(#provider="hf-inference",
|
|
| 188 |
#system_message = "Please limit your response to only a few sentences."
|
| 189 |
#system_message = "Be creative and imprecise with your word choice. And don't be too repetitive. Please limit your respone to only a few sentences."
|
| 190 |
system_message = "Don't be too repetitive. Please limit your respone to only a few sentences."
|
|
|
|
| 191 |
#Function to generate chatbot responses
|
| 192 |
|
| 193 |
artist_history = [""] # Variable to keep track of the previous artist
|
|
|
|
| 27 |
return "gemma.png"
|
| 28 |
return "radiohead.png"
|
| 29 |
|
| 30 |
+
#def find_most_relevant_lyric(lyrics, user_input):
|
| 31 |
+
# user_doc = nlp(user_input)
|
| 32 |
+
# best_match = max(lyrics, key=lambda lyric: user_doc.similarity(nlp(lyric)))
|
| 33 |
+
# return best_match
|
| 34 |
+
#
|
| 35 |
+
#def stitch_lyrics(lyrics, line_number=1):
|
| 36 |
+
# return [lyrics[i] + " " + lyrics[i + line_number] for i in range(len(lyrics) - line_number)]
|
| 37 |
|
| 38 |
# Load lyrics from a text file
|
| 39 |
def load_lyrics(filename):
|
|
|
|
| 137 |
return f"Error: {str(e)}"
|
| 138 |
|
| 139 |
lyric_response = artist_response(gemma_response, artist)
|
| 140 |
+
if lyric_response == messages[-2]["content"]:
|
| 141 |
+
# If the response is the same as the last one, get a new one:
|
| 142 |
+
messages[-1]={"role": "user", "content": system_message_repeated + "\n\n" + user_input}
|
| 143 |
+
try:
|
| 144 |
+
response = client.chat_completion(
|
| 145 |
+
messages=messages,
|
| 146 |
+
model="google/gemma-2-2b-it",
|
| 147 |
+
max_tokens=256,
|
| 148 |
+
temperature=0.75,
|
| 149 |
+
#top_p=0.9
|
| 150 |
+
)
|
| 151 |
+
gemma_response= response["choices"][0]["message"]["content"]
|
| 152 |
+
except Exception as e:
|
| 153 |
+
return f"Error: {str(e)}"
|
| 154 |
+
lyric_response = artist_response(gemma_response, artist)
|
| 155 |
|
| 156 |
history.append({"role": "user", "content": user_input})
|
| 157 |
history.append({"role": "assistant", "content": lyric_response})
|
| 158 |
artist_history.append(artist) # Store the previous artist for the next call
|
| 159 |
artist_history[:] = artist_history[-10:] # Keep only last 10 entries
|
|
|
|
|
|
|
|
|
|
| 160 |
return lyric_response
|
| 161 |
|
| 162 |
HF_API_KEY = os.environ["HF_API_KEY"]
|
|
|
|
| 200 |
#system_message = "Please limit your response to only a few sentences."
|
| 201 |
#system_message = "Be creative and imprecise with your word choice. And don't be too repetitive. Please limit your respone to only a few sentences."
|
| 202 |
system_message = "Don't be too repetitive. Please limit your respone to only a few sentences."
|
| 203 |
+
system_message_repeated = "Be creative and imprecise with your word choice. Limit your response to a few sentences"
|
| 204 |
#Function to generate chatbot responses
|
| 205 |
|
| 206 |
artist_history = [""] # Variable to keep track of the previous artist
|