File size: 2,304 Bytes
39dff4c
 
 
391032d
4a0366f
39dff4c
c6ddc86
4a0366f
 
 
 
c6ddc86
39dff4c
 
c6ddc86
 
39dff4c
 
c6ddc86
 
 
 
 
39dff4c
 
c6ddc86
39dff4c
5909527
c6ddc86
4a0366f
 
c6ddc86
4a0366f
 
 
 
 
 
 
5909527
 
 
 
 
 
 
 
 
c6ddc86
4a0366f
c6ddc86
39dff4c
7afe812
f66ea9a
39dff4c
f66ea9a
1544bf4
7afe812
1544bf4
f66ea9a
 
 
 
 
1544bf4
5909527
141372c
1544bf4
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
61
62
63
64
65
66
67
68
import gradio as gr
import requests
import json
from decouple import Config

# Function to interact with Vectara API
def query_vectara(question, chat_history, uploaded_file):
    # Handle file upload to Vectara
    customer_id = config('CUSTOMER_ID')  # Read from .env file
    corpus_id = config('CORPUS_ID')  # Read from .env file
    api_key = config('API_KEY')  # Read from .env file
    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 the file"

    # Get the user's message from the chat history
    user_message = chat_history[-1][0]

    query_body = {
        "query": [
            {
                "query": user_message,  # Use the user's message as the query
                "start": 0,
                "numResults": 10,
                "corpusKey": [
                    {
                        "customerId": customer_id,
                        "corpusId": corpus_id,
                        "lexicalInterpolationConfig": {"lambda": 0.025}
                    }
                ]
            }
        ]
    }

    api_endpoint = "https://api.vectara.io/v1/query"
    return f"{upload_status}\n\nResponse from Vectara API: {response.text}"

# Create a Gradio ChatInterface
iface = gr.Interface(
    fn=query_vectara,
    inputs=[gr.File(label="Upload a file")],  # Use `gr.File` directly
    examples=["Hello", "What is the weather today?", "Tell me a joke"],
    title="Vectara Chatbot",
    description="Ask me anything using the Vectara API!",
    submit="Send",  # Use `submit` instead of `submit_btn`
    stop="Stop",  # Use `stop` instead of `stop_btn`
    retry="Retry",  # Use `retry` instead of `retry_btn`
    undo="Undo",  # Use `undo` instead of `undo_btn`
    clear="Clear",  # Use `clear` instead of `clear_btn`
    autofocus=True
)

iface.launch()