Spaces:
Sleeping
Sleeping
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() | |