Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,62 +3,45 @@ import requests
|
|
| 3 |
import json
|
| 4 |
|
| 5 |
# Function to interact with Vectara API
|
| 6 |
-
def query_vectara(question, chat_history):
|
| 7 |
-
|
| 8 |
customer_id = "<YOUR-CUSTOMER-ID>"
|
| 9 |
corpus_id = "<YOUR-CORPUS-ID>"
|
| 10 |
api_key = "<YOUR-API-KEY>"
|
| 11 |
-
|
| 12 |
-
# Get the user's message from the chat history
|
| 13 |
-
user_message = chat_history[-1][0]
|
| 14 |
-
|
| 15 |
-
query_body = {
|
| 16 |
-
"query": [
|
| 17 |
-
{
|
| 18 |
-
"query": user_message, # Use the user's message as the query
|
| 19 |
-
"start": 0,
|
| 20 |
-
"numResults": 10,
|
| 21 |
-
"corpusKey": [
|
| 22 |
-
{
|
| 23 |
-
"customerId": customer_id,
|
| 24 |
-
"corpusId": corpus_id,
|
| 25 |
-
"lexicalInterpolationConfig": {"lambda": 0.025}
|
| 26 |
-
}
|
| 27 |
-
],
|
| 28 |
-
"contextConfig": {
|
| 29 |
-
"sentencesBefore": 3,
|
| 30 |
-
"sentencesAfter": 3,
|
| 31 |
-
"startTag": "%START_TAG%",
|
| 32 |
-
"endTag": "%END_TAG%"
|
| 33 |
-
},
|
| 34 |
-
"summary": [
|
| 35 |
-
{
|
| 36 |
-
"responseLang": "eng",
|
| 37 |
-
"maxSummarizedResults": 7,
|
| 38 |
-
"summarizerPromptName": "vectara-summarizer-ext-v1.3.0"
|
| 39 |
-
}
|
| 40 |
-
]
|
| 41 |
-
}
|
| 42 |
-
]
|
| 43 |
-
}
|
| 44 |
|
| 45 |
post_headers = {
|
| 46 |
-
"
|
| 47 |
-
"
|
| 48 |
-
"customer-id": customer_id,
|
| 49 |
-
"x-api-key": api_key
|
| 50 |
}
|
| 51 |
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if response.status_code == 200:
|
| 55 |
-
|
| 56 |
else:
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
# Create a Gradio ChatInterface
|
| 60 |
-
iface = gr.
|
| 61 |
fn=query_vectara,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
examples=["Hello", "What is the weather today?", "Tell me a joke"],
|
| 63 |
title="Vectara Chatbot",
|
| 64 |
description="Ask me anything using the Vectara API!",
|
|
|
|
| 3 |
import json
|
| 4 |
|
| 5 |
# Function to interact with Vectara API
|
| 6 |
+
def query_vectara(question, chat_history, uploaded_file):
|
| 7 |
+
# Handle file upload here (using the provided code)
|
| 8 |
customer_id = "<YOUR-CUSTOMER-ID>"
|
| 9 |
corpus_id = "<YOUR-CORPUS-ID>"
|
| 10 |
api_key = "<YOUR-API-KEY>"
|
| 11 |
+
url = f"https://api.vectara.io/v1/upload?c={customer_id}&o={corpus_id}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
post_headers = {
|
| 14 |
+
"x-api-key": api_key,
|
| 15 |
+
"customer-id": customer_id
|
|
|
|
|
|
|
| 16 |
}
|
| 17 |
|
| 18 |
+
files = {
|
| 19 |
+
"file": (uploaded_file.name, uploaded_file),
|
| 20 |
+
"doc_metadata": (None, json.dumps({"metadata_key": "metadata_value"})), # Replace with your metadata
|
| 21 |
+
}
|
| 22 |
+
response = requests.post(url, files=files, verify=True, headers=post_headers)
|
| 23 |
|
| 24 |
if response.status_code == 200:
|
| 25 |
+
upload_status = "File uploaded successfully"
|
| 26 |
else:
|
| 27 |
+
upload_status = "Failed to upload file"
|
| 28 |
+
|
| 29 |
+
# The rest of the chatbot logic goes here
|
| 30 |
+
|
| 31 |
+
api_endpoint = "https://api.vectara.io/v1/query"
|
| 32 |
+
|
| 33 |
+
# Rest of the chatbot logic as before
|
| 34 |
+
|
| 35 |
+
return f"{upload_status}\n\nResponse from Vectara API: {response.text}"
|
| 36 |
|
| 37 |
# Create a Gradio ChatInterface
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
fn=query_vectara,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.inputs.Text(label="Ask a question:"),
|
| 42 |
+
gr.inputs.File(label="Upload a file")
|
| 43 |
+
],
|
| 44 |
+
outputs=gr.outputs.Textbox(),
|
| 45 |
examples=["Hello", "What is the weather today?", "Tell me a joke"],
|
| 46 |
title="Vectara Chatbot",
|
| 47 |
description="Ask me anything using the Vectara API!",
|