Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,27 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
# Load
|
5 |
-
|
|
|
|
|
6 |
|
7 |
# Function to rerank documents
|
8 |
def rerank(query, documents):
|
9 |
-
documents = documents.split("
|
10 |
-
|
11 |
-
|
|
|
12 |
return [{"document": doc, "score": round(score, 4)} for doc, score in ranked_docs]
|
13 |
|
14 |
# Gradio Interface
|
15 |
iface = gr.Interface(
|
16 |
fn=rerank,
|
17 |
-
inputs=["text", gr.Textbox(label="Documents (Separate with
|
18 |
outputs="json",
|
19 |
-
title="JinaAI v2 Reranker API",
|
20 |
-
description="Enter a query and documents (separated by '
|
21 |
)
|
22 |
|
23 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from optimum.pipelines import pipeline
|
3 |
+
from transformers import AutoTokenizer
|
4 |
|
5 |
+
# Load ONNX optimized model
|
6 |
+
model_name = "jinaai/jina-reranker-v2-base-multilingual"
|
7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
+
model = pipeline("text-classification", model=model_name, tokenizer=tokenizer)
|
9 |
|
10 |
# Function to rerank documents
|
11 |
def rerank(query, documents):
|
12 |
+
documents = documents.split("&&&")
|
13 |
+
inputs = [[query, doc] for doc in documents if doc.strip()]
|
14 |
+
scores = model(inputs)
|
15 |
+
ranked_docs = sorted(zip(documents, [s['score'] for s in scores]), key=lambda x: x[1], reverse=True)
|
16 |
return [{"document": doc, "score": round(score, 4)} for doc, score in ranked_docs]
|
17 |
|
18 |
# Gradio Interface
|
19 |
iface = gr.Interface(
|
20 |
fn=rerank,
|
21 |
+
inputs=["text", gr.Textbox(label="Documents (Separate with &&&)", placeholder="Doc1 &&& Doc2 &&& Doc3")],
|
22 |
outputs="json",
|
23 |
+
title="JinaAI v2 Reranker API (Optimized)",
|
24 |
+
description="Enter a query and documents (separated by '&&&'). The model will rank them based on relevance.",
|
25 |
)
|
26 |
|
27 |
iface.launch()
|