Spaces:
Sleeping
Sleeping
File size: 919 Bytes
43374c9 67bcd69 43374c9 c5d0940 43374c9 67bcd69 43374c9 6e20d6b 43374c9 1f9d7aa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import gradio as gr
import openai
import numpy as np
def calc_distance(text1, text2, api) -> str:
text1_emb = openai.Embedding.create(input=text1, model="text-embedding-ada-002", api_key=api)
text2_emb = openai.Embedding.create(input=text2, model="text-embedding-ada-002", api_key=api)
text1_emb = np.array(text1_emb["data"][0]["embedding"])
text2_emb = np.array(text2_emb["data"][0]["embedding"])
distance = np.linalg.norm(text1_emb-text2_emb)
return str(distance)
with gr.Blocks() as b:
openai_apikey = gr.TextArea(label="OpenAI API key", lines=1)
with gr.Row():
with gr.Column():
text1 = gr.TextArea(label="Text 1")
text2 = gr.TextArea(label="Text 2")
btn = gr.Button("Calculate")
output = gr.outputs.Label(label="Distance")
btn.click(fn=calc_distance, inputs=[text1, text2, openai_apikey], outputs=output)
b.launch()
|