Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
# Function to interact with Vectara API | |
def query_vectara(question): | |
api_endpoint = "https://api.vectara.io/v1/query" | |
customer_id = "<YOUR-CUSTOMER-ID>" | |
corpus_id = "<YOUR-CORPUS-ID>" | |
api_key = "<YOUR-API-KEY>" | |
query_body = { | |
"query": [ | |
{ | |
"query": question, # Use the 'question' input as the query | |
"start": 0, | |
"numResults": 10, | |
"corpusKey": [ | |
{ | |
"customerId": customer_id, | |
"corpusId": corpus_id, | |
"lexicalInterpolationConfig": {"lambda": 0.025} | |
} | |
], | |
"contextConfig": { | |
"sentencesBefore": 3, | |
"sentencesAfter": 3, | |
"startTag": "%START_TAG%", | |
"endTag": "%END_TAG%" | |
}, | |
"summary": [ | |
{ | |
"responseLang": "eng", | |
"maxSummarizedResults": 7, | |
"summarizerPromptName": "vectara-summarizer-ext-v1.3.0" | |
} | |
] | |
} | |
] | |
} | |
post_headers = { | |
"Content-type": "application/json", | |
"Accept": "application/json", | |
"customer-id": customer_id, | |
"x-api-key": api_key | |
} | |
response = requests.post(api_endpoint, json=query_body, headers=post_headers) | |
if response.status_code == 200: | |
return response.json() | |
else: | |
return {"error": "Failed to query Vectara API"} | |
# Create a Gradio interface | |
iface = gr.Interface( | |
fn=query_vectara, | |
inputs=gr.inputs.Text(label="Ask a question:"), | |
outputs=gr.outputs.JSON(), | |
live=True, | |
capture_session=True | |
) | |
# Run the Gradio interface | |
iface.launch() | |