Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -16,28 +16,33 @@ try:
|
|
16 |
except Exception as e:
|
17 |
print(f"Failed to load models: {e}")
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
def load_and_preprocess_text(filename):
|
20 |
"""
|
21 |
Load and preprocess text from a file, removing empty lines and stripping whitespace.
|
22 |
"""
|
23 |
try:
|
24 |
with open(filename, 'r', encoding='utf-8') as file:
|
25 |
-
segments = [line.strip() for line in file if line.strip()]
|
26 |
print("Text loaded and preprocessed successfully.")
|
27 |
return segments
|
28 |
except Exception as e:
|
29 |
print(f"Failed to load or preprocess text: {e}")
|
30 |
return []
|
31 |
|
32 |
-
segments = load_and_preprocess_text(
|
33 |
|
34 |
def find_relevant_segments(user_query, segments, top_k=5):
|
35 |
-
"""
|
36 |
-
Find the top-k most relevant text segments for a user's query using cosine similarity among sentence embeddings.
|
37 |
-
"""
|
38 |
try:
|
39 |
-
#
|
40 |
-
lower_query = user_query
|
41 |
# Encode the query and the segments
|
42 |
query_embedding = retrieval_model.encode(lower_query)
|
43 |
segment_embeddings = retrieval_model.encode(segments)
|
@@ -51,6 +56,7 @@ def find_relevant_segments(user_query, segments, top_k=5):
|
|
51 |
print(f"Error in finding relevant segments: {e}")
|
52 |
return []
|
53 |
|
|
|
54 |
def generate_response(user_query, relevant_segments):
|
55 |
"""
|
56 |
Generate a response providing song recommendations based on mood.
|
|
|
16 |
except Exception as e:
|
17 |
print(f"Failed to load models: {e}")
|
18 |
|
19 |
+
def preprocess_text(text):
|
20 |
+
"""
|
21 |
+
Preprocess text by lowercasing and removing special characters.
|
22 |
+
"""
|
23 |
+
text = text.lower()
|
24 |
+
text = re.sub(r'[^a-z0-9\s]', '', text)
|
25 |
+
return text
|
26 |
+
|
27 |
def load_and_preprocess_text(filename):
|
28 |
"""
|
29 |
Load and preprocess text from a file, removing empty lines and stripping whitespace.
|
30 |
"""
|
31 |
try:
|
32 |
with open(filename, 'r', encoding='utf-8') as file:
|
33 |
+
segments = [preprocess_text(line.strip()) for line in file if line.strip()]
|
34 |
print("Text loaded and preprocessed successfully.")
|
35 |
return segments
|
36 |
except Exception as e:
|
37 |
print(f"Failed to load or preprocess text: {e}")
|
38 |
return []
|
39 |
|
40 |
+
segments = load_and_preprocess_text(filena
|
41 |
|
42 |
def find_relevant_segments(user_query, segments, top_k=5):
|
|
|
|
|
|
|
43 |
try:
|
44 |
+
# Preprocess and lowercase the query for better matching
|
45 |
+
lower_query = preprocess_text(user_query)
|
46 |
# Encode the query and the segments
|
47 |
query_embedding = retrieval_model.encode(lower_query)
|
48 |
segment_embeddings = retrieval_model.encode(segments)
|
|
|
56 |
print(f"Error in finding relevant segments: {e}")
|
57 |
return []
|
58 |
|
59 |
+
|
60 |
def generate_response(user_query, relevant_segments):
|
61 |
"""
|
62 |
Generate a response providing song recommendations based on mood.
|