Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,358 +1,353 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
-
from transformers import AutoTokenizer, AutoModel
|
| 5 |
-
from typing import List, Union
|
| 6 |
-
import json
|
| 7 |
-
import logging
|
| 8 |
-
import os
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
logging.
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
except Exception as e:
|
| 105 |
-
logger.warning(f"Error generating embedding for text: {str(e)}")
|
| 106 |
-
#
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
def
|
| 198 |
-
"""
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
)
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
label="Embedding
|
| 280 |
-
placeholder='["0.1", "0.2", ...]',
|
| 281 |
-
lines=3
|
| 282 |
-
)
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
```
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
#
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
quiet=False
|
| 355 |
-
)
|
| 356 |
-
|
| 357 |
-
if __name__ == "__main__":
|
| 358 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from transformers import AutoTokenizer, AutoModel
|
| 5 |
+
from typing import List, Union
|
| 6 |
+
import json
|
| 7 |
+
import logging
|
| 8 |
+
import os
|
| 9 |
+
import time
|
| 10 |
+
|
| 11 |
+
# Configure logging
|
| 12 |
+
logging.basicConfig(level=logging.INFO)
|
| 13 |
+
logger = logging.getLogger(__name__)
|
| 14 |
+
|
| 15 |
+
# Model configuration
|
| 16 |
+
MODEL_NAME = "Qwen/Qwen3-Embedding-0.6B" # Qwen3 Embedding model
|
| 17 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
MAX_LENGTH = 512
|
| 19 |
+
|
| 20 |
+
# Global variables for model and tokenizer
|
| 21 |
+
model = None
|
| 22 |
+
tokenizer = None
|
| 23 |
+
|
| 24 |
+
def load_model():
|
| 25 |
+
"""Load the Qwen3 embedding model and tokenizer"""
|
| 26 |
+
global model, tokenizer
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
logger.info(f"Loading Qwen3 embedding model on device: {DEVICE}")
|
| 30 |
+
|
| 31 |
+
# Load tokenizer and model for Qwen3 embedding
|
| 32 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
| 33 |
+
model = AutoModel.from_pretrained(
|
| 34 |
+
MODEL_NAME,
|
| 35 |
+
trust_remote_code=True,
|
| 36 |
+
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
|
| 37 |
+
device_map="auto" if DEVICE == "cuda" else None
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if DEVICE == "cpu":
|
| 41 |
+
model = model.to(DEVICE)
|
| 42 |
+
|
| 43 |
+
model.eval()
|
| 44 |
+
|
| 45 |
+
logger.info("Qwen3 embedding model loaded successfully")
|
| 46 |
+
return True
|
| 47 |
+
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logger.error(f"Error loading Qwen3 model: {str(e)}")
|
| 50 |
+
# Try fallback to a simpler approach
|
| 51 |
+
try:
|
| 52 |
+
logger.info("Trying fallback model loading...")
|
| 53 |
+
from sentence_transformers import SentenceTransformer
|
| 54 |
+
model = SentenceTransformer('all-MiniLM-L6-v2')
|
| 55 |
+
tokenizer = None
|
| 56 |
+
logger.info("Fallback model loaded successfully")
|
| 57 |
+
return True
|
| 58 |
+
except Exception as fallback_error:
|
| 59 |
+
logger.error(f"Fallback model loading also failed: {str(fallback_error)}")
|
| 60 |
+
return False
|
| 61 |
+
|
| 62 |
+
def generate_embeddings(texts: Union[str, List[str]]) -> Union[List[float], List[List[float]]]:
|
| 63 |
+
"""Generate embeddings for input text(s) using Qwen3 or fallback model"""
|
| 64 |
+
global model, tokenizer
|
| 65 |
+
|
| 66 |
+
try:
|
| 67 |
+
# Ensure texts is a list
|
| 68 |
+
if isinstance(texts, str):
|
| 69 |
+
texts = [texts]
|
| 70 |
+
single_text = True
|
| 71 |
+
else:
|
| 72 |
+
single_text = False
|
| 73 |
+
|
| 74 |
+
# Truncate texts if too long
|
| 75 |
+
texts = [text[:MAX_LENGTH] for text in texts]
|
| 76 |
+
|
| 77 |
+
embeddings = []
|
| 78 |
+
|
| 79 |
+
for text in texts:
|
| 80 |
+
try:
|
| 81 |
+
# Method 1: Try using the Qwen model directly
|
| 82 |
+
if model and tokenizer:
|
| 83 |
+
inputs = tokenizer(
|
| 84 |
+
text,
|
| 85 |
+
return_tensors="pt",
|
| 86 |
+
padding=True,
|
| 87 |
+
truncation=True,
|
| 88 |
+
max_length=MAX_LENGTH
|
| 89 |
+
).to(DEVICE)
|
| 90 |
+
|
| 91 |
+
with torch.no_grad():
|
| 92 |
+
outputs = model(**inputs)
|
| 93 |
+
# Use mean pooling of last hidden state
|
| 94 |
+
embedding = outputs.last_hidden_state.mean(dim=1).squeeze().cpu().numpy()
|
| 95 |
+
embeddings.append(embedding.tolist())
|
| 96 |
+
|
| 97 |
+
elif model and hasattr(model, 'encode'):
|
| 98 |
+
# Method 2: Using sentence transformer fallback
|
| 99 |
+
embedding = model.encode(text)
|
| 100 |
+
embeddings.append(embedding.tolist())
|
| 101 |
+
else:
|
| 102 |
+
raise Exception("No model available")
|
| 103 |
+
|
| 104 |
+
except Exception as e:
|
| 105 |
+
logger.warning(f"Error generating embedding for text: {str(e)}")
|
| 106 |
+
# Return zero vector as last resort
|
| 107 |
+
embeddings.append([0.0] * 384) # Standard dimension for fallback
|
| 108 |
+
|
| 109 |
+
return embeddings[0] if single_text else embeddings
|
| 110 |
+
|
| 111 |
+
except Exception as e:
|
| 112 |
+
logger.error(f"Error in generate_embeddings: {str(e)}")
|
| 113 |
+
# Return zero vectors as fallback
|
| 114 |
+
if single_text:
|
| 115 |
+
return [0.0] * 384
|
| 116 |
+
else:
|
| 117 |
+
return [[0.0] * 384] * len(texts)
|
| 118 |
+
|
| 119 |
+
def compute_similarity(embedding1: List[float], embedding2: List[float]) -> float:
|
| 120 |
+
"""Compute cosine similarity between two embeddings"""
|
| 121 |
+
try:
|
| 122 |
+
# Convert to numpy arrays
|
| 123 |
+
emb1 = np.array(embedding1)
|
| 124 |
+
emb2 = np.array(embedding2)
|
| 125 |
+
|
| 126 |
+
# Compute cosine similarity
|
| 127 |
+
dot_product = np.dot(emb1, emb2)
|
| 128 |
+
norm1 = np.linalg.norm(emb1)
|
| 129 |
+
norm2 = np.linalg.norm(emb2)
|
| 130 |
+
|
| 131 |
+
if norm1 == 0 or norm2 == 0:
|
| 132 |
+
return 0.0
|
| 133 |
+
|
| 134 |
+
similarity = dot_product / (norm1 * norm2)
|
| 135 |
+
return float(similarity)
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
logger.error(f"Error computing similarity: {str(e)}")
|
| 139 |
+
return 0.0
|
| 140 |
+
|
| 141 |
+
def batch_embedding_interface(texts: str) -> str:
|
| 142 |
+
"""Interface for batch embedding generation"""
|
| 143 |
+
try:
|
| 144 |
+
# Split texts by newlines
|
| 145 |
+
text_list = [text.strip() for text in texts.split('\n') if text.strip()]
|
| 146 |
+
|
| 147 |
+
if not text_list:
|
| 148 |
+
return json.dumps([])
|
| 149 |
+
|
| 150 |
+
# Generate embeddings
|
| 151 |
+
embeddings = generate_embeddings(text_list)
|
| 152 |
+
|
| 153 |
+
# Return as JSON string
|
| 154 |
+
return json.dumps(embeddings)
|
| 155 |
+
|
| 156 |
+
except Exception as e:
|
| 157 |
+
logger.error(f"Error in batch_embedding_interface: {str(e)}")
|
| 158 |
+
return json.dumps([])
|
| 159 |
+
|
| 160 |
+
def single_embedding_interface(text: str) -> str:
|
| 161 |
+
"""Interface for single embedding generation"""
|
| 162 |
+
try:
|
| 163 |
+
if not text.strip():
|
| 164 |
+
return json.dumps([])
|
| 165 |
+
|
| 166 |
+
# Generate embedding
|
| 167 |
+
embedding = generate_embeddings(text)
|
| 168 |
+
|
| 169 |
+
# Return as JSON string
|
| 170 |
+
return json.dumps(embedding)
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
logger.error(f"Error in single_embedding_interface: {str(e)}")
|
| 174 |
+
return json.dumps([])
|
| 175 |
+
|
| 176 |
+
def similarity_interface(embedding1: str, embedding2: str) -> float:
|
| 177 |
+
"""Interface for computing similarity between two embeddings"""
|
| 178 |
+
try:
|
| 179 |
+
# Parse embeddings from JSON strings
|
| 180 |
+
emb1 = json.loads(embedding1)
|
| 181 |
+
emb2 = json.loads(embedding2)
|
| 182 |
+
|
| 183 |
+
# Compute similarity
|
| 184 |
+
similarity = compute_similarity(emb1, emb2)
|
| 185 |
+
|
| 186 |
+
return similarity
|
| 187 |
+
|
| 188 |
+
except Exception as e:
|
| 189 |
+
logger.error(f"Error in similarity_interface: {str(e)}")
|
| 190 |
+
return 0.0
|
| 191 |
+
|
| 192 |
+
def health_check():
|
| 193 |
+
"""Health check endpoint"""
|
| 194 |
+
return {"status": "healthy", "model_loaded": model is not None}
|
| 195 |
+
|
| 196 |
+
# Create Gradio interface
|
| 197 |
+
def create_interface():
|
| 198 |
+
"""Create the Gradio interface"""
|
| 199 |
+
|
| 200 |
+
with gr.Blocks(
|
| 201 |
+
title="Qwen Embedding Model",
|
| 202 |
+
theme=gr.themes.Soft(),
|
| 203 |
+
css="""
|
| 204 |
+
.gradio-container {
|
| 205 |
+
max-width: 1200px !important;
|
| 206 |
+
margin: auto !important;
|
| 207 |
+
}
|
| 208 |
+
"""
|
| 209 |
+
) as interface:
|
| 210 |
+
|
| 211 |
+
gr.Markdown("""
|
| 212 |
+
# Qwen Embedding Model API
|
| 213 |
+
|
| 214 |
+
This space provides a stable API for generating text embeddings using the Qwen model.
|
| 215 |
+
The API supports both single text and batch processing.
|
| 216 |
+
""")
|
| 217 |
+
|
| 218 |
+
with gr.Tab("Single Text Embedding"):
|
| 219 |
+
gr.Markdown("Generate embedding for a single text input.")
|
| 220 |
+
|
| 221 |
+
with gr.Row():
|
| 222 |
+
with gr.Column():
|
| 223 |
+
single_text_input = gr.Textbox(
|
| 224 |
+
label="Input Text",
|
| 225 |
+
placeholder="Enter text to generate embedding...",
|
| 226 |
+
lines=3
|
| 227 |
+
)
|
| 228 |
+
single_btn = gr.Button("Generate Embedding", variant="primary")
|
| 229 |
+
|
| 230 |
+
with gr.Column():
|
| 231 |
+
single_output = gr.Textbox(
|
| 232 |
+
label="Embedding (JSON)",
|
| 233 |
+
lines=10,
|
| 234 |
+
interactive=False
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
single_btn.click(
|
| 238 |
+
single_embedding_interface,
|
| 239 |
+
inputs=[single_text_input],
|
| 240 |
+
outputs=[single_output]
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
+
with gr.Tab("Batch Text Embedding"):
|
| 244 |
+
gr.Markdown("Generate embeddings for multiple texts (one per line).")
|
| 245 |
+
|
| 246 |
+
with gr.Row():
|
| 247 |
+
with gr.Column():
|
| 248 |
+
batch_text_input = gr.Textbox(
|
| 249 |
+
label="Input Texts (one per line)",
|
| 250 |
+
placeholder="Enter multiple texts, one per line...",
|
| 251 |
+
lines=5
|
| 252 |
+
)
|
| 253 |
+
batch_btn = gr.Button("Generate Embeddings", variant="primary")
|
| 254 |
+
|
| 255 |
+
with gr.Column():
|
| 256 |
+
batch_output = gr.Textbox(
|
| 257 |
+
label="Embeddings (JSON)",
|
| 258 |
+
lines=10,
|
| 259 |
+
interactive=False
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
batch_btn.click(
|
| 263 |
+
batch_embedding_interface,
|
| 264 |
+
inputs=[batch_text_input],
|
| 265 |
+
outputs=[batch_output]
|
| 266 |
+
)
|
| 267 |
+
|
| 268 |
+
with gr.Tab("Similarity Calculator"):
|
| 269 |
+
gr.Markdown("Compute cosine similarity between two embeddings.")
|
| 270 |
+
|
| 271 |
+
with gr.Row():
|
| 272 |
+
with gr.Column():
|
| 273 |
+
emb1_input = gr.Textbox(
|
| 274 |
+
label="Embedding 1 (JSON)",
|
| 275 |
+
placeholder='["0.1", "0.2", ...]',
|
| 276 |
+
lines=3
|
| 277 |
+
)
|
| 278 |
+
emb2_input = gr.Textbox(
|
| 279 |
+
label="Embedding 2 (JSON)",
|
| 280 |
+
placeholder='["0.1", "0.2", ...]',
|
| 281 |
+
lines=3
|
| 282 |
+
)
|
| 283 |
+
sim_btn = gr.Button("Compute Similarity", variant="primary")
|
| 284 |
+
|
| 285 |
+
with gr.Column():
|
| 286 |
+
similarity_output = gr.Number(
|
| 287 |
+
label="Cosine Similarity",
|
| 288 |
+
precision=4
|
| 289 |
+
)
|
| 290 |
+
|
| 291 |
+
sim_btn.click(
|
| 292 |
+
similarity_interface,
|
| 293 |
+
inputs=[emb1_input, emb2_input],
|
| 294 |
+
outputs=[similarity_output]
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
+
with gr.Tab("API Documentation"):
|
| 298 |
+
gr.Markdown("""
|
| 299 |
+
## API Endpoints
|
| 300 |
+
|
| 301 |
+
### 1. Single Text Embedding
|
| 302 |
+
**POST** `/api/predict`
|
| 303 |
+
|
| 304 |
+
```json
|
| 305 |
+
{
|
| 306 |
+
"data": ["Your text here"]
|
| 307 |
+
}
|
| 308 |
+
```
|
| 309 |
+
|
| 310 |
+
### 2. Batch Text Embedding
|
| 311 |
+
**POST** `/api/predict`
|
| 312 |
+
|
| 313 |
+
```json
|
| 314 |
+
{
|
| 315 |
+
"data": [["Text 1", "Text 2", "Text 3"]]
|
| 316 |
+
}
|
| 317 |
+
```
|
| 318 |
+
|
| 319 |
+
### 3. Health Check
|
| 320 |
+
**GET** `/health`
|
| 321 |
+
|
| 322 |
+
Returns: `{"status": "healthy", "model_loaded": true}`
|
| 323 |
+
|
| 324 |
+
## Response Format
|
| 325 |
+
|
| 326 |
+
All endpoints return embeddings as JSON arrays of floating-point numbers.
|
| 327 |
+
""")
|
| 328 |
+
|
| 329 |
+
return interface
|
| 330 |
+
|
| 331 |
+
def main():
|
| 332 |
+
"""Main function to run the application"""
|
| 333 |
+
logger.info("Starting Qwen Embedding Model API...")
|
| 334 |
+
|
| 335 |
+
# Load model
|
| 336 |
+
if not load_model():
|
| 337 |
+
logger.error("Failed to load model. Exiting...")
|
| 338 |
+
return
|
| 339 |
+
|
| 340 |
+
# Create and launch interface
|
| 341 |
+
interface = create_interface()
|
| 342 |
+
|
| 343 |
+
# Launch with public access
|
| 344 |
+
interface.launch(
|
| 345 |
+
server_name="0.0.0.0",
|
| 346 |
+
server_port=7860,
|
| 347 |
+
share=False,
|
| 348 |
+
show_error=True,
|
| 349 |
+
quiet=False
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
if __name__ == "__main__":
|
| 353 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|