Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
|
| 6 |
+
# Load model and tokenizer
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("BAAI/bge-reranker-v2-m3")
|
| 8 |
+
model = AutoModelForSequenceClassification.from_pretrained("BAAI/bge-reranker-v2-m3")
|
| 9 |
+
|
| 10 |
+
# Define a function to get score
|
| 11 |
+
def rerank_score(query, document):
|
| 12 |
+
# Format input as "query: document"
|
| 13 |
+
input_text = f"{query} [SEP] {document}"
|
| 14 |
+
inputs = tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)
|
| 15 |
+
with torch.no_grad():
|
| 16 |
+
outputs = model(**inputs)
|
| 17 |
+
scores = F.softmax(outputs.logits, dim=1)
|
| 18 |
+
relevance_score = scores[0][1].item() # Assuming label 1 means "relevant"
|
| 19 |
+
return f"Relevance Score: {relevance_score:.4f}"
|
| 20 |
+
|
| 21 |
+
# Gradio UI
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=rerank_score,
|
| 24 |
+
inputs=[gr.Textbox(label="Query"), gr.Textbox(label="Document")],
|
| 25 |
+
outputs=gr.Textbox(label="Relevance Score"),
|
| 26 |
+
title="BAAI bge-reranker-v2-m3",
|
| 27 |
+
description="Enter a query and a document to get a relevance score."
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
if __name__ == "__main__":
|
| 31 |
+
demo.launch()
|