Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,28 +3,31 @@ import requests
|
|
3 |
import json
|
4 |
from decouple import Config
|
5 |
|
6 |
-
def query_vectara(question, chat_history, uploaded_file):
|
7 |
-
# Handle file upload to Vectara
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
|
24 |
-
|
25 |
-
|
|
|
|
|
26 |
else:
|
27 |
-
upload_status = "
|
28 |
|
29 |
# Get the user's message from the chat history
|
30 |
user_message = chat_history[-1][0]
|
@@ -91,15 +94,14 @@ def query_vectara(question, chat_history, uploaded_file):
|
|
91 |
response_message = f"{upload_status}\n\nError: {query_response.status_code}"
|
92 |
|
93 |
return response_message
|
94 |
-
|
95 |
-
# Create a Gradio ChatInterface with a text input
|
96 |
iface = gr.Interface(
|
97 |
fn=query_vectara,
|
98 |
-
inputs=[gr.Textbox(label="Input Text"), gr.File(label="Upload a file")],
|
99 |
outputs=gr.Textbox(label="Output Text"),
|
100 |
title="Vectara Chatbot",
|
101 |
description="Ask me anything using the Vectara API!"
|
102 |
)
|
103 |
|
104 |
iface.launch()
|
105 |
-
|
|
|
3 |
import json
|
4 |
from decouple import Config
|
5 |
|
6 |
+
def query_vectara(question, chat_history, uploaded_file=None):
|
7 |
+
# Handle file upload to Vectara if a file is provided
|
8 |
+
if uploaded_file is not None:
|
9 |
+
customer_id = config('CUSTOMER_ID') # Read from .env file
|
10 |
+
corpus_id = config('CORPUS_ID') # Read from .env file
|
11 |
+
api_key = config('API_KEY') # Read from .env file
|
12 |
+
url = f"https://api.vectara.io/v1/upload?c={customer_id}&o={corpus_id}"
|
13 |
|
14 |
+
post_headers = {
|
15 |
+
"x-api-key": api_key,
|
16 |
+
"customer-id": customer_id
|
17 |
+
}
|
18 |
|
19 |
+
files = {
|
20 |
+
"file": (uploaded_file.name, uploaded_file),
|
21 |
+
"doc_metadata": (None, json.dumps({"metadata_key": "metadata_value"})), # Replace with your metadata
|
22 |
+
}
|
23 |
+
response = requests.post(url, files=files, headers=post_headers)
|
24 |
|
25 |
+
if response.status_code == 200:
|
26 |
+
upload_status = "File uploaded successfully"
|
27 |
+
else:
|
28 |
+
upload_status = "Failed to upload the file"
|
29 |
else:
|
30 |
+
upload_status = "No file uploaded"
|
31 |
|
32 |
# Get the user's message from the chat history
|
33 |
user_message = chat_history[-1][0]
|
|
|
94 |
response_message = f"{upload_status}\n\nError: {query_response.status_code}"
|
95 |
|
96 |
return response_message
|
97 |
+
|
98 |
+
# Create a Gradio ChatInterface with a text input and an optional file upload input
|
99 |
iface = gr.Interface(
|
100 |
fn=query_vectara,
|
101 |
+
inputs=[gr.Textbox(label="Input Text"), gr.File(label="Upload a file", required=False)],
|
102 |
outputs=gr.Textbox(label="Output Text"),
|
103 |
title="Vectara Chatbot",
|
104 |
description="Ask me anything using the Vectara API!"
|
105 |
)
|
106 |
|
107 |
iface.launch()
|
|