Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
from decouple import Config | |
config = Config('.env') | |
def query_vectara(question): | |
user_message = question | |
# Read authentication parameters from the .env file | |
CUSTOMER_ID = config('CUSTOMER_ID') | |
CORPUS_ID = config('CORPUS_ID') | |
API_KEY = config('API_KEY') | |
# Define the data dictionary | |
data_dict = { | |
"query": [ | |
{ | |
"query": user_message, | |
"num_results": 10, | |
"corpus_key": [ | |
{ | |
"customer_id": CUSTOMER_ID, | |
"corpus_id": CORPUS_ID | |
} | |
] | |
} | |
] | |
} | |
# Define the headers | |
api_key_header = { | |
"customer-id": CUSTOMER_ID, | |
"x-api-key": API_KEY | |
} | |
# Make the API request | |
response = requests.post( | |
"https://api.vectara.io/v1/query", | |
data=json.dumps(data_dict), | |
verify=True, | |
headers=api_key_header, | |
headers={"Content-Type": "application/json"} # Set Content-Type header | |
) | |
if response.status_code == 200: | |
query_data = response.json() | |
response_message = f"Response from Vectara API: {json.dumps(query_data, indent=2)}" | |
else: | |
response_message = f"Error: {response.status_code}" | |
return response_message | |
iface = gr.Interface( | |
fn=query_vectara, | |
inputs=[gr.Textbox(label="Input Text")], | |
outputs=gr.Textbox(label="Output Text"), | |
title="Vectara Chatbot", | |
description="Ask me anything using the Vectara API!" | |
) | |
iface.launch() | |