Spaces:
Sleeping
Sleeping
Rob Learsch commited on
Commit ·
e165667
1
Parent(s): 94e7e9a
Update app.py
Browse files
app.py
CHANGED
|
@@ -174,7 +174,7 @@ def cosine_similarity_int8(query, embeddings):
|
|
| 174 |
|
| 175 |
return dots / (emb_norms * query_norm + 1e-8)
|
| 176 |
|
| 177 |
-
|
| 178 |
|
| 179 |
|
| 180 |
|
|
@@ -208,8 +208,8 @@ all_phrases_grateful_dead = generate_cumulative_phrases(songs_from_text(grateful
|
|
| 208 |
size = 350 #256
|
| 209 |
|
| 210 |
# Initialize Hugging Face Inference Client
|
| 211 |
-
client = InferenceClient(
|
| 212 |
-
|
| 213 |
|
| 214 |
system_message = "Don't be too repetitive. Please limit your response to only a few sentences."
|
| 215 |
|
|
@@ -220,71 +220,71 @@ size = 150
|
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
-
|
| 224 |
-
#
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
# with gr.Blocks() as demo:
|
| 228 |
-
# with gr.Row():
|
| 229 |
-
# with gr.Column(scale=1):
|
| 230 |
-
# artist_dropdown = gr.Dropdown(
|
| 231 |
-
# choices=["Radiohead", "Kendrick Lamar", "Grateful Dead", "Google Gemma"],
|
| 232 |
-
# value="Radiohead",
|
| 233 |
-
# label="Select artist",
|
| 234 |
-
# interactive=True,
|
| 235 |
-
# )
|
| 236 |
-
# artist_image = gr.Image(
|
| 237 |
-
# value=return_image("Radiohead"),
|
| 238 |
-
# label="Thumbnail",
|
| 239 |
-
# height=size,
|
| 240 |
-
# width=size,
|
| 241 |
-
# show_label=False,
|
| 242 |
-
# show_fullscreen_button=False,
|
| 243 |
-
# show_download_button=False,
|
| 244 |
-
# show_share_button=False,
|
| 245 |
-
# )
|
| 246 |
-
# with gr.Column(scale=3):
|
| 247 |
-
# chatbot = gr.Chatbot(height=400, type='messages')
|
| 248 |
-
|
| 249 |
-
# message_input = gr.Textbox(
|
| 250 |
-
# label="Your message",
|
| 251 |
-
# placeholder="Enter a message and press Enter",
|
| 252 |
-
# lines=2,
|
| 253 |
-
# interactive=True,
|
| 254 |
-
# )
|
| 255 |
-
|
| 256 |
-
# # Update image when artist changes
|
| 257 |
-
# artist_dropdown.change(fn=update_artist_image, inputs=artist_dropdown, outputs=artist_image)
|
| 258 |
-
|
| 259 |
-
# # Define how chat works
|
| 260 |
-
# def chatbot_response(message, artist, chat_history):
|
| 261 |
-
# if message is None or message.strip() == "":
|
| 262 |
-
# return chat_history
|
| 263 |
-
# # Call your chat_with_musician function, which should return response text
|
| 264 |
-
# response = chat_with_musician(message, artist)
|
| 265 |
-
# chat_history = chat_history or []
|
| 266 |
-
# chat_history.append((message, response))
|
| 267 |
-
# return chat_history
|
| 268 |
-
|
| 269 |
-
# # Wire message input submit to update chatbot
|
| 270 |
-
# message_input.submit(
|
| 271 |
-
# fn=chatbot_response,
|
| 272 |
-
# inputs=[message_input, artist_dropdown, chatbot],
|
| 273 |
-
# outputs=chatbot,
|
| 274 |
-
# queue=False,
|
| 275 |
-
# ).then(lambda: "", None, message_input) # Clear input after submit
|
| 276 |
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
def respond(message, artist, chat_history):
|
| 290 |
if not message:
|
|
|
|
| 174 |
|
| 175 |
return dots / (emb_norms * query_norm + 1e-8)
|
| 176 |
|
| 177 |
+
HF_API_KEY = os.environ["HF_API_KEY"]
|
| 178 |
|
| 179 |
|
| 180 |
|
|
|
|
| 208 |
size = 350 #256
|
| 209 |
|
| 210 |
# Initialize Hugging Face Inference Client
|
| 211 |
+
client = InferenceClient(token=HF_API_KEY)
|
| 212 |
+
|
| 213 |
|
| 214 |
system_message = "Don't be too repetitive. Please limit your response to only a few sentences."
|
| 215 |
|
|
|
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
+
def update_artist_image(artist):
|
| 224 |
+
# Call your existing function to get the image path or PIL.Image
|
| 225 |
+
return return_image(artist)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 226 |
|
| 227 |
+
with gr.Blocks() as demo:
|
| 228 |
+
with gr.Row():
|
| 229 |
+
with gr.Column(scale=1):
|
| 230 |
+
artist_dropdown = gr.Dropdown(
|
| 231 |
+
choices=["Radiohead", "Kendrick Lamar", "Grateful Dead", "Google Gemma"],
|
| 232 |
+
value="Radiohead",
|
| 233 |
+
label="Select artist",
|
| 234 |
+
interactive=True,
|
| 235 |
+
)
|
| 236 |
+
artist_image = gr.Image(
|
| 237 |
+
value=return_image("Radiohead"),
|
| 238 |
+
label="Thumbnail",
|
| 239 |
+
height=size,
|
| 240 |
+
width=size,
|
| 241 |
+
show_label=False,
|
| 242 |
+
show_fullscreen_button=False,
|
| 243 |
+
show_download_button=False,
|
| 244 |
+
show_share_button=False,
|
| 245 |
+
)
|
| 246 |
+
with gr.Column(scale=1):
|
| 247 |
+
chatbot = gr.Chatbot(height=400, type='messages')
|
| 248 |
+
|
| 249 |
+
message_input = gr.Textbox(
|
| 250 |
+
label="Your message",
|
| 251 |
+
placeholder="Enter a message and press Enter",
|
| 252 |
+
lines=2,
|
| 253 |
+
interactive=True,
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
# Update image when artist changes
|
| 257 |
+
artist_dropdown.change(fn=update_artist_image, inputs=artist_dropdown, outputs=artist_image)
|
| 258 |
+
|
| 259 |
+
# Define how chat works
|
| 260 |
+
def chatbot_response(message, artist, chat_history):
|
| 261 |
+
if message is None or message.strip() == "":
|
| 262 |
+
return chat_history
|
| 263 |
+
# Call your chat_with_musician function, which should return response text
|
| 264 |
+
response = chat_with_musician(message, artist)
|
| 265 |
+
chat_history = chat_history or []
|
| 266 |
+
chat_history.append((message, response))
|
| 267 |
+
return chat_history
|
| 268 |
+
|
| 269 |
+
# Wire message input submit to update chatbot
|
| 270 |
+
message_input.submit(
|
| 271 |
+
fn=chatbot_response,
|
| 272 |
+
inputs=[message_input, artist_dropdown, chatbot],
|
| 273 |
+
outputs=chatbot,
|
| 274 |
+
queue=False,
|
| 275 |
+
).then(lambda: "", None, message_input) # Clear input after submit
|
| 276 |
+
|
| 277 |
+
# def return_image(artist):
|
| 278 |
+
# # For test: return a colored image based on artist
|
| 279 |
+
# colors = {
|
| 280 |
+
# "Radiohead": (255, 0, 0),
|
| 281 |
+
# "Kendrick Lamar": (0, 255, 0),
|
| 282 |
+
# "Grateful Dead": (0, 0, 255),
|
| 283 |
+
# "Google Gemma": (255, 255, 0)
|
| 284 |
+
# }
|
| 285 |
+
# color = colors.get(artist, (128, 128, 128))
|
| 286 |
+
# img = Image.new('RGB', (150, 150), color=color)
|
| 287 |
+
# return img
|
| 288 |
|
| 289 |
def respond(message, artist, chat_history):
|
| 290 |
if not message:
|