Spaces:
Sleeping
Sleeping
import gradio as gr | |
from sentence_transformers import CrossEncoder | |
# Load and optimize the model | |
model = CrossEncoder( | |
"jinaai/jina-reranker-v1-tiny-en", | |
trust_remote_code=True | |
) | |
# Function to rerank documents | |
def rerank(query, documents): | |
documents = documents.split("&&&") # Use special delimiter | |
inputs = [[query, doc] for doc in documents if doc.strip()] | |
scores = model.predict(inputs) | |
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 (Optimized)", | |
description="Enter a query and documents (separated by '&&&'). The model will rank them based on relevance.", | |
) | |
iface.launch() | |