AreejMehboob commited on
Commit
6b43b0b
·
verified ·
1 Parent(s): 40c0bf4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -5
app.py CHANGED
@@ -1,13 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import gradio as gr
3
  import numpy as np
4
  from transformers import AutoTokenizer, AutoModel
5
  import time
 
 
6
  # :white_check_mark: Setup environment
7
  os.makedirs(os.environ.get("HF_HOME", "./hf_cache"), exist_ok=True)
8
  hf_token = os.environ.get("HF_TOKEN")
9
  if not hf_token:
10
  raise EnvironmentError(":x: Environment variable HF_TOKEN is not set.")
 
 
 
 
 
11
  # :white_check_mark: Load model and tokenizer
12
  text_tokenizer = AutoTokenizer.from_pretrained(
13
  "nomic-ai/nomic-embed-text-v1.5",
@@ -20,23 +79,27 @@ text_model = AutoModel.from_pretrained(
20
  trust_remote_code=True,
21
  token=hf_token,
22
  cache_dir=os.environ["HF_HOME"]
23
- )
 
24
  # :white_check_mark: Embedding function
25
  def get_text_embeddings(text):
26
  """
27
  Converts input text into a dense embedding using the Nomic embedding model.
28
  These embeddings are used to query Qdrant for semantically relevant document chunks.
29
  """
30
- inputs = text_tokenizer(text, return_tensors="pt", padding=True, truncation=True)
31
- outputs = text_model(**inputs)
 
32
  embeddings = outputs.last_hidden_state.mean(dim=1)
33
- return embeddings[0].detach().numpy()
 
34
  # :white_check_mark: Gradio interface function
35
  def embed_text_interface(text):
36
- strt_time=time.time()
37
  embedding = get_text_embeddings(text)
38
  print(f"Total time taken by nomic to embed: {time.time()-strt_time}")
39
  return str(embedding)
 
40
  # :white_check_mark: Gradio UI
41
  interface = gr.Interface(
42
  fn=embed_text_interface,
@@ -45,6 +108,7 @@ interface = gr.Interface(
45
  title="Text Embedding with Nomic AI",
46
  description="Enter some text, and get its embedding vector using Nomic's embedding model."
47
  )
 
48
  # :white_check_mark: Launch the app
49
  if __name__ == "__main__":
50
  interface.launch()
 
1
+ # import os
2
+ # import gradio as gr
3
+ # import numpy as np
4
+ # from transformers import AutoTokenizer, AutoModel
5
+ # import time
6
+ # # :white_check_mark: Setup environment
7
+ # os.makedirs(os.environ.get("HF_HOME", "./hf_cache"), exist_ok=True)
8
+ # hf_token = os.environ.get("HF_TOKEN")
9
+ # if not hf_token:
10
+ # raise EnvironmentError(":x: Environment variable HF_TOKEN is not set.")
11
+ # # :white_check_mark: Load model and tokenizer
12
+ # text_tokenizer = AutoTokenizer.from_pretrained(
13
+ # "nomic-ai/nomic-embed-text-v1.5",
14
+ # trust_remote_code=True,
15
+ # token=hf_token,
16
+ # cache_dir=os.environ["HF_HOME"]
17
+ # )
18
+ # text_model = AutoModel.from_pretrained(
19
+ # "nomic-ai/nomic-embed-text-v1.5",
20
+ # trust_remote_code=True,
21
+ # token=hf_token,
22
+ # cache_dir=os.environ["HF_HOME"]
23
+ # )
24
+ # # :white_check_mark: Embedding function
25
+ # def get_text_embeddings(text):
26
+ # """
27
+ # Converts input text into a dense embedding using the Nomic embedding model.
28
+ # These embeddings are used to query Qdrant for semantically relevant document chunks.
29
+ # """
30
+ # inputs = text_tokenizer(text, return_tensors="pt", padding=True, truncation=True)
31
+ # outputs = text_model(**inputs)
32
+ # embeddings = outputs.last_hidden_state.mean(dim=1)
33
+ # return embeddings[0].detach().numpy()
34
+ # # :white_check_mark: Gradio interface function
35
+ # def embed_text_interface(text):
36
+ # strt_time=time.time()
37
+ # embedding = get_text_embeddings(text)
38
+ # print(f"Total time taken by nomic to embed: {time.time()-strt_time}")
39
+ # return str(embedding)
40
+ # # :white_check_mark: Gradio UI
41
+ # interface = gr.Interface(
42
+ # fn=embed_text_interface,
43
+ # inputs=gr.Textbox(label="Enter text to embed", lines=5),
44
+ # outputs=gr.Textbox(label="Embedding vector"),
45
+ # title="Text Embedding with Nomic AI",
46
+ # description="Enter some text, and get its embedding vector using Nomic's embedding model."
47
+ # )
48
+ # # :white_check_mark: Launch the app
49
+ # if __name__ == "__main__":
50
+ # interface.launch()
51
+
52
+
53
  import os
54
  import gradio as gr
55
  import numpy as np
56
  from transformers import AutoTokenizer, AutoModel
57
  import time
58
+ import torch
59
+
60
  # :white_check_mark: Setup environment
61
  os.makedirs(os.environ.get("HF_HOME", "./hf_cache"), exist_ok=True)
62
  hf_token = os.environ.get("HF_TOKEN")
63
  if not hf_token:
64
  raise EnvironmentError(":x: Environment variable HF_TOKEN is not set.")
65
+
66
+ # Check for GPU availability
67
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
68
+ print(f"Using device: {device}")
69
+
70
  # :white_check_mark: Load model and tokenizer
71
  text_tokenizer = AutoTokenizer.from_pretrained(
72
  "nomic-ai/nomic-embed-text-v1.5",
 
79
  trust_remote_code=True,
80
  token=hf_token,
81
  cache_dir=os.environ["HF_HOME"]
82
+ ).to(device) # Move model to GPU if available
83
+
84
  # :white_check_mark: Embedding function
85
  def get_text_embeddings(text):
86
  """
87
  Converts input text into a dense embedding using the Nomic embedding model.
88
  These embeddings are used to query Qdrant for semantically relevant document chunks.
89
  """
90
+ inputs = text_tokenizer(text, return_tensors="pt", padding=True, truncation=True).to(device) # Move inputs to same device as model
91
+ with torch.no_grad(): # Disable gradient calculation for inference
92
+ outputs = text_model(**inputs)
93
  embeddings = outputs.last_hidden_state.mean(dim=1)
94
+ return embeddings[0].cpu().numpy() # Move back to CPU for numpy conversion
95
+
96
  # :white_check_mark: Gradio interface function
97
  def embed_text_interface(text):
98
+ strt_time = time.time()
99
  embedding = get_text_embeddings(text)
100
  print(f"Total time taken by nomic to embed: {time.time()-strt_time}")
101
  return str(embedding)
102
+
103
  # :white_check_mark: Gradio UI
104
  interface = gr.Interface(
105
  fn=embed_text_interface,
 
108
  title="Text Embedding with Nomic AI",
109
  description="Enter some text, and get its embedding vector using Nomic's embedding model."
110
  )
111
+
112
  # :white_check_mark: Launch the app
113
  if __name__ == "__main__":
114
  interface.launch()