|
import gradio as gr |
|
from sentence_transformers import SentenceTransformer |
|
from sklearn.metrics.pairwise import cosine_similarity |
|
import time |
|
|
|
sentences = [ |
|
|
|
"New York State Route 359 (NY 359) is a state highway located entirely within the town of Skaneateles in Onondaga County, New York in the United States. At 1.71 miles (2.75 km) in length, it is one of the shortest signed state highways in all of New York. The route begins at NY 38A a half-mile (0.8 km) north of the Onondaga-Cayuga County county line and ends at NY 41A in the hamlet of Mandana. It lies between Skaneateles Lake and Owasco Lake. Excluding its termini, there are only two junctions with other roadways along the entire route. NY 359 was assigned in the early 1940s and was part of the original NY 26 from 1924 to 1930. NY 359 begins in the town of Skaneateles at an intersection with Heifer Street, a local road, and NY 38A. The route goes north, intersecting with Weeks Road north of NY 38A. Here, NY 359 begins to curve to the east with Weeks Road carrying on the northward alignment of NY 359. Upon meeting Lacy Road, the route takes over its east-west routing and heads east. NY 359 comes to an end just under a mile later at NY 41A in the lakeside hamlet of Mandana. East of NY 41A, the roadway continues for roughly to a boat launch on the western shore of Skaneateles Lake, from NY 38A. Signed as a north-south route, it lies that way at the southern end, but about midway along its length the road turns and thence travels east-west. Excluding its termini, there are only two junctions with other roadways along the entire route. It is the only instance in New York of a route designated without an alphabetic suffix that terminates at both ends at suffixed routes. The origins of NY 359 date back to the assignment of the first state routes in New York in 1924. At this time, all of modern NY 359 became part of NY 26, a highway that continued north along what is now NY 41A toward the village of Skaneateles and south on modern NY 38A toward the village of Moravia. In the 1930 renumbering of state highways in New York, NY 26 was reassigned elsewhere in the state. Its former routing was incorporated into several new routes, including part of NY 38A from Moravia to the modern junction of NY 38A and NY 359 and part of NY 41A from Mandana to Skaneateles. The portion that is now NY 359, however, received no designation and became unnumbered. It was designated as NY 359 in the early 1940s.", |
|
"New York State Route 424 (NY 424) was an east–west state highway in northern Chautauqua County, New York, in the United States. The route began at an intersection with NY 380 (now County Route 380 or CR 380) in the town of Stockton and ended at a junction with NY 60 in the village of Cassadaga. NY 424 was assigned as part of the 1930 renumbering of state highways in New York and removed from the state highway system in 1980 as part of a highway maintenance swap between the state of New York and Chautauqua County. The route's former alignment is now part of CR 58. NY 424 began at an intersection with NY 380 in the town of Stockton. The route headed northeast, passing by forests and homes as it ascended Stockton Hill. Here, it intersected with Nelson Hill Road, a short alternate route of NY 424. After reaching the top of the hill, NY 424 became known as Stockton Hill Road and went through fields on its way to the village of Cassadaga. Once in the village limits, the highway intersected Putnam Road (CR 71) in an area of Cassadaga known as Burnhams. NY 424 continued on into the heart of the village, passing by homes and the southern end of Lower Lake before coming to an end at an intersection with NY 60. NY 424 was assigned as part of the 1930 renumbering of state highways in New York. It went unchanged until April 1, 1980, when ownership and maintenance of the route was transferred from the state of New York to Chautauqua County as part of a highway maintenance swap between the two levels of government. The other routes given to the county were the portion of NY 380 between NY 424 and U.S. Route 20 (US 20), the entirety of NY 428, and two reference routes in Dunkirk and Fredonia. In return, the state assumed control of NY 394 between NY 5 and US 20, US 62 from NY 394 to NY 60, and Forest Avenue south of Jamestown. The NY 424 designation was officially removed on August 7, 1980, and its former routing is now part of CR 58, which was extended over the length of the state highway.", |
|
"Marguerite Stitt Church (New York City, September 27, 1892 – Chicago, May 26, 1990) was a psychologist by profession who was elected to her husband Ralph E. Church's congressional seat to complete his term, following his death in office. She continued to represent Illinois' 13th congressional district as a Republican from 1951 to 1963. Church born in 1892 to William J. and Adelaide Stitt (née Forsythe). She grew up in the New York City metropolitan area where she attended St. Agatha School. She traveled abroad with her parents each summer, which helped develop her interest in foreign countries. In 1914, Church graduated from Wellesley College where she earned a B.A. in psychology, with a minor in economics and sociology. She went on to get a Master's degree from Columbia University in 1917. After graduating from Columbia, Church became a consulting psychologist for the State Charities Aid Association of New York City for one year. When her husband, Ralph E. Church, was first elected as a U.S. Representative in 1934, she became closely involved with his work. In March 1950, Ralph died of heart failure during a House committee hearing. Church ran for her husband's now vacant seat and was elected in November 1950 by the highest majority ever had by a candidate from Evanston. On December 21, 1918, she married Ralph E. Church. The couple settled in Evanston, Illinois and had three children: Ralph Jr., William, and Marjory. She died in Evanston on May 26, 1990. Church is buried in Memorial Park, Skokie, Illinois.", |
|
"The Fly Club is a final club, traditionally 'punching' (inviting to stand for election) male undergraduates of Harvard College during their sophomore or junior year. Undergraduate and graduate members participate in club activities. Founded 1836 as a literary society by the editors of 'Harvardiana', the club was granted a charter by the Alpha Delta Phi fraternity in 1837 and remained a chapter until surrendering its charter in 1865. With the graduation of the members of the class of 1868, the club was discontinued until 1878, when graduate members, including Edward Everett Hale (class of 1839) and Phillips Brooks (class of 1855), initiated undergraduates from the class of 1879, to whom the old charter was restored. In 1906, the charter was once again surrendered, and in 1910, the organization officially adopted the name 'Fly Club,' its unofficial title since 1885. In 1996, the Fly Club merged with the DU Club, another final club, and the combined entity retained the name 'Fly Club.' Some sources maintain that the club's name was derived by combining the 'PH' from 'Alpha,' the 'l' from 'Delta,' and the 'i' from 'Phi,' to get 'Phli,' pronounced 'Fly'. The club motto, suggested by Prof. Morris H. Morgan (class of 1881) and adopted Feb. 1902, reads DURATURIS HAUD DURIS VINCULIS, an ablative absolute construction translated as 'Bonds should be lasting, not chafing or hard.' Constructed in 1896, with brick facade added in 1902, the Fly clubhouse is located at Two Holyoke Place, near Harvard Square, along the 'Gold Coast' of formerly private residences that now comprise Harvard's Adams House (completed 1932) The Fly sits in front of Harvard's Lowell House (1930), across Mt. Auburn St. from the Harvard Lampoon building (1909). The Fly Club Gate is located along the exterior of Winthrop House. An English Baroque structure, the gate was built in 1914 by a grant from members of the Fly Club. The Fly's symbol, a 'leopard rampant gardant' (known as the 'Kitty'), is centered within the ironwork above the entry. Inscribed below is a dedication: 'For Friendships Made in College the Fly Club in Gratitude has Built this Gate.' <nowiki>*</nowiki> Initiated into the D.U. Club, which merged with the Fly Club in 1996.", |
|
] |
|
|
|
|
|
def run_model(model_name, passage_1, passage_2): |
|
messages = [] |
|
message = "Model: " + model_name |
|
messages.append(message) |
|
messages.append('') |
|
|
|
passages = [passage_1, passage_2] |
|
|
|
message = "Comparing the following passages for similarity using model embeddings.\n A score of 1 means exact same similarity and a score of 0 indicates no similarity.\n" |
|
messages.append(message) |
|
|
|
model = SentenceTransformer(model_name) |
|
start_time = time.time() |
|
embeddings = model.encode(passages) |
|
|
|
|
|
score = cosine_similarity([embeddings[0]], [embeddings[1]])[0][0] |
|
messages.append('score: ' + str(score)) |
|
messages.append('') |
|
|
|
messages.append('Passage 1:\n' + passages[0]) |
|
messages.append('') |
|
messages.append('Passage 2:\n' + passages[1]) |
|
messages.append('') |
|
|
|
messages.append( |
|
'Passage 1 embedding (len=' + str(len(embeddings[0])) + '): \n' + str(embeddings[0])) |
|
messages.append('') |
|
messages.append( |
|
'Passage 2 embedding (len=' + str(len(embeddings[1])) + '): \n' + str(embeddings[1])) |
|
messages.append('') |
|
|
|
return '\n'.join(messages) |
|
|
|
|
|
app = gr.Interface( |
|
fn=run_model, |
|
inputs=[ |
|
gr.Dropdown( |
|
choices=['sentence-transformers/all-MiniLM-L6-v2', |
|
'sentence-transformers/all-mpnet-base-v2', |
|
'sentence-transformers/msmarco-distilroberta-base-v2',], |
|
value='sentence-transformers/all-MiniLM-L6-v2', |
|
label="Choose Model", |
|
type="value"), |
|
gr.Textbox( |
|
label="Passage 1", |
|
placeholder="Enter a passage to compare to passage 2.", |
|
type="text"), |
|
gr.Textbox( |
|
label="Passage 2", |
|
placeholder="Enter a passage to compare to passage 1.", |
|
type="text"), |
|
], |
|
outputs=gr.Textbox(label="Results"), |
|
allow_flagging="never", |
|
title="Sentence Transformer Embeddings", |
|
description="This is a demo of the Sentence Transformers library.", |
|
examples=[ |
|
[ |
|
'sentence-transformers/all-MiniLM-L6-v2', |
|
'Hello, how are you?', |
|
'I am fine, thank you.', |
|
], |
|
[ |
|
'sentence-transformers/all-MiniLM-L6-v2', |
|
'New York State Route 359 (NY 359) is a state highway located entirely within the town of Skaneateles in Onondaga County.', |
|
"New York State Route 359 (NY 359) is a state highway located entirely within the town of Skaneateles in Onondaga County, New York in the United States. At 1.71 miles (2.75 km) in length, it is one of the shortest signed state highways in all of New York. The route begins at NY 38A a half-mile (0.8 km) north of the Onondaga-Cayuga County county line and ends at NY 41A in the hamlet of Mandana.", |
|
], |
|
] |
|
) |
|
|
|
app.launch() |
|
|