Tonic commited on
Commit
fd6e173
·
1 Parent(s): 6b39e2d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -30
app.py CHANGED
@@ -3,36 +3,15 @@ import requests
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]
34
 
35
  # Query Vectara API
 
 
 
 
36
  query_url = "https://api.vectara.io/v1/query/v1/query"
37
 
38
  headers = {
@@ -89,16 +68,16 @@ def query_vectara(question, chat_history, uploaded_file=None):
89
 
90
  if query_response.status_code == 200:
91
  query_data = query_response.json()
92
- response_message = f"{upload_status}\n\nResponse from Vectara API: {json.dumps(query_data, indent=2)}"
93
  else:
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")],
102
  outputs=gr.Textbox(label="Output Text"),
103
  title="Vectara Chatbot",
104
  description="Ask me anything using the Vectara API!"
 
3
  import json
4
  from decouple import Config
5
 
6
+ def query_vectara(question, chat_history):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  # Get the user's message from the chat history
8
  user_message = chat_history[-1][0]
9
 
10
  # Query Vectara API
11
+ customer_id = config('CUSTOMER_ID') # Read from .env file
12
+ corpus_id = config('CORPUS_ID') # Read from .env file
13
+ api_key = config('API_KEY') # Read from .env file
14
+
15
  query_url = "https://api.vectara.io/v1/query/v1/query"
16
 
17
  headers = {
 
68
 
69
  if query_response.status_code == 200:
70
  query_data = query_response.json()
71
+ response_message = f"Response from Vectara API: {json.dumps(query_data, indent=2)}"
72
  else:
73
+ response_message = f"Error: {query_response.status_code}"
74
 
75
  return response_message
76
 
77
+ # Create a Gradio ChatInterface with only a text input
78
  iface = gr.Interface(
79
  fn=query_vectara,
80
+ inputs=[gr.Textbox(label="Input Text")],
81
  outputs=gr.Textbox(label="Output Text"),
82
  title="Vectara Chatbot",
83
  description="Ask me anything using the Vectara API!"