Tonic commited on
Commit
ea7a2b9
1 Parent(s): bb31795

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -22
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
- customer_id = config('CUSTOMER_ID') # Read from .env file
9
- corpus_id = config('CORPUS_ID') # Read from .env file
10
- api_key = config('API_KEY') # Read from .env file
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, headers=post_headers)
23
 
24
- if response.status_code == 200:
25
- upload_status = "File uploaded successfully"
 
 
26
  else:
27
- upload_status = "Failed to upload the file"
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, a file upload input, and a text output
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()