Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
)
|
| 9 |
|
| 10 |
-
def generate_embeddings(texts: str, use_query_mode: bool):
|
| 11 |
-
"""One text per line
|
| 12 |
lines = [line.strip() for line in texts.split("\n") if line.strip()]
|
| 13 |
if not lines:
|
| 14 |
return {"embeddings": [], "texts": [], "dimension": 1024}
|
| 15 |
|
| 16 |
-
#
|
| 17 |
prompt_name = "query" if use_query_mode else None
|
| 18 |
|
| 19 |
embeddings = model.encode(
|
|
@@ -21,40 +22,40 @@ def generate_embeddings(texts: str, use_query_mode: bool):
|
|
| 21 |
prompt_name=prompt_name,
|
| 22 |
convert_to_numpy=True,
|
| 23 |
normalize_embeddings=True,
|
| 24 |
-
batch_size=32, # tuned for fast CPU inference
|
| 25 |
).tolist()
|
| 26 |
|
| 27 |
return {
|
| 28 |
-
"embeddings": embeddings, # list of 1024-dim vectors
|
| 29 |
"texts": lines,
|
| 30 |
"dimension": len(embeddings[0]) if embeddings else 1024,
|
|
|
|
|
|
|
| 31 |
}
|
| 32 |
|
| 33 |
|
| 34 |
-
# Gradio
|
| 35 |
demo = gr.Interface(
|
| 36 |
fn=generate_embeddings,
|
| 37 |
inputs=[
|
| 38 |
gr.Textbox(
|
| 39 |
-
lines=
|
| 40 |
-
placeholder="Paste one text per line...\n\
|
| 41 |
label="Input Texts (one per line)",
|
| 42 |
),
|
| 43 |
gr.Checkbox(
|
| 44 |
-
label="Use Query Mode (recommended for search/
|
| 45 |
value=True,
|
| 46 |
-
info="Adds
|
| 47 |
),
|
| 48 |
],
|
| 49 |
outputs=gr.JSON(label="Embedding Response"),
|
| 50 |
-
title="π Qwen3-Embedding-0.6B
|
| 51 |
-
description="""
|
| 52 |
-
|
| 53 |
-
Powered by Qwen/Qwen3-Embedding-0.6B on Hugging Face Spaces.""",
|
| 54 |
examples=[
|
| 55 |
-
["What is the capital of
|
| 56 |
-
["
|
| 57 |
-
]
|
| 58 |
)
|
| 59 |
|
| 60 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from model2vec import StaticModel
|
| 4 |
|
| 5 |
+
# Suppress warnings
|
| 6 |
+
os.environ["TOKENIZERS_PARALLELISM"] = "false"
|
| 7 |
+
|
| 8 |
+
# Distilled Qwen3-Embedding-0.6B β static & ultra-fast on CPU
|
| 9 |
+
model = StaticModel.from_pretrained("futur/Qwen3-Embedding-0.6B-model2vec-onnx")
|
| 10 |
|
| 11 |
+
def generate_embeddings(texts: str, use_query_mode: bool = True):
|
| 12 |
+
"""One text per line. Handles 500β1000+ tokens instantly on CPU."""
|
| 13 |
lines = [line.strip() for line in texts.split("\n") if line.strip()]
|
| 14 |
if not lines:
|
| 15 |
return {"embeddings": [], "texts": [], "dimension": 1024}
|
| 16 |
|
| 17 |
+
# Query mode adds instruction (keeps the original Qwen3 behavior)
|
| 18 |
prompt_name = "query" if use_query_mode else None
|
| 19 |
|
| 20 |
embeddings = model.encode(
|
|
|
|
| 22 |
prompt_name=prompt_name,
|
| 23 |
convert_to_numpy=True,
|
| 24 |
normalize_embeddings=True,
|
|
|
|
| 25 |
).tolist()
|
| 26 |
|
| 27 |
return {
|
| 28 |
+
"embeddings": embeddings, # list of 1024-dim vectors (or reduced if you want)
|
| 29 |
"texts": lines,
|
| 30 |
"dimension": len(embeddings[0]) if embeddings else 1024,
|
| 31 |
+
"mode": "query (with instruction)" if use_query_mode else "document (raw)",
|
| 32 |
+
"note": "Distilled Qwen3-Embedding-0.6B (Model2Vec) β’ 500Γ faster on CPU β’ <1s for 500β1000 tokens",
|
| 33 |
}
|
| 34 |
|
| 35 |
|
| 36 |
+
# Gradio 5+ Interface + full REST API
|
| 37 |
demo = gr.Interface(
|
| 38 |
fn=generate_embeddings,
|
| 39 |
inputs=[
|
| 40 |
gr.Textbox(
|
| 41 |
+
lines=12,
|
| 42 |
+
placeholder="Paste one text/document per line...\n\n(500β1000+ tokens per line works instantly now)",
|
| 43 |
label="Input Texts (one per line)",
|
| 44 |
),
|
| 45 |
gr.Checkbox(
|
| 46 |
+
label="Use Query Mode (recommended for search/RAG)",
|
| 47 |
value=True,
|
| 48 |
+
info="Adds Qwen3-style instruction automatically",
|
| 49 |
),
|
| 50 |
],
|
| 51 |
outputs=gr.JSON(label="Embedding Response"),
|
| 52 |
+
title="π Qwen3-Embedding-0.6B (Distilled Model2Vec) CPU API",
|
| 53 |
+
description="""Exact Qwen3-Embedding-0.6B you wanted β now distilled to static Model2Vec.
|
| 54 |
+
Same quality + 500Γ faster on free CPU β’ Handles long sequences instantly.""",
|
|
|
|
| 55 |
examples=[
|
| 56 |
+
["What is the capital of France?\n" + "Long document text repeated to simulate 500β1000 tokens... " * 30, True],
|
| 57 |
+
["Short sentence.\nAnother longer paragraph with many tokens for testing speed...", False],
|
| 58 |
+
],
|
| 59 |
)
|
| 60 |
|
| 61 |
demo.launch()
|