| | import gradio as gr |
| | import requests |
| | import json |
| | from decouple import Config |
| |
|
| | |
| | def query_vectara(question, chat_history, uploaded_file): |
| | |
| | customer_id = config('CUSTOMER_ID') |
| | corpus_id = config('CORPUS_ID') |
| | api_key = config('API_KEY') |
| | 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"})), |
| | } |
| | 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" |
| |
|
| | |
| | user_message = chat_history[-1][0] |
| |
|
| | query_body = { |
| | "query": [ |
| | { |
| | "query": user_message, |
| | "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}" |
| |
|
| | |
| | |
| | iface = gr.Interface( |
| | fn=query_vectara, |
| | inputs=[gr.Textbox(label="Input Text"), gr.File(label="Upload a file")], |
| | outputs=gr.Textbox(label="Output Text"), |
| | title="Vectara Chatbot", |
| | description="Ask me anything using the Vectara API!" |
| | ) |
| |
|
| | iface.launch() |