File size: 2,248 Bytes
92d1986
 
f81630c
d8f89be
 
f81630c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e8b8c7d
 
 
 
 
 
 
 
 
 
 
 
d8f89be
e8b8c7d
 
 
 
f81630c
 
e8b8c7d
 
f81630c
 
 
 
 
7843274
f81630c
 
 
 
e8b8c7d
f81630c
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
from gradio_client import Client

title = """# 🙋🏻‍♂️Welcome to tonic's openai⚒️connector for 🐣e5-mistral🛌🏻
this⚒️connector looks like openai embeddings but uses 🐣e5-mistral🛌🏻 - so you can use it as a drop in replacement for the open ai api. Both the inputs and outputs exactly match what is⚒️expected from openai's api so anything already⚒️compatible with that api is now compative with 🐣e5-mistral🛌🏻
"""

client = Client("https://tonic-e5.hf.space/--replicas/w3v1e/")

def get_embeddings(task, input_text):
    try:
        result = client.predict(
            task,       
            input_text, 
            api_name="/compute_embeddings"
        )
        return result
    except Exception as e:
        return str(e)

def format_response(embeddings, model):
    return {
        "data": [
            {
                "embedding": embeddings,
                "index": 0,
                "object": "embedding"
            }
        ],
        "model": model,
        "object": "list",
        "usage": {
            "prompt_tokens": 17,
            "total_tokens": 17
        }
    }

def generate_embeddings(input_text, model, encoding_format, user):
    embeddings = get_embeddings(model, input_text)
    formatted_response = format_response(embeddings, model)
    return formatted_response

with gr.Blocks() as app:
    gr.Markdown(title)
    with gr.Row():
        input_text = gr.Textbox(label="Input Text", placeholder="Enter text or array of texts")
        model = gr.Dropdown(label="Model", choices=["ArguAna", "ClimateFEVER", "DBPedia", "FEVER", "FiQA2018", "HotpotQA", "MSMARCO", "NFCorpus", "NQ", "QuoraRetrieval", "SCIDOCS", "SciFact", "Touche2020", "TRECCOVID"], value="text-embedding-ada-002")
        encoding_format = gr.Radio(label="Encoding Format", choices=["float", "base64"], value="float")
        user = gr.Textbox(label="User", placeholder="Enter user identifier (optional)")
        submit_button = gr.Button("Generate Embeddings")

    output = gr.JSON(label="Embeddings Output")

    submit_button.click(
        fn=generate_embeddings,
        inputs=[input_text, model, encoding_format, user],
        outputs=output
    )

app.launch()