Sirapatrwan commited on
Commit
040b657
·
verified ·
1 Parent(s): e4f3062

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from sentence_transformers import CrossEncoder
2
+
3
+ # 1. Load a pre-trained CrossEncoder model
4
+ model = CrossEncoder("cross-encoder/ms-marco-MiniLM-L6-v2")
5
+
6
+ query = "How many people live in Berlin?"
7
+ passages = [
8
+ "Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.",
9
+ "Berlin is well known for its museums.",
10
+ "In 2014, the city state Berlin had 37,368 live births (+6.6%), a record number since 1991.",
11
+ "The urban area of Berlin comprised about 4.1 million people in 2014, making it the seventh most populous urban area in the European Union.",
12
+ "The city of Paris had a population of 2,165,423 people within its administrative city limits as of January 1, 2019",
13
+ "An estimated 300,000-420,000 Muslims reside in Berlin, making up about 8-11 percent of the population.",
14
+ "Berlin is subdivided into 12 boroughs or districts (Bezirke).",
15
+ "In 2015, the total labour force in Berlin was 1.85 million.",
16
+ "In 2013 around 600,000 Berliners were registered in one of the more than 2,300 sport and fitness clubs.",
17
+ "Berlin has a yearly total of about 135 million day visitors, which puts it in third place among the most-visited city destinations in the European Union.",
18
+ ]
19
+
20
+ # 2a. Either: predict scores for all pairs of sentences involved in the query
21
+ scores = model.predict([(query, passage) for passage in passages])
22
+ # => [ 8.607138 -4.320077 7.5978117 8.915804 -4.237982 8.2359 0.33119553 3.4510403 6.352979 5.416662 ]
23
+
24
+ # 2b. Or rank a list of passages for a query
25
+ ranks = model.rank(query, passages, return_documents=True)
26
+
27
+ # Print the reranked passages
28
+ print("Query:", query)
29
+ for rank in ranks:
30
+ print(f"- #{rank['corpus_id']} ({rank['score']:.2f}): {rank['text']}")
31
+ """
32
+ Query: How many people live in Berlin?
33
+ - #3 (8.92): The urban area of Berlin comprised about 4.1 million people in 2014, making it the seventh most populous urban area in the European Union.
34
+ - #0 (8.61): Berlin had a population of 3,520,031 registered inhabitants in an area of 891.82 square kilometers.
35
+ - #5 (8.24): An estimated 300,000-420,000 Muslims reside in Berlin, making up about 8-11 percent of the population.
36
+ - #2 (7.60): In 2014, the city state Berlin had 37,368 live births (+6.6%), a record number since 1991.
37
+ - #8 (6.35): In 2013 around 600,000 Berliners were registered in one of the more than 2,300 sport and fitness clubs.
38
+ - #9 (5.42): Berlin has a yearly total of about 135 million day visitors, which puts it in third place among the most-visited city destinations in the European Union.
39
+ - #7 (3.45): In 2015, the total labour force in Berlin was 1.85 million.
40
+ - #6 (0.33): Berlin is subdivided into 12 boroughs or districts (Bezirke).
41
+ - #4 (-4.24): The city of Paris had a population of 2,165,423 people within its administrative city limits as of January 1, 2019
42
+ - #1 (-4.32): Berlin is well known for its museums.
43
+ """