Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -7,9 +7,17 @@ import torch
|
|
7 |
|
8 |
# STEP 2 FROM SEMANTIC SEARCH
|
9 |
# Open the water_cycle.txt file in read mode with UTF-8 encoding
|
10 |
-
with open("
|
11 |
# Read the entire contents of the file and store it in a variable
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
# STEP 3 FROM SEMANTIC SEARCH
|
15 |
def preprocess_text(text):
|
@@ -37,7 +45,9 @@ def preprocess_text(text):
|
|
37 |
return cleaned_chunks
|
38 |
|
39 |
# Call the preprocess_text function and store the result in a cleaned_chunks variable
|
40 |
-
|
|
|
|
|
41 |
|
42 |
#STEP 4 FROM SEMANTIC SEARCH
|
43 |
# Load the pre-trained embedding model that converts text to vectors
|
@@ -59,7 +69,9 @@ def create_embeddings(text_chunks):
|
|
59 |
return chunk_embeddings
|
60 |
|
61 |
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
|
62 |
-
|
|
|
|
|
63 |
|
64 |
#STEP 5 FROM SEMANTIC SEARCH
|
65 |
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
|
@@ -96,21 +108,18 @@ def get_top_chunks(query, chunk_embeddings, text_chunks):
|
|
96 |
# Return the list of most relevant chunks
|
97 |
return top_chunks
|
98 |
|
99 |
-
print(get_top_chunks("How are you", chunk_embeddings, cleaned_chunks))
|
100 |
-
|
101 |
# STEP 6 FROM SEMANTIC SEARCH
|
102 |
# Call the get_top_chunks function with the original query
|
103 |
-
|
104 |
-
|
105 |
-
#
|
106 |
-
print(top_results)
|
107 |
-
|
108 |
|
109 |
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
|
110 |
|
111 |
def respond(message, history):
|
112 |
-
|
113 |
-
|
|
|
114 |
str_chunks = "\n".join(best_chunks)
|
115 |
messages = [{"role": "system", "content": f"You are chatbot that plays the role of the user's mom. Base your response on the provided context: {str_chunks}"},
|
116 |
{"role": "user",
|
|
|
7 |
|
8 |
# STEP 2 FROM SEMANTIC SEARCH
|
9 |
# Open the water_cycle.txt file in read mode with UTF-8 encoding
|
10 |
+
with open("cool_mom_phrases.txt", "r", encoding="utf-8") as file:
|
11 |
# Read the entire contents of the file and store it in a variable
|
12 |
+
cool_mom_text = file.read()
|
13 |
+
|
14 |
+
with open("tutor_mom_phrases.txt", "r", encoding="utf-8") as file:
|
15 |
+
# Read the entire contents of the file and store it in a variable
|
16 |
+
tutor_mom_text = file.read()
|
17 |
+
|
18 |
+
with open("strict_mom_phrases.txt", "r", encoding="utf-8") as file:
|
19 |
+
# Read the entire contents of the file and store it in a variable
|
20 |
+
strict_mom_text = file.read()
|
21 |
|
22 |
# STEP 3 FROM SEMANTIC SEARCH
|
23 |
def preprocess_text(text):
|
|
|
45 |
return cleaned_chunks
|
46 |
|
47 |
# Call the preprocess_text function and store the result in a cleaned_chunks variable
|
48 |
+
cleaned_cool_chunks = preprocess_text(cool_mom_text) # Complete this line
|
49 |
+
cleaned_tutor_chunks = preprocess_text(tutor_mom_text)
|
50 |
+
cleaned_strict_chunks = preprocess_text(strict_mom_text)
|
51 |
|
52 |
#STEP 4 FROM SEMANTIC SEARCH
|
53 |
# Load the pre-trained embedding model that converts text to vectors
|
|
|
69 |
return chunk_embeddings
|
70 |
|
71 |
# Call the create_embeddings function and store the result in a new chunk_embeddings variable
|
72 |
+
cool_chunk_embeddings = create_embeddings(cleaned_cool_chunks) # Complete this line
|
73 |
+
tutor_chunk_embeddings = create_embeddings(cleaned_tutor_chunks)
|
74 |
+
strict_chunk_embeddings = create_embeddings(cleaned_strict_chunks)
|
75 |
|
76 |
#STEP 5 FROM SEMANTIC SEARCH
|
77 |
# Define a function to find the most relevant text chunks for a given query, chunk_embeddings, and text_chunks
|
|
|
108 |
# Return the list of most relevant chunks
|
109 |
return top_chunks
|
110 |
|
|
|
|
|
111 |
# STEP 6 FROM SEMANTIC SEARCH
|
112 |
# Call the get_top_chunks function with the original query
|
113 |
+
#top_cool_results = get_top_chunks(message, cool_chunk_embeddings, cleaned_cool_chunks) # Complete this line
|
114 |
+
#top_tutor_results = get_top_chunks(message, tutor_chunk_embeddings, cleaned_tutor_chunks)
|
115 |
+
#top_tiger_results = get_top_chunks(message, tiger_chunk_embeddings, cleaned_tiger_chunks)
|
|
|
|
|
116 |
|
117 |
client = InferenceClient("Qwen/Qwen2.5-72B-Instruct")
|
118 |
|
119 |
def respond(message, history):
|
120 |
+
top_cool_results = get_top_chunks(message, cool_chunk_embeddings, cleaned_cool_chunks) # Complete this line
|
121 |
+
top_tutor_results = get_top_chunks(message, tutor_chunk_embeddings, cleaned_tutor_chunks)
|
122 |
+
top_strict_results = get_top_chunks(message, strict_chunk_embeddings, cleaned_strict_chunks)
|
123 |
str_chunks = "\n".join(best_chunks)
|
124 |
messages = [{"role": "system", "content": f"You are chatbot that plays the role of the user's mom. Base your response on the provided context: {str_chunks}"},
|
125 |
{"role": "user",
|