antreaspiece commited on
Commit
4869609
·
verified ·
1 Parent(s): be372e1

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +34 -19
README.md CHANGED
@@ -33,7 +33,31 @@ pip install torch sentence-transformers transformers
33
 
34
  ## Usage
35
 
36
- ### Basic Loading and Encoding
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
  ```python
39
  import torch
@@ -97,25 +121,14 @@ print(f"Embeddings shape: {embeddings.shape}")
97
  ### Query-Tag Retrieval Example
98
 
99
  ```python
 
100
  import torch
101
  from typing import List
102
 
103
- def encode_texts(model, texts: List[str], max_length: int = 512):
104
- """Encode texts with proper Identity projection handling."""
105
- with torch.no_grad():
106
- if hasattr(model.backbone, 'projection'):
107
- proj = model.backbone.projection
108
- is_identity = (
109
- isinstance(proj, torch.nn.Identity) or
110
- 'Identity' in proj.__class__.__name__
111
- )
112
- if is_identity:
113
- return model.backbone.transformer.encode_texts(
114
- texts, max_length=max_length, return_dict=False
115
- )
116
- return model.backbone.encode_texts(
117
- texts, max_length=max_length, return_dict=False
118
- )
119
 
120
  def compute_similarities(query_embeddings, tag_embeddings):
121
  """Compute cosine similarities between queries and tags."""
@@ -136,8 +149,8 @@ tags = [
136
  ]
137
 
138
  # Encode queries and tags
139
- query_embeddings = encode_texts(model, queries)
140
- tag_embeddings = encode_texts(model, tags)
141
 
142
  # Compute similarities
143
  similarities = compute_similarities(query_embeddings, tag_embeddings)
@@ -156,6 +169,8 @@ for query_idx, query in enumerate(queries):
156
  print(f" {rank}. {tags[tag_idx]} (similarity: {sim:.4f})")
157
  ```
158
 
 
 
159
  ## Model Architecture
160
 
161
  - **Transformer Layers**: 2 (pruned from 24, keeping last 2 layers)
 
33
 
34
  ## Usage
35
 
36
+ ### Simple Usage with SentenceTransformers (Recommended)
37
+
38
+ The easiest way to use this model is with the `sentence-transformers` library:
39
+
40
+ ```python
41
+ from sentence_transformers import SentenceTransformer
42
+ import torch
43
+
44
+ # Load the model
45
+ model = SentenceTransformer(
46
+ "Pieces/embeddinggemma-300m-distilled-depth10pct-10-768dim-best",
47
+ device="cuda" if torch.cuda.is_available() else "cpu"
48
+ )
49
+
50
+ # Encode texts
51
+ texts = ["Hello world", "This is a test"]
52
+ embeddings = model.encode(texts, convert_to_tensor=True)
53
+
54
+ print(f"Embeddings shape: {embeddings.shape}")
55
+ # Output: torch.Size([2, 768])
56
+ ```
57
+
58
+ ### Advanced Usage (Full Model Access)
59
+
60
+ If you need access to the full model structure:
61
 
62
  ```python
63
  import torch
 
121
  ### Query-Tag Retrieval Example
122
 
123
  ```python
124
+ from sentence_transformers import SentenceTransformer
125
  import torch
126
  from typing import List
127
 
128
+ # Load model
129
+ model = SentenceTransformer(
130
+ "Pieces/embeddinggemma-300m-distilled-depth10pct-10-768dim-best"
131
+ )
 
 
 
 
 
 
 
 
 
 
 
 
132
 
133
  def compute_similarities(query_embeddings, tag_embeddings):
134
  """Compute cosine similarities between queries and tags."""
 
149
  ]
150
 
151
  # Encode queries and tags
152
+ query_embeddings = model.encode(queries, convert_to_tensor=True)
153
+ tag_embeddings = model.encode(tags, convert_to_tensor=True)
154
 
155
  # Compute similarities
156
  similarities = compute_similarities(query_embeddings, tag_embeddings)
 
169
  print(f" {rank}. {tags[tag_idx]} (similarity: {sim:.4f})")
170
  ```
171
 
172
+ See `usage_example.py` in this repository for a complete standalone example.
173
+
174
  ## Model Architecture
175
 
176
  - **Transformer Layers**: 2 (pruned from 24, keeping last 2 layers)