File size: 795 Bytes
bb59c27
b8777eb
 
bb59c27
d9b26f0
 
 
 
 
 
 
 
 
 
b8777eb
 
 
 
 
 
d9b26f0
b8777eb
bb59c27
b8777eb
d9b26f0
 
 
bb59c27
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import gradio as gr
from transformers import AutoModel, AutoTokenizer
import torch

# Load model with caching
tokenizer = AutoTokenizer.from_pretrained(
    "Qwen/Qwen3-Embedding-8B",
    trust_remote_code=True
)
model = AutoModel.from_pretrained(
    "Qwen/Qwen3-Embedding-8B",
    trust_remote_code=True,
    device_map="auto"
).eval()

def get_embedding(text):
    inputs = tokenizer(text, return_tensors="pt", truncation=True).to(model.device)
    with torch.no_grad():
        outputs = model(**inputs)
    embedding = outputs.last_hidden_state.mean(dim=1).squeeze().tolist()
    return {"text": text, "embedding_size": len(embedding)}

demo = gr.Interface(
    fn=get_embedding,
    inputs=gr.Textbox(label="Input text"),
    outputs=gr.JSON(),
    title="Qwen3 Embeddings"
)
demo.launch()