TheresaQWQ commited on
Commit
c732e8c
·
verified ·
1 Parent(s): a191a68

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification
3
+
4
+ # Load the pre-trained model
5
+ model = AutoModelForSequenceClassification.from_pretrained(
6
+ 'jinaai/jina-reranker-v2-base-multilingual',
7
+ torch_dtype="auto",
8
+ trust_remote_code=True,
9
+ )
10
+
11
+ model.eval()
12
+
13
+ def compute_scores(query, documents):
14
+ """
15
+ Compute scores between a query and multiple documents using the loaded model.
16
+
17
+ Args:
18
+ query (str): The input query string.
19
+ documents (list of str): List of document strings to compare against the query.
20
+
21
+ Returns:
22
+ list of float: Scores representing the relevance of each document to the query.
23
+ """
24
+ sentence_pairs = [[query, doc] for doc in documents]
25
+ scores = model.compute_score(sentence_pairs, max_length=1024)
26
+ return scores.tolist()
27
+
28
+ # Define Gradio interface
29
+ iface = gr.Interface(
30
+ fn=compute_scores,
31
+ inputs=[
32
+ gr.inputs.Textbox(lines=2, placeholder="Enter your query here..."),
33
+ gr.inputs.Textbox(lines=8, placeholder="Enter your documents separated by newlines...")
34
+ ],
35
+ outputs="json",
36
+ title="Sentence Pair Scoring with Jina Reranker Model",
37
+ description="This tool computes the relevance scores between a given query and a set of documents using the Jina Reranker model."
38
+ )
39
+
40
+ # Launch the interface
41
+ if __name__ == "__main__":
42
+ iface.launch()