Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import CrossEncoder
|
| 3 |
+
|
| 4 |
+
# Load the reranker model
|
| 5 |
+
model = CrossEncoder("jinaai/jina-reranker-v2-base-multilingual")
|
| 6 |
+
|
| 7 |
+
# Function to score query-document pairs
|
| 8 |
+
def rerank(query, documents):
|
| 9 |
+
scores = model.predict([[query, doc] for doc in documents])
|
| 10 |
+
ranked_docs = sorted(zip(documents, scores), key=lambda x: x[1], reverse=True)
|
| 11 |
+
return [{"document": doc, "score": round(score, 4)} for doc, score in ranked_docs]
|
| 12 |
+
|
| 13 |
+
# Define Gradio interface
|
| 14 |
+
iface = gr.Interface(
|
| 15 |
+
fn=rerank,
|
| 16 |
+
inputs=["text", gr.List(gr.Textbox())],
|
| 17 |
+
outputs="json",
|
| 18 |
+
title="JinaAI v2 Reranker API",
|
| 19 |
+
description="Enter a query and a list of documents. The model will rank them based on relevance.",
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Launch Gradio app
|
| 23 |
+
iface.launch()
|