Spaces:
Runtime error
Runtime error
File size: 1,808 Bytes
b6035c5 c924889 9ffe2c6 f52954a c924889 9ffe2c6 aea4761 c924889 b6035c5 9ffe2c6 aea4761 75a0efd 3097d03 c924889 aea4761 c924889 b6035c5 aea4761 b6035c5 c924889 b6035c5 c924889 64d5c2f c924889 9ffe2c6 c924889 9ffe2c6 c924889 9ffe2c6 b6035c5 c924889 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
import gradio as gr
import requests
import os # Import the os module to access environment variables
def chat_with_pdf(content):
# Use the CHATPDFKEY environment variable for the API key
api_key = os.getenv('CHATPDFKEY')
if not api_key:
return "API key (CHATPDFKEY) not set in environment variables."
headers = {
'x-api-key': api_key,
"Content-Type": "application/json",
}
data = {
'stream': True,
'sourceId': 'src_REdjHAwIpm8Lhuk2HAtnm', # Replace with your source ID
'messages': [
{
'role': "user",
'content': content,
},
],
}
url = 'https://api.chatpdf.com/v1/chats/message'
try:
response = requests.post(url, json=data, headers=headers, stream=True)
response.raise_for_status()
if response.iter_content:
max_chunk_size = 1023
chat_response = ""
for chunk in response.iter_content(max_chunk_size):
text = chunk.decode()
chat_response += text.strip()
return chat_response
else:
return "No data received"
except requests.exceptions.HTTPError as error:
return f"HTTP Error: {error.response.status_code} - {error.response.text}"
except requests.exceptions.RequestException as error:
return f"Request Exception: {error}"
# Create Gradio interface
iface = gr.Interface(
fn=chat_with_pdf,
inputs=gr.inputs.Textbox(lines=2, placeholder="Ask something..."),
outputs="text",
title="ChatPDF Query Interface",
description="Type your question to get answers from PDFs. Make sure the CHATPDFKEY environment variable is set."
)
# Run the Gradio app
if __name__ == "__main__":
iface.launch()
|