hrguarinv commited on
Commit
a9e0812
·
verified ·
1 Parent(s): 198fb2d

Update services/utils.py

Browse files
Files changed (1) hide show
  1. services/utils.py +28 -7
services/utils.py CHANGED
@@ -12,7 +12,7 @@ nltk.download('wordnet')
12
  stop_words = set(stopwords.words('english'))
13
  lemmatizer = WordNetLemmatizer()
14
  model = SentenceTransformer('all-mpnet-base-v2')
15
-
16
 
17
  def clean_text(text):
18
  # Lowercase
@@ -28,14 +28,35 @@ def clean_text(text):
28
  cleaned_text = ' '.join(words)
29
  return cleaned_text
30
 
31
-
32
- def encode_and_normalize(text):
33
- vector = model.encode(text)
34
- normalized_vector = vector / np.linalg.norm(vector)
35
- return normalized_vector
36
-
37
  def extract_order_id_from_query(text):
38
  match = re.search(r'\bB-\d+\b', text)
39
  if match:
40
  return match.group(0)
41
  return None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  stop_words = set(stopwords.words('english'))
13
  lemmatizer = WordNetLemmatizer()
14
  model = SentenceTransformer('all-mpnet-base-v2')
15
+ clip_model = SentenceTransformer('clip-ViT-B-32')
16
 
17
  def clean_text(text):
18
  # Lowercase
 
28
  cleaned_text = ' '.join(words)
29
  return cleaned_text
30
 
 
 
 
 
 
 
31
  def extract_order_id_from_query(text):
32
  match = re.search(r'\bB-\d+\b', text)
33
  if match:
34
  return match.group(0)
35
  return None
36
+
37
+ def generate_text_embedding(text):
38
+ try:
39
+ text_embedding = clip_model.encode(text, convert_to_tensor=True)
40
+ return text_embedding.cpu().numpy()
41
+ except Exception as e:
42
+ print(f"Error processing text '{text}': {e}")
43
+ return np.zeros((512,))
44
+
45
+ def generate_image_embedding(image_path):
46
+ try:
47
+ image = Image.open(image_path)
48
+ image = image.convert('RGB')
49
+ image_embedding = clip_model.encode(image, convert_to_tensor=True)
50
+ return image_embedding.cpu().numpy() # Convert to numpy array for easy storage
51
+ except Exception as e:
52
+ print(f"Error processing image from {image_path}: {e}")
53
+ return np.zeros((512,))
54
+
55
+ def clear_chat():
56
+ return []
57
+
58
+
59
+ def undo_last_message(chatbot):
60
+ if chatbot:
61
+ chatbot.pop()
62
+ return chatbot