Spaces:
Sleeping
Sleeping
Rob Learsch commited on
Commit ·
84c55de
1
Parent(s): e989faa
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,8 +5,10 @@ import os
|
|
| 5 |
from google import genai
|
| 6 |
from google.genai import types
|
| 7 |
import spacy
|
| 8 |
-
|
| 9 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 10 |
|
| 11 |
load_dotenv()
|
| 12 |
HF_API_KEY = os.environ["HF_API_KEY"]
|
|
@@ -50,6 +52,14 @@ stitched_radiohead_lyrics = stitch_lyrics(radiohead_lyrics)
|
|
| 50 |
stitched_kendrick_lyrics = stitch_lyrics(kendrick_lyrics)
|
| 51 |
stitched_grateful_dead_lyrics = stitch_lyrics(grateful_dead_lyrics)
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# Initialize Hugging Face Inference Client
|
| 54 |
client = InferenceClient(#provider="hf-inference",
|
| 55 |
token=HF_API_KEY)
|
|
@@ -86,12 +96,19 @@ def chat_with_musician(user_input, history, artist):
|
|
| 86 |
if artist == "Kendrick Lamar":
|
| 87 |
#lyric_response = find_most_relevant_lyric(stitched_kendrick_lyrics,
|
| 88 |
# gemma_response)
|
| 89 |
-
result = client.sentence_similarity(
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
if artist == "Google Gemma":
|
| 96 |
lyric_response = gemma_response
|
| 97 |
return lyric_response
|
|
|
|
| 5 |
from google import genai
|
| 6 |
from google.genai import types
|
| 7 |
import spacy
|
| 8 |
+
from sentence_transformers import SentenceTransformer, SimilarityFunction
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
+
import numpy as np
|
| 11 |
+
|
| 12 |
|
| 13 |
load_dotenv()
|
| 14 |
HF_API_KEY = os.environ["HF_API_KEY"]
|
|
|
|
| 52 |
stitched_kendrick_lyrics = stitch_lyrics(kendrick_lyrics)
|
| 53 |
stitched_grateful_dead_lyrics = stitch_lyrics(grateful_dead_lyrics)
|
| 54 |
|
| 55 |
+
encoder_model = SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2',
|
| 56 |
+
backend='openvino'
|
| 57 |
+
)
|
| 58 |
+
#radiohead_embeddings = encoder_model.encode(stitched_radiohead_lyrics)
|
| 59 |
+
kendrick_embeddings = encoder_model.encode(stitched_kendrick_lyrics)
|
| 60 |
+
#grateful_dead_embeddings = encoder_model.encode(stitched_grateful_dead_lyrics)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
# Initialize Hugging Face Inference Client
|
| 64 |
client = InferenceClient(#provider="hf-inference",
|
| 65 |
token=HF_API_KEY)
|
|
|
|
| 96 |
if artist == "Kendrick Lamar":
|
| 97 |
#lyric_response = find_most_relevant_lyric(stitched_kendrick_lyrics,
|
| 98 |
# gemma_response)
|
| 99 |
+
#result = client.sentence_similarity(
|
| 100 |
+
# gemma_response,
|
| 101 |
+
# other_sentences= stitched_kendrick_lyrics,
|
| 102 |
+
# model="sentence-transformers/all-MiniLM-L6-v2",
|
| 103 |
+
# )
|
| 104 |
+
encoded_gemma = encoder_model.encode(gemma_response)
|
| 105 |
+
similarity_result = encoder_model.similarity(
|
| 106 |
+
encoded_gemma,
|
| 107 |
+
kendrick_embeddings,
|
| 108 |
+
similarity_function=SimilarityFunction.COSINE,
|
| 109 |
+
)
|
| 110 |
+
result_max_index = np.argmax(similarity_result)
|
| 111 |
+
lyric_response = stitched_kendrick_lyrics[result_max_index]
|
| 112 |
if artist == "Google Gemma":
|
| 113 |
lyric_response = gemma_response
|
| 114 |
return lyric_response
|