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 headers | |
| api_key_header = { | |
| "customer-id": CUSTOMER_ID, | |
| "x-api-key": API_KEY | |
| } | |
| # Define the request body in the structure provided in the example | |
| request_body = { | |
| "query": [ | |
| { | |
| "query": user_message, | |
| "queryContext": "", | |
| "start": 1, | |
| "numResults": 10, | |
| "contextConfig": { | |
| "charsBefore": 0, | |
| "charsAfter": 0, | |
| "sentencesBefore": 2, | |
| "sentencesAfter": 2, | |
| "startTag": "%START_SNIPPET%", | |
| "endTag": "%END_SNIPPET%", | |
| }, | |
| "rerankingConfig": { | |
| "rerankerId": 272725718, | |
| "mmrConfig": { | |
| "diversityBias": 0.27 | |
| } | |
| }, | |
| "corpusKey": [ | |
| { | |
| "customerId": CUSTOMER_ID, | |
| "corpusId": CORPUS_ID, | |
| "semantics": 0, | |
| "metadataFilter": "", | |
| "lexicalInterpolationConfig": { | |
| "lambda": 0 | |
| }, | |
| "dim": [] | |
| } | |
| ], | |
| "summary": [ | |
| { | |
| "maxSummarizedResults": 5, | |
| "responseLang": "eng", | |
| "summarizerPromptName": "vectara-summary-ext-v1.2.0" | |
| } | |
| ] | |
| } | |
| ] | |
| } | |
| # Make the API request using Gradio | |
| response = requests.post( | |
| "https://api.vectara.io/v1/query", | |
| json=request_body, # Use json to automatically serialize the request body | |
| verify=True, | |
| headers=api_key_header | |
| ) | |
| if response.status_code == 200: | |
| query_data = response.json() | |
| print(query_data) | |
| if query_data: | |
| # Extract summary and the first 5 sources | |
| response_set = query_data.get('responseSet', [{}])[0] # get the first response set | |
| summary = response_set.get('summary', [{}])[0] # get the first summary | |
| summary_text = summary.get('text', 'No summary available') | |
| sources = response_set.get('response', [])[:5] | |
| sources_text = [source.get('text', '') for source in sources] | |
| return f"Summary: {summary_text}\n\nSources:\n{json.dumps(sources_text, indent=2)}" | |
| else: | |
| return "No data found in the response." | |
| else: | |
| return f"Error: {response.status_code}" | |
| 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() | |
 
			
