rishiraj commited on
Commit
09ea753
·
verified ·
1 Parent(s): 56e001a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from sentence_transformers import SentenceTransformer
3
+ import spaces
4
+ import torch
5
+ import ast
6
+
7
+ # Load the model
8
+ model = SentenceTransformer("Qwen/Qwen3-Embedding-0.6B")
9
+
10
+ @spaces.GPU
11
+ def compute(queries_input, documents_input):
12
+ try:
13
+ # Convert string input to Python lists
14
+ queries = ast.literal_eval(queries_input)
15
+ documents = ast.literal_eval(documents_input)
16
+
17
+ # Validate input
18
+ if not isinstance(queries, list) or not isinstance(documents, list):
19
+ return "Inputs must be lists."
20
+
21
+ # Generate embeddings
22
+ query_embeddings = model.encode(queries, prompt_name="query")
23
+ document_embeddings = model.encode(documents)
24
+
25
+ # Compute similarity
26
+ similarity_matrix = torch.tensor(model.similarity(query_embeddings, document_embeddings))
27
+ return similarity_matrix.tolist()
28
+ except Exception as e:
29
+ return str(e)
30
+
31
+ demo = gr.Interface(
32
+ fn=compute,
33
+ inputs=[
34
+ gr.Textbox(label="Queries (Python list, e.g. ['query1', 'query2'])"),
35
+ gr.Textbox(label="Documents (Python list, e.g. ['doc1', 'doc2'])")
36
+ ],
37
+ outputs="json",
38
+ title="embedding"
39
+ )
40
+ demo.launch()