import gradio as gr import requests import json # Function to interact with Vectara API def query_vectara(query_str): api_endpoint = "https://api.vectara.io/v1/query" customer_id = "" corpus_id = "" api_key = "" query_body = { "query": [ { "query": query_str, "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.Textbox(label="Ask a question:"), outputs=gr.outputs.JSON(), live=True, capture_session=True ) # Run the Gradio interface iface.launch()