TiniThingsInc commited on
Commit
34addd4
Β·
verified Β·
1 Parent(s): ce958e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -32
app.py CHANGED
@@ -13,9 +13,9 @@ import spaces # ZeroGPU decorator
13
  # Using Qwen3-Embedding-4B for 2560 native dimensions (truncate to 1536 for production)
14
  # Qwen3-4B is optimal for 1536 dims: 60% retention (vs 42.9% for GTE-Qwen2-7B)
15
  MODEL_NAME = "Qwen/Qwen3-Embedding-4B"
16
- print(f"πŸ”„ Loading model: {MODEL_NAME}")
17
  model = SentenceTransformer(MODEL_NAME, trust_remote_code=True)
18
- print(f"βœ… Model loaded successfully")
19
  print(f" Native Dimensions: {model.get_sentence_embedding_dimension()}")
20
  print(f" Max Seq Length: {model.max_seq_length}")
21
  print(f" Matryoshka Support: Yes (truncate to any dimension ≀ {model.get_sentence_embedding_dimension()})")
@@ -140,16 +140,16 @@ def calculate_similarity(text1: str, text2: str, use_instruction: bool) -> dict:
140
  # Create Gradio interface
141
  with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft()) as demo:
142
  gr.Markdown("""
143
- # 🎲 FairFate Embeddings API
144
 
145
  **Powered by Qwen3-Embedding-4B** - Advanced Multilingual Embedding Model
146
 
147
- - 🌍 **100+ Languages** (English, Spanish, French, German, Chinese, Japanese, etc.)
148
- - πŸ“ **2560 Native Dimensions** (matryoshka truncation to 1536 for production)
149
- - πŸ“š **32K Context** (massive text support)
150
- - ⚑ **Instruction-Aware** (optimized for RPG content)
151
- - πŸ”¬ **Matryoshka Support** (flexible 32-2560 dimensions)
152
- - πŸ† **Optimal for 1536 dims** (60% dimension retention)
153
 
154
  Perfect for: Product classification, semantic search, recommendations, multilingual matching
155
  """)
@@ -187,7 +187,7 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
187
  inputs=[input_text, use_inst, output_dims],
188
  )
189
 
190
- with gr.Tab("πŸ” Similarity Calculator"):
191
  gr.Markdown("""
192
  **Comprehensive Similarity Analysis** - Compare two texts using multiple metrics:
193
 
@@ -203,12 +203,12 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
203
 
204
  with gr.Row():
205
  with gr.Column():
206
- text1 = gr.Textbox(
207
  label="First Text",
208
  placeholder="Storm King's Thunder - Giant-themed D&D adventure",
209
  lines=3
210
  )
211
- text2 = gr.Textbox(
212
  label="Second Text",
213
  placeholder="Princes of the Apocalypse - Elemental evil campaign",
214
  lines=3
@@ -230,9 +230,9 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
230
  inputs=[text1, text2, use_inst_sim],
231
  )
232
 
233
- with gr.Tab("πŸ“– API Documentation"):
234
  gr.Markdown("""
235
- ## πŸš€ Quick Start
236
 
237
  ### Python
238
 
@@ -297,7 +297,7 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
297
  }'
298
  ```
299
 
300
- ## πŸ“Š Parameters
301
 
302
  | Parameter | Type | Default | Description |
303
  |-----------|------|---------|-------------|
@@ -305,7 +305,7 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
305
  | `use_instruction` | boolean | true | Add instruction prefix (improves accuracy) |
306
  | `output_dimensions` | number | 1536 | Output size (32-3584, production default: 1536) |
307
 
308
- ## 🎯 Use Cases
309
 
310
  - **Product Classification**: Auto-tag by genre, system, theme
311
  - **Semantic Search**: Find by meaning, not keywords
@@ -313,20 +313,20 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
313
  - **Duplicate Detection**: Find similar listings
314
  - **Multilingual Matching**: Cross-language similarity
315
 
316
- ## ⚑ Performance
317
 
318
  | Batch Size | GPU Throughput | CPU Throughput |
319
  |------------|----------------|----------------|
320
  | 1 | ~800/sec | ~80/sec |
321
  | 32 | ~4000/sec | ~250/sec |
322
 
323
- ## 🌍 Supported Languages
324
 
325
  English, Spanish, French, German, Italian, Portuguese, Russian, Polish, Dutch, Czech,
326
  Chinese, Japanese, Korean, Arabic, Hebrew, Hindi, Thai, Vietnamese, Indonesian,
327
  Turkish, Swedish, Norwegian, Danish, Finnish, Greek, Romanian, Hungarian, and 80+ more!
328
 
329
- ## πŸ“ Citation
330
 
331
  ```bibtex
332
  @misc{qwen3-embedding-2025,
@@ -338,25 +338,25 @@ with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft())
338
  ```
339
  """)
340
 
341
- with gr.Tab("ℹ️ Model Info"):
342
  gr.Markdown(f"""
343
  ## Model Details
344
 
345
- - **Model:** {MODEL_NAME}
346
- - **Dimensions:** {model.get_sentence_embedding_dimension()}
347
- - **Max Sequence Length:** {model.max_seq_length} tokens
348
- - **Languages:** 100+
349
- - **License:** Apache 2.0
350
- - **Normalization:** L2 normalized (ready for cosine similarity)
351
 
352
  ## Advantages
353
 
354
- βœ… **Best Multilingual Performance** - Top tier on MTEB leaderboard
355
- βœ… **Massive Context** - 32K tokens (vs 512 for most models)
356
- βœ… **Instruction-Aware** - Can customize for specific domains
357
- βœ… **Flexible Dimensions** - 32 to 2560 dimensions (matryoshka truncation)
358
- βœ… **Code-Switching** - Handles mixed-language text
359
- βœ… **Production Optimized** - 60% retention at 1536 dims (best in class)
360
 
361
  ## Resources
362
 
 
13
  # Using Qwen3-Embedding-4B for 2560 native dimensions (truncate to 1536 for production)
14
  # Qwen3-4B is optimal for 1536 dims: 60% retention (vs 42.9% for GTE-Qwen2-7B)
15
  MODEL_NAME = "Qwen/Qwen3-Embedding-4B"
16
+ print(f" Loading model: {MODEL_NAME}")
17
  model = SentenceTransformer(MODEL_NAME, trust_remote_code=True)
18
+ print(f" Model loaded successfully")
19
  print(f" Native Dimensions: {model.get_sentence_embedding_dimension()}")
20
  print(f" Max Seq Length: {model.max_seq_length}")
21
  print(f" Matryoshka Support: Yes (truncate to any dimension ≀ {model.get_sentence_embedding_dimension()})")
 
140
  # Create Gradio interface
141
  with gr.Blocks(title="FairFate Embeddings API - Qwen3", theme=gr.themes.Soft()) as demo:
142
  gr.Markdown("""
143
+ # FairFate Embeddings API
144
 
145
  **Powered by Qwen3-Embedding-4B** - Advanced Multilingual Embedding Model
146
 
147
+ - **100+ Languages** (English, Spanish, French, German, Chinese, Japanese, etc.)
148
+ - **2560 Native Dimensions** (matryoshka truncation to 1536 for production)
149
+ - **32K Context** (massive text support)
150
+ - **Instruction-Aware** (optimized for RPG content)
151
+ - **Matryoshka Support** (flexible 32-2560 dimensions)
152
+ - **Optimal for 1536 dims** (60% dimension retention)
153
 
154
  Perfect for: Product classification, semantic search, recommendations, multilingual matching
155
  """)
 
187
  inputs=[input_text, use_inst, output_dims],
188
  )
189
 
190
+ with gr.Tab("Similarity Calculator"):
191
  gr.Markdown("""
192
  **Comprehensive Similarity Analysis** - Compare two texts using multiple metrics:
193
 
 
203
 
204
  with gr.Row():
205
  with gr.Column():
206
+ text1 = gr.JSON(
207
  label="First Text",
208
  placeholder="Storm King's Thunder - Giant-themed D&D adventure",
209
  lines=3
210
  )
211
+ text2 = gr.JSON(
212
  label="Second Text",
213
  placeholder="Princes of the Apocalypse - Elemental evil campaign",
214
  lines=3
 
230
  inputs=[text1, text2, use_inst_sim],
231
  )
232
 
233
+ with gr.Tab("API Documentation"):
234
  gr.Markdown("""
235
+ ## Quick Start
236
 
237
  ### Python
238
 
 
297
  }'
298
  ```
299
 
300
+ ## Parameters
301
 
302
  | Parameter | Type | Default | Description |
303
  |-----------|------|---------|-------------|
 
305
  | `use_instruction` | boolean | true | Add instruction prefix (improves accuracy) |
306
  | `output_dimensions` | number | 1536 | Output size (32-3584, production default: 1536) |
307
 
308
+ ## Use Cases
309
 
310
  - **Product Classification**: Auto-tag by genre, system, theme
311
  - **Semantic Search**: Find by meaning, not keywords
 
313
  - **Duplicate Detection**: Find similar listings
314
  - **Multilingual Matching**: Cross-language similarity
315
 
316
+ ## Performance
317
 
318
  | Batch Size | GPU Throughput | CPU Throughput |
319
  |------------|----------------|----------------|
320
  | 1 | ~800/sec | ~80/sec |
321
  | 32 | ~4000/sec | ~250/sec |
322
 
323
+ ## Supported Languages
324
 
325
  English, Spanish, French, German, Italian, Portuguese, Russian, Polish, Dutch, Czech,
326
  Chinese, Japanese, Korean, Arabic, Hebrew, Hindi, Thai, Vietnamese, Indonesian,
327
  Turkish, Swedish, Norwegian, Danish, Finnish, Greek, Romanian, Hungarian, and 80+ more!
328
 
329
+ ## Citation
330
 
331
  ```bibtex
332
  @misc{qwen3-embedding-2025,
 
338
  ```
339
  """)
340
 
341
+ with gr.Tab("Model Info"):
342
  gr.Markdown(f"""
343
  ## Model Details
344
 
345
+ - **Model:** {MODEL_NAME}
346
+ - **Dimensions:** {model.get_sentence_embedding_dimension()}
347
+ - **Max Sequence Length:** {model.max_seq_length} tokens
348
+ - **Languages:** 100+
349
+ - **License:** Apache 2.0
350
+ - **Normalization:** L2 normalized (ready for cosine similarity)
351
 
352
  ## Advantages
353
 
354
+ **Best Multilingual Performance** - Top tier on MTEB leaderboard
355
+ **Massive Context** - 32K tokens (vs 512 for most models)
356
+ **Instruction-Aware** - Can customize for specific domains
357
+ **Flexible Dimensions** - 32 to 2560 dimensions (matryoshka truncation)
358
+ **Code-Switching** - Handles mixed-language text
359
+ **Production Optimized** - 60% retention at 1536 dims (best in class)
360
 
361
  ## Resources
362