stephenmccartney1234 commited on
Commit
9ffe2c6
·
verified ·
1 Parent(s): 90626d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -1,16 +1,20 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
  def chat_with_pdf(content):
5
- api_key = os.getenv('CHATPDFKEY')
 
 
 
6
 
7
- headers = {
8
  "x-api-key": api_key,
9
  "Content-Type": "application/json",
10
  }
11
 
12
- data = {
13
- #"stream": True,
14
  "sourceId": "src_REdjHAwIpm8Lhuk2HAtnm", # Replace with your source ID
15
  "messages": [
16
  {
@@ -20,7 +24,7 @@ data = {
20
  ],
21
  }
22
 
23
- url = "https://api.chatpdf.com/v1/chats/message"
24
 
25
  try:
26
  response = requests.post(url, json=data, headers=headers, stream=True)
@@ -35,8 +39,10 @@ url = "https://api.chatpdf.com/v1/chats/message"
35
  return chat_response
36
  else:
37
  return "No data received"
 
 
38
  except requests.exceptions.RequestException as error:
39
- return str(error)
40
 
41
  # Create Gradio interface
42
  iface = gr.Interface(
@@ -44,7 +50,7 @@ iface = gr.Interface(
44
  inputs=gr.inputs.Textbox(lines=2, placeholder="Ask something..."),
45
  outputs="text",
46
  title="ChatPDF Query Interface",
47
- description="Type your question to get answers from PDFs."
48
  )
49
 
50
  # Run the Gradio app
 
1
  import gradio as gr
2
  import requests
3
+ import os # Import the os module to access environment variables
4
 
5
  def chat_with_pdf(content):
6
+ # Use the CHATPDFKEY environment variable for the API key
7
+ api_key = os.getenv('CHATPDFKEY')
8
+ if not api_key:
9
+ return "API key (CHATPDFKEY) not set in environment variables."
10
 
11
+ headers = {
12
  "x-api-key": api_key,
13
  "Content-Type": "application/json",
14
  }
15
 
16
+ data = {
17
+ "stream": True,
18
  "sourceId": "src_REdjHAwIpm8Lhuk2HAtnm", # Replace with your source ID
19
  "messages": [
20
  {
 
24
  ],
25
  }
26
 
27
+ url = "https://api.chatpdf.com/v1/chats/message"
28
 
29
  try:
30
  response = requests.post(url, json=data, headers=headers, stream=True)
 
39
  return chat_response
40
  else:
41
  return "No data received"
42
+ except requests.exceptions.HTTPError as error:
43
+ return f"HTTP Error: {error.response.status_code} - {error.response.text}"
44
  except requests.exceptions.RequestException as error:
45
+ return f"Request Exception: {error}"
46
 
47
  # Create Gradio interface
48
  iface = gr.Interface(
 
50
  inputs=gr.inputs.Textbox(lines=2, placeholder="Ask something..."),
51
  outputs="text",
52
  title="ChatPDF Query Interface",
53
+ description="Type your question to get answers from PDFs. Make sure the CHATPDFKEY environment variable is set."
54
  )
55
 
56
  # Run the Gradio app