File size: 1,547 Bytes
5b4f208
 
 
4cc6483
 
5b4f208
bc8b062
5b4f208
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c07832
 
5b4f208
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import json
import os 


API_KEY = os.getenv('API_KEY') 
headers = {
    "Authorization": f"Bearer {API_KEY}",
    "Accept": "application/json",
}

def generate_embedding(input_text, model_type, encoding_format):
    invoke_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/functions/091a03bb-7364-4087-8090-bd71e9277520"
    payload = {
        "input": input_text,
        "model": model_type,
        "encoding_format": encoding_format
    }

    session = requests.Session()
    response = session.post(invoke_url, headers=headers, json=payload)

    while response.status_code == 202:
        request_id = response.headers.get("NVCF-REQID")
        fetch_url = "https://api.nvcf.nvidia.com/v2/nvcf/pexec/status/" + request_id
        response = session.get(fetch_url, headers=headers)

    response.raise_for_status()
    response_body = response.json()
    embedding = response_body["data"][0]["embedding"]
    return json.dumps(embedding, indent=2)

# Criação da interface Gradio
iface = gr.Interface(
    fn=generate_embedding,
    inputs=[
        gr.Textbox(label="Input Text"),
        gr.Radio(choices=["query", "passage"], value="query", label="Model Type"),
        gr.Radio(choices=["float", "base64"], value="float", label="Encoding Format")
    ],
    outputs=[gr.Textbox(label="Embedding")],
    title="NVIDIA Retrieval QA Embedding Generator",
    description="Generate embeddings for your text using NVIDIA's Retrieval QA Embedding model."
)

# Executar a aplicação Gradio
iface.launch()