Spaces:
Sleeping
Sleeping
crate app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("BAAI/bge-reranker-v2-m3")
|
| 6 |
+
model = AutoModelForSequenceClassification.from_pretrained("BAAI/bge-reranker-v2-m3")
|
| 7 |
+
|
| 8 |
+
def rerank(query, docs):
|
| 9 |
+
docs = docs.strip().split('\n')
|
| 10 |
+
pairs = [(query, doc) for doc in docs]
|
| 11 |
+
inputs = tokenizer(pairs, padding=True, truncation=True, return_tensors="pt")
|
| 12 |
+
with torch.no_grad():
|
| 13 |
+
scores = model(**inputs).logits.squeeze(-1)
|
| 14 |
+
results = sorted(zip(docs, scores.tolist()), key=lambda x: x[1], reverse=True)
|
| 15 |
+
return "\n\n".join([f"Score: {score:.4f}\n{doc}" for doc, score in results])
|
| 16 |
+
|
| 17 |
+
iface = gr.Interface(
|
| 18 |
+
fn=rerank,
|
| 19 |
+
inputs=[
|
| 20 |
+
gr.Textbox(label="Query", lines=1),
|
| 21 |
+
gr.Textbox(label="Documents (one per line)", lines=10)
|
| 22 |
+
],
|
| 23 |
+
outputs="text",
|
| 24 |
+
title="BGE Reranker v2 M3",
|
| 25 |
+
description="Input a query and a list of documents. Outputs reranked documents with scores."
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
iface.launch()
|