Purgatorium commited on
Commit
810908b
·
verified ·
1 Parent(s): 8e9a75e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer, util
3
+ import torch
4
+
5
+ # Load the model (Qwen3-Embedding-0.6B)
6
+ # trust_remote_code is required for some Qwen architectures
7
+ model = SentenceTransformer("Qwen/Qwen3-Embedding-0.6B", trust_remote_code=True)
8
+
9
+ def embed_text(text, is_query=True):
10
+ """
11
+ API 1: Text Embedding
12
+ Qwen3 benefits from a 'query' prompt for retrieval tasks.
13
+ """
14
+ prompt_name = "query" if is_query else None
15
+ embedding = model.encode(text, prompt_name=prompt_name, convert_to_tensor=True)
16
+ return embedding.tolist()
17
+
18
+ def calculate_similarity(text_a, text_b, is_query=True):
19
+ """
20
+ API 2: Embedding Similarity
21
+ Returns a float between 0 and 1 (clamped) representing the similarity.
22
+ """
23
+ prompt_name = "query" if is_query else None
24
+
25
+ # Encode both texts
26
+ emb_a = model.encode(text_a, prompt_name=prompt_name, convert_to_tensor=True)
27
+ emb_b = model.encode(text_b, prompt_name=prompt_name, convert_to_tensor=True)
28
+
29
+ # Compute Cosine Similarity
30
+ similarity = util.cos_sim(emb_a, emb_b).item()
31
+
32
+ # Clamp to [0, 1] for "percentage" logic
33
+ score = max(0, min(1, similarity))
34
+ percentage = f"{score * 100:.2f}%"
35
+
36
+ return score, percentage
37
+
38
+ # Building the Gradio UI
39
+ with gr.Blocks(title="Qwen3 Embedding API") as demo:
40
+ gr.Markdown("# Qwen3-Embedding-0.6B API & UI")
41
+ gr.Markdown("This space provides high-quality text embeddings and similarity scores.")
42
+
43
+ with gr.Tab("Text Embedding"):
44
+ with gr.Row():
45
+ with gr.Column():
46
+ input_text = gr.Textbox(label="Input Text", placeholder="Enter text to embed...")
47
+ is_query_toggle = gr.Checkbox(label="Is this a search query?", value=True, info="Uses Qwen's specific query prompt for better retrieval.")
48
+ btn_embed = gr.Button("Generate Embedding", variant="primary")
49
+ with gr.Column():
50
+ output_vec = gr.JSON(label="Embedding Vector (Truncated in UI)")
51
+
52
+ btn_embed.click(fn=embed_text, inputs=[input_text, is_query_toggle], outputs=output_vec, api_name="embed")
53
+
54
+ with gr.Tab("Similarity Score"):
55
+ with gr.Row():
56
+ with gr.Column():
57
+ text_a = gr.Textbox(label="Text A", placeholder="First sentence...")
58
+ text_b = gr.Textbox(label="Text B", placeholder="Second sentence...")
59
+ is_query_sim = gr.Checkbox(label="Use query prompts?", value=True)
60
+ btn_sim = gr.Button("Compare Texts", variant="primary")
61
+ with gr.Column():
62
+ sim_float = gr.Number(label="Similarity Score (0-1)")
63
+ sim_percent = gr.Label(label="Match Percentage")
64
+
65
+ # API returns both the float and the label string
66
+ btn_sim.click(fn=calculate_similarity, inputs=[text_a, text_b, is_query_sim], outputs=[sim_float, sim_percent], api_name="similarity")
67
+
68
+ gr.Markdown("""
69
+ ### How to use the API
70
+ You can call these endpoints programmatically using the Gradio Python Client:
71
+ ```python
72
+ from gradion_client import Client
73
+ client = Client("your-username/your-space-name")
74
+
75
+ # API 1: Embedding
76
+ result = client.predict("Hello world", True, api_name="/embed")
77
+
78
+ # API 2: Similarity
79
+ score, percent = client.predict("Text A", "Text B", True, api_name="/similarity")
80
+ ```
81
+ """)
82
+
83
+ if __name__ == "__main__":
84
+ demo.launch()