File size: 1,542 Bytes
39dff4c
 
 
 
 
c6ddc86
 
39dff4c
 
 
c6ddc86
39dff4c
 
c6ddc86
 
39dff4c
 
c6ddc86
 
 
 
 
39dff4c
 
c6ddc86
39dff4c
c6ddc86
 
 
 
 
 
 
 
 
39dff4c
7afe812
c6ddc86
39dff4c
c6ddc86
 
 
 
 
7afe812
 
 
39dff4c
 
 
2d46b21
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
import gradio as gr
import requests
import json

# Function to interact with Vectara API
def query_vectara(question, chat_history, uploaded_file):
    # Handle file upload here (using the provided code)
    customer_id = "<YOUR-CUSTOMER-ID>"
    corpus_id = "<YOUR-CORPUS-ID>"
    api_key = "<YOUR-API-KEY>"
    url = f"https://api.vectara.io/v1/upload?c={customer_id}&o={corpus_id}"

    post_headers = {
        "x-api-key": api_key,
        "customer-id": customer_id
    }

    files = {
        "file": (uploaded_file.name, uploaded_file),
        "doc_metadata": (None, json.dumps({"metadata_key": "metadata_value"})),  # Replace with your metadata
    }
    response = requests.post(url, files=files, verify=True, headers=post_headers)

    if response.status_code == 200:
        upload_status = "File uploaded successfully"
    else:
        upload_status = "Failed to upload file"

    # The rest of the chatbot logic goes here

    api_endpoint = "https://api.vectara.io/v1/query"

    # Rest of the chatbot logic as before

    return f"{upload_status}\n\nResponse from Vectara API: {response.text}"

# Create a Gradio ChatInterface
iface = gr.Interface(
    fn=query_vectara,
    inputs=[
        gr.inputs.Text(label="Ask a question:"),
        gr.inputs.File(label="Upload a file")
    ],
    outputs=gr.outputs.Textbox(),
    examples=["Hello", "What is the weather today?", "Tell me a joke"],
    title="Vectara Chatbot",
    description="Ask me anything using the Vectara API!",
)

# Run the Gradio interface
iface.launch()