Spaces:
Sleeping
Sleeping
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() | |