File size: 940 Bytes
e90224d
 
 
 
7074f5d
e90224d
7074f5d
e90224d
4364a46
7074f5d
e90224d
 
 
7074f5d
e90224d
 
4364a46
e90224d
 
4364a46
e90224d
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import gradio as gr
from sentence_transformers import CrossEncoder

# Load the reranker model
model = CrossEncoder("jinaai/jina-reranker-v2-base-multilingual", trust_remote_code=True)

# Function to rerank documents
def rerank(query, documents):
    documents = documents.split("&&")  # Use custom delimiter to split documents correctly
    scores = model.predict([[query, doc] for doc in documents if doc.strip()])
    ranked_docs = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True)
    return [{"document": doc, "score": round(score, 4)} for doc, score in ranked_docs]

# Gradio Interface
iface = gr.Interface(
    fn=rerank,
    inputs=["text", gr.Textbox(label="Documents (Separate with &&)", placeholder="Doc1 ||| Doc2 ||| Doc3")], 
    outputs="json",
    title="JinaAI v2 Reranker API",
    description="Enter a query and documents (separated by '&&'). The model will rank them based on relevance.",
)

iface.launch()