Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
2 |
+
import torch
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Load model and tokenizer
|
6 |
+
model_name = 'cross-encoder/ms-marco-MiniLM-L6-v2'
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Define inference function
|
12 |
+
def get_similarity(question, answer):
|
13 |
+
features = tokenizer(question, answer, padding=True, truncation=True, return_tensors="pt")
|
14 |
+
with torch.no_grad():
|
15 |
+
score = model(**features).logits
|
16 |
+
return float(score[0][0]) # Convert tensor to float
|
17 |
+
|
18 |
+
# Create Gradio interface
|
19 |
+
iface = gr.Interface(
|
20 |
+
fn=get_similarity,
|
21 |
+
inputs=[
|
22 |
+
gr.Textbox(label="Question"),
|
23 |
+
gr.Textbox(label="Answer")
|
24 |
+
],
|
25 |
+
outputs=gr.Number(label="Similarity Score"),
|
26 |
+
title="Cross-Encoder QA Relevance"
|
27 |
+
)
|
28 |
+
|
29 |
+
iface.launch()
|