Spaces:
Running
Running
Patryk Ptasiński
commited on
Commit
·
eca0fa7
1
Parent(s):
7d3e0ef
Allow any Hugging Face model in API with trust_remote_code=False for unlisted models
Browse files
CLAUDE.md
CHANGED
|
@@ -40,8 +40,9 @@ The application consists of a single `app.py` file with:
|
|
| 40 |
|
| 41 |
- **Queue**: Hugging Face Spaces enforces queuing at infrastructure level, even without `.queue()` in code
|
| 42 |
- **CPU Mode**: Explicitly set to CPU to avoid GPU requirements
|
| 43 |
-
- **Trust Remote Code**:
|
| 44 |
-
- **
|
|
|
|
| 45 |
|
| 46 |
## API Usage
|
| 47 |
|
|
|
|
| 40 |
|
| 41 |
- **Queue**: Hugging Face Spaces enforces queuing at infrastructure level, even without `.queue()` in code
|
| 42 |
- **CPU Mode**: Explicitly set to CPU to avoid GPU requirements
|
| 43 |
+
- **Trust Remote Code**: Only predefined models in MODELS dict allow `trust_remote_code=True`
|
| 44 |
+
- **Any HF Model**: API accepts any Hugging Face model name but uses `trust_remote_code=False` for unlisted models
|
| 45 |
+
- **API Access**: Direct HTTP available via FastAPI endpoints
|
| 46 |
|
| 47 |
## API Usage
|
| 48 |
|
app.py
CHANGED
|
@@ -34,12 +34,16 @@ current_model_name = "nomic-ai/nomic-embed-text-v1.5"
|
|
| 34 |
def load_model(model_name: str):
|
| 35 |
global loaded_models
|
| 36 |
if model_name not in loaded_models:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
return loaded_models[model_name]
|
| 44 |
|
| 45 |
# Load default model
|
|
@@ -50,9 +54,12 @@ fastapi_app = FastAPI()
|
|
| 50 |
|
| 51 |
|
| 52 |
def embed(document: str, model_name: str = None):
|
| 53 |
-
if model_name
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
| 56 |
return model.encode(document)
|
| 57 |
|
| 58 |
|
|
@@ -70,11 +77,11 @@ async def embed_text(data: Dict[str, Any]):
|
|
| 70 |
content={"error": "No text provided"}
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
if model_name not in MODELS:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
)
|
| 78 |
|
| 79 |
# Generate embedding
|
| 80 |
embedding = embed(text, model_name)
|
|
@@ -83,7 +90,9 @@ async def embed_text(data: Dict[str, Any]):
|
|
| 83 |
content={
|
| 84 |
"embedding": embedding.tolist(),
|
| 85 |
"dim": len(embedding),
|
| 86 |
-
"model": model_name
|
|
|
|
|
|
|
| 87 |
}
|
| 88 |
)
|
| 89 |
except Exception as e:
|
|
@@ -117,12 +126,13 @@ with gr.Blocks(title="Multi-Model Text Embeddings", css="""
|
|
| 117 |
gr.Markdown("# Multi-Model Text Embeddings")
|
| 118 |
gr.Markdown("Generate embeddings for your text using 16+ state-of-the-art embedding models from Nomic, BGE, Snowflake, IBM Granite, and more.")
|
| 119 |
|
| 120 |
-
# Model selector dropdown
|
| 121 |
model_dropdown = gr.Dropdown(
|
| 122 |
choices=list(MODELS.keys()),
|
| 123 |
value=current_model_name,
|
| 124 |
label="Select Embedding Model",
|
| 125 |
-
info="Choose
|
|
|
|
| 126 |
)
|
| 127 |
|
| 128 |
# Create an input text box
|
|
@@ -143,6 +153,8 @@ with gr.Blocks(title="Multi-Model Text Embeddings", css="""
|
|
| 143 |
gr.Markdown("""
|
| 144 |
You can use this API in two ways: via the direct FastAPI endpoint or through Gradio clients.
|
| 145 |
|
|
|
|
|
|
|
| 146 |
### List Available Models
|
| 147 |
```bash
|
| 148 |
curl https://ipepe-nomic-embeddings.hf.space/models
|
|
@@ -155,10 +167,15 @@ with gr.Blocks(title="Multi-Model Text Embeddings", css="""
|
|
| 155 |
-H "Content-Type: application/json" \
|
| 156 |
-d '{"text": "Your text to embed goes here"}'
|
| 157 |
|
| 158 |
-
# With
|
| 159 |
curl -X POST https://ipepe-nomic-embeddings.hf.space/embed \
|
| 160 |
-H "Content-Type: application/json" \
|
| 161 |
-d '{"text": "Your text to embed goes here", "model": "sentence-transformers/all-MiniLM-L6-v2"}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
```
|
| 163 |
|
| 164 |
Response format:
|
|
@@ -166,7 +183,9 @@ with gr.Blocks(title="Multi-Model Text Embeddings", css="""
|
|
| 166 |
{
|
| 167 |
"embedding": [0.123, -0.456, ...],
|
| 168 |
"dim": 384,
|
| 169 |
-
"model": "sentence-transformers/all-MiniLM-L6-v2"
|
|
|
|
|
|
|
| 170 |
}
|
| 171 |
```
|
| 172 |
|
|
|
|
| 34 |
def load_model(model_name: str):
|
| 35 |
global loaded_models
|
| 36 |
if model_name not in loaded_models:
|
| 37 |
+
# Only allow trust_remote_code=True for predefined models
|
| 38 |
+
trust_remote_code = MODELS.get(model_name, {}).get("trust_remote_code", False)
|
| 39 |
+
try:
|
| 40 |
+
loaded_models[model_name] = SentenceTransformer(
|
| 41 |
+
model_name,
|
| 42 |
+
trust_remote_code=trust_remote_code,
|
| 43 |
+
device='cpu'
|
| 44 |
+
)
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise ValueError(f"Failed to load model '{model_name}': {str(e)}")
|
| 47 |
return loaded_models[model_name]
|
| 48 |
|
| 49 |
# Load default model
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
def embed(document: str, model_name: str = None):
|
| 57 |
+
if model_name:
|
| 58 |
+
try:
|
| 59 |
+
selected_model = load_model(model_name)
|
| 60 |
+
return selected_model.encode(document)
|
| 61 |
+
except Exception as e:
|
| 62 |
+
raise ValueError(f"Error with model '{model_name}': {str(e)}")
|
| 63 |
return model.encode(document)
|
| 64 |
|
| 65 |
|
|
|
|
| 77 |
content={"error": "No text provided"}
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# Allow any model but warn about trust_remote_code
|
| 81 |
if model_name not in MODELS:
|
| 82 |
+
trust_remote_code = False
|
| 83 |
+
else:
|
| 84 |
+
trust_remote_code = MODELS[model_name].get("trust_remote_code", False)
|
|
|
|
| 85 |
|
| 86 |
# Generate embedding
|
| 87 |
embedding = embed(text, model_name)
|
|
|
|
| 90 |
content={
|
| 91 |
"embedding": embedding.tolist(),
|
| 92 |
"dim": len(embedding),
|
| 93 |
+
"model": model_name,
|
| 94 |
+
"trust_remote_code": trust_remote_code,
|
| 95 |
+
"predefined": model_name in MODELS
|
| 96 |
}
|
| 97 |
)
|
| 98 |
except Exception as e:
|
|
|
|
| 126 |
gr.Markdown("# Multi-Model Text Embeddings")
|
| 127 |
gr.Markdown("Generate embeddings for your text using 16+ state-of-the-art embedding models from Nomic, BGE, Snowflake, IBM Granite, and more.")
|
| 128 |
|
| 129 |
+
# Model selector dropdown (allows custom input)
|
| 130 |
model_dropdown = gr.Dropdown(
|
| 131 |
choices=list(MODELS.keys()),
|
| 132 |
value=current_model_name,
|
| 133 |
label="Select Embedding Model",
|
| 134 |
+
info="Choose from predefined models or enter any Hugging Face model name",
|
| 135 |
+
allow_custom_value=True
|
| 136 |
)
|
| 137 |
|
| 138 |
# Create an input text box
|
|
|
|
| 153 |
gr.Markdown("""
|
| 154 |
You can use this API in two ways: via the direct FastAPI endpoint or through Gradio clients.
|
| 155 |
|
| 156 |
+
**Security Note**: Only predefined models allow `trust_remote_code=True`. Any other Hugging Face model will use `trust_remote_code=False` for security.
|
| 157 |
+
|
| 158 |
### List Available Models
|
| 159 |
```bash
|
| 160 |
curl https://ipepe-nomic-embeddings.hf.space/models
|
|
|
|
| 167 |
-H "Content-Type: application/json" \
|
| 168 |
-d '{"text": "Your text to embed goes here"}'
|
| 169 |
|
| 170 |
+
# With predefined model (trust_remote_code allowed)
|
| 171 |
curl -X POST https://ipepe-nomic-embeddings.hf.space/embed \
|
| 172 |
-H "Content-Type: application/json" \
|
| 173 |
-d '{"text": "Your text to embed goes here", "model": "sentence-transformers/all-MiniLM-L6-v2"}'
|
| 174 |
+
|
| 175 |
+
# With any Hugging Face model (trust_remote_code=False for security)
|
| 176 |
+
curl -X POST https://ipepe-nomic-embeddings.hf.space/embed \
|
| 177 |
+
-H "Content-Type: application/json" \
|
| 178 |
+
-d '{"text": "Your text to embed goes here", "model": "intfloat/e5-base-v2"}'
|
| 179 |
```
|
| 180 |
|
| 181 |
Response format:
|
|
|
|
| 183 |
{
|
| 184 |
"embedding": [0.123, -0.456, ...],
|
| 185 |
"dim": 384,
|
| 186 |
+
"model": "sentence-transformers/all-MiniLM-L6-v2",
|
| 187 |
+
"trust_remote_code": false,
|
| 188 |
+
"predefined": true
|
| 189 |
}
|
| 190 |
```
|
| 191 |
|