schroneko commited on
Commit
bc085bc
1 Parent(s): babe057

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -8,7 +8,7 @@ from sentence_transformers import SentenceTransformer
8
  def load_model(model_name):
9
  if model_name == "GLuCoSE-base-ja-v2":
10
  return SentenceTransformer("pkshatech/GLuCoSE-base-ja-v2")
11
- elif model_name == "RoSEtta-base-ja":
12
  return SentenceTransformer("pkshatech/RoSEtta-base", trust_remote_code=True)
13
  elif model_name == "ruri-large":
14
  return SentenceTransformer("cl-nagoya/ruri-large")
@@ -17,20 +17,21 @@ def load_model(model_name):
17
  def get_similarities(model_name, sentences):
18
  model = load_model(model_name)
19
 
20
- if model_name == "ruri-large":
 
 
 
 
 
21
  sentences = [
22
  "クエリ: " + s if i % 2 == 0 else "文章: " + s
23
  for i, s in enumerate(sentences)
24
  ]
25
 
26
  embeddings = model.encode(sentences, convert_to_tensor=True)
27
-
28
- if model_name in ["GLuCoSE-base-ja-v2", "RoSEtta-base-ja"]:
29
- similarities = model.similarity(embeddings, embeddings)
30
- else: # ruri-large
31
- similarities = F.cosine_similarity(
32
- embeddings.unsqueeze(0), embeddings.unsqueeze(1), dim=2
33
- )
34
 
35
  return similarities.cpu().numpy()
36
 
@@ -45,7 +46,7 @@ def process_input(model_name, input_text):
45
  return format_similarities(similarities)
46
 
47
 
48
- models = ["GLuCoSE-base-ja-v2", "RoSEtta-base-ja", "ruri-large"]
49
 
50
  with gr.Blocks() as demo:
51
  gr.Markdown("# Sentence Similarity Demo")
@@ -55,7 +56,14 @@ with gr.Blocks() as demo:
55
  model_dropdown = gr.Dropdown(
56
  choices=models, label="Select Model", value=models[0]
57
  )
58
- input_text = gr.Textbox(lines=5, label="Input Sentences (one per line)")
 
 
 
 
 
 
 
59
  submit_btn = gr.Button(value="Calculate Similarities")
60
 
61
  with gr.Column():
@@ -69,11 +77,11 @@ with gr.Blocks() as demo:
69
  examples=[
70
  [
71
  "GLuCoSE-base-ja-v2",
72
- "The weather is lovely today.\nIt's so sunny outside!\nHe drove to the stadium.",
73
  ],
74
  [
75
- "RoSEtta-base-ja",
76
- "The weather is lovely today.\nIt's so sunny outside!\nHe drove to the stadium.",
77
  ],
78
  [
79
  "ruri-large",
 
8
  def load_model(model_name):
9
  if model_name == "GLuCoSE-base-ja-v2":
10
  return SentenceTransformer("pkshatech/GLuCoSE-base-ja-v2")
11
+ elif model_name == "RoSEtta-base":
12
  return SentenceTransformer("pkshatech/RoSEtta-base", trust_remote_code=True)
13
  elif model_name == "ruri-large":
14
  return SentenceTransformer("cl-nagoya/ruri-large")
 
17
  def get_similarities(model_name, sentences):
18
  model = load_model(model_name)
19
 
20
+ if model_name in ["GLuCoSE-base-ja-v2", "RoSEtta-base"]:
21
+ sentences = [
22
+ "query: " + s if i % 2 == 0 else "passage: " + s
23
+ for i, s in enumerate(sentences)
24
+ ]
25
+ elif model_name == "ruri-large":
26
  sentences = [
27
  "クエリ: " + s if i % 2 == 0 else "文章: " + s
28
  for i, s in enumerate(sentences)
29
  ]
30
 
31
  embeddings = model.encode(sentences, convert_to_tensor=True)
32
+ similarities = F.cosine_similarity(
33
+ embeddings.unsqueeze(0), embeddings.unsqueeze(1), dim=2
34
+ )
 
 
 
 
35
 
36
  return similarities.cpu().numpy()
37
 
 
46
  return format_similarities(similarities)
47
 
48
 
49
+ models = ["GLuCoSE-base-ja-v2", "RoSEtta-base", "ruri-large"]
50
 
51
  with gr.Blocks() as demo:
52
  gr.Markdown("# Sentence Similarity Demo")
 
56
  model_dropdown = gr.Dropdown(
57
  choices=models, label="Select Model", value=models[0]
58
  )
59
+ input_text = gr.Textbox(
60
+ lines=5,
61
+ label="Input Sentences (one per line)",
62
+ placeholder="Enter query and passage pairs, alternating lines.",
63
+ )
64
+ gr.Markdown("""
65
+ **Note:** Prefixes ('query:' / 'passage:' or 'クエリ:' / '文章:') are added automatically. Just input your sentences.
66
+ """)
67
  submit_btn = gr.Button(value="Calculate Similarities")
68
 
69
  with gr.Column():
 
77
  examples=[
78
  [
79
  "GLuCoSE-base-ja-v2",
80
+ "PKSHAはどんな会社ですか?\n研究開発したアルゴリズムを、多くの企業のソフトウエア・オペレーションに導入しています。",
81
  ],
82
  [
83
+ "RoSEtta-base",
84
+ "PKSHAはどんな会社ですか?\n研究開発したアルゴリズムを、多くの企業のソフトウエア・オペレーションに導入しています。",
85
  ],
86
  [
87
  "ruri-large",