Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,18 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
import json
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
# Function to interact with Vectara API
|
6 |
def query_vectara(question, chat_history, uploaded_file):
|
7 |
-
# Handle file upload
|
8 |
-
customer_id =
|
9 |
-
corpus_id =
|
10 |
-
api_key =
|
11 |
url = f"https://api.vectara.io/v1/upload?c={customer_id}&o={corpus_id}"
|
12 |
|
13 |
post_headers = {
|
@@ -26,12 +32,29 @@ def query_vectara(question, chat_history, uploaded_file):
|
|
26 |
else:
|
27 |
upload_status = "Failed to upload file"
|
28 |
|
29 |
-
#
|
|
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
|
|
35 |
return f"{upload_status}\n\nResponse from Vectara API: {response.text}"
|
36 |
|
37 |
# Create a Gradio ChatInterface
|
@@ -45,7 +68,4 @@ iface = gr.Interface(
|
|
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!",
|
48 |
-
)
|
49 |
-
|
50 |
-
# Run the Gradio interface
|
51 |
-
iface.launch()
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
|
5 |
+
# Function to interact with Vectara API
|
6 |
+
import gradio as gr
|
7 |
+
import requests
|
8 |
+
import json
|
9 |
+
from decouple import config # Import config from python-decouple
|
10 |
+
|
11 |
# Function to interact with Vectara API
|
12 |
def query_vectara(question, chat_history, uploaded_file):
|
13 |
+
# Handle file upload to Vectara
|
14 |
+
customer_id = config('CUSTOMER_ID') # Read from .env file
|
15 |
+
corpus_id = config('CORPUS_ID') # Read from .env file
|
16 |
+
api_key = config('API_KEY') # Read from .env file
|
17 |
url = f"https://api.vectara.io/v1/upload?c={customer_id}&o={corpus_id}"
|
18 |
|
19 |
post_headers = {
|
|
|
32 |
else:
|
33 |
upload_status = "Failed to upload file"
|
34 |
|
35 |
+
# Get the user's message from the chat history
|
36 |
+
user_message = chat_history[-1][0]
|
37 |
|
38 |
+
query_body = {
|
39 |
+
"query": [
|
40 |
+
{
|
41 |
+
"query": user_message, # Use the user's message as the query
|
42 |
+
"start": 0,
|
43 |
+
"numResults": 10,
|
44 |
+
"corpusKey": [
|
45 |
+
else:
|
46 |
+
return {"error": "Failed to query Vectara API"}
|
47 |
|
48 |
+
# Create a Gradio ChatInterface
|
49 |
+
iface = gr.ChatInterface(
|
50 |
+
fn=query_vectara,
|
51 |
+
examples=["Hello", "What is the weather today?", "Tell me a joke"],
|
52 |
+
title="Vectara Chatbot",
|
53 |
+
description="Ask me anything using the Vectara API!",
|
54 |
+
|
55 |
+
)
|
56 |
|
57 |
+
api_endpoint = "https://api.vectara.io/v1/query"
|
58 |
return f"{upload_status}\n\nResponse from Vectara API: {response.text}"
|
59 |
|
60 |
# Create a Gradio ChatInterface
|
|
|
68 |
examples=["Hello", "What is the weather today?", "Tell me a joke"],
|
69 |
title="Vectara Chatbot",
|
70 |
description="Ask me anything using the Vectara API!",
|
71 |
+
)
|
|
|
|
|
|