schroneko commited on
Commit
99d2415
1 Parent(s): 1555443

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +116 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ from openai import OpenAI
4
+ import voyageai
5
+ from typing import List, Tuple
6
+
7
+
8
+ def initialize_clients(openai_key: str, voyage_key: str):
9
+ """Initialize API clients with provided keys or environment variables"""
10
+ openai_key = openai_key.strip() or None
11
+ voyage_key = voyage_key.strip() or None
12
+ return OpenAI(api_key=openai_key), voyageai.Client(api_key=voyage_key)
13
+
14
+
15
+ def get_openai_embedding(client: OpenAI, text: str) -> List[float]:
16
+ """Get embedding from OpenAI's text-embedding-3-large model"""
17
+ response = client.embeddings.create(input=text, model="text-embedding-3-large")
18
+ return response.data[0].embedding
19
+
20
+
21
+ def get_voyage_embedding(client: voyageai.Client, text: str) -> List[float]:
22
+ """Get embedding from Voyage's voyage-3 model"""
23
+ result = client.embed([text], model="voyage-3")
24
+ return result.embeddings[0]
25
+
26
+
27
+ def cosine_similarity(a: List[float], b: List[float]) -> float:
28
+ """Calculate cosine similarity between two vectors"""
29
+ a = np.array(a)
30
+ b = np.array(b)
31
+ return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
32
+
33
+
34
+ def process_texts(
35
+ openai_key: str, voyage_key: str, text1: str, text2: str
36
+ ) -> Tuple[float, float, float]:
37
+ """Process two texts and return their embeddings and similarities"""
38
+ # Initialize clients with provided keys
39
+ openai_client, voyage_client = initialize_clients(openai_key, voyage_key)
40
+
41
+ # Get embeddings from both models
42
+ openai_emb1 = get_openai_embedding(openai_client, text1)
43
+ openai_emb2 = get_openai_embedding(openai_client, text2)
44
+ voyage_emb1 = get_voyage_embedding(voyage_client, text1)
45
+ voyage_emb2 = get_voyage_embedding(voyage_client, text2)
46
+
47
+ # Calculate similarities
48
+ openai_similarity = cosine_similarity(openai_emb1, openai_emb2)
49
+ voyage_similarity = cosine_similarity(voyage_emb1, voyage_emb2)
50
+
51
+ # Calculate difference in similarities
52
+ similarity_diff = abs(openai_similarity - voyage_similarity)
53
+
54
+ return openai_similarity, voyage_similarity, similarity_diff
55
+
56
+
57
+ def compare_embeddings(
58
+ openai_key: str, voyage_key: str, text1: str, text2: str
59
+ ) -> Tuple[str, str, str]:
60
+ """Compare embeddings from both models and return formatted results"""
61
+ try:
62
+ openai_sim, voyage_sim, sim_diff = process_texts(
63
+ openai_key, voyage_key, text1, text2
64
+ )
65
+
66
+ openai_result = f"{openai_sim:.4f}"
67
+ voyage_result = f"{voyage_sim:.4f}"
68
+ diff_result = f"{sim_diff:.4f}"
69
+
70
+ return openai_result, voyage_result, diff_result
71
+ except Exception as e:
72
+ return f"Error: {str(e)}", "", ""
73
+
74
+
75
+ # Create Gradio interface
76
+ with gr.Blocks() as demo:
77
+ gr.Markdown("""
78
+ # 埋め込みモデルの比較デモ
79
+
80
+ 対象モデルは OpenAI の text-embedding-3-large と Voyage AI の voyage-3 のふたつ。入力テキストに対して、それぞれのモデルでの類似度とその差分を計算する。
81
+
82
+ ## API Key
83
+
84
+ OpenAI と Voyage AI の API キーは下記より。
85
+
86
+ - OpenAI API Key: [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)
87
+ - Voyage AI API Key: [https://dash.voyageai.com](https://dash.voyageai.com)
88
+ """)
89
+
90
+ with gr.Row():
91
+ openai_key = gr.Textbox(
92
+ label="OpenAI API Key", placeholder="sk-...", type="password", scale=2
93
+ )
94
+ voyage_key = gr.Textbox(
95
+ label="Voyage AI API Key", placeholder="pa-...", type="password", scale=2
96
+ )
97
+
98
+ with gr.Row():
99
+ text1 = gr.Textbox(label="Text 1", lines=3)
100
+ text2 = gr.Textbox(label="Text 2", lines=3)
101
+
102
+ compare_btn = gr.Button("Compare")
103
+
104
+ with gr.Row():
105
+ openai_output = gr.Textbox(label="OpenAI text-embedding-3-large Similarity")
106
+ voyage_output = gr.Textbox(label="Voyage AI voyage-3 Similarity")
107
+ diff_output = gr.Textbox(label="Absolute Difference")
108
+
109
+ compare_btn.click(
110
+ compare_embeddings,
111
+ inputs=[openai_key, voyage_key, text1, text2],
112
+ outputs=[openai_output, voyage_output, diff_output],
113
+ )
114
+
115
+ if __name__ == "__main__":
116
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio>=5.6.0
2
+ numpy>=2.1.3
3
+ openai>=1.54.4
4
+ voyageai>=0.3.1