Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,50 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import sys
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
temperature=0.7
|
| 19 |
-
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
return response.choices[0].text.strip()
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
def chat_with_pdf(content):
|
| 5 |
+
headers = {
|
| 6 |
+
"x-api-key": "sec_xxxxxx", # Replace with your API key
|
| 7 |
+
"Content-Type": "application/json",
|
| 8 |
+
}
|
| 9 |
|
| 10 |
+
data = {
|
| 11 |
+
"stream": True,
|
| 12 |
+
"sourceId": "src_xxxxxx", # Replace with your source ID
|
| 13 |
+
"messages": [
|
| 14 |
+
{
|
| 15 |
+
"role": "user",
|
| 16 |
+
"content": content,
|
| 17 |
+
},
|
| 18 |
+
],
|
| 19 |
+
}
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
url = "https://api.chatpdf.com/v1/chats/message"
|
|
|
|
| 22 |
|
| 23 |
+
try:
|
| 24 |
+
response = requests.post(url, json=data, headers=headers, stream=True)
|
| 25 |
+
response.raise_for_status()
|
| 26 |
|
| 27 |
+
if response.iter_content:
|
| 28 |
+
max_chunk_size = 1024
|
| 29 |
+
chat_response = ""
|
| 30 |
+
for chunk in response.iter_content(max_chunk_size):
|
| 31 |
+
text = chunk.decode()
|
| 32 |
+
chat_response += text.strip()
|
| 33 |
+
return chat_response
|
| 34 |
+
else:
|
| 35 |
+
return "No data received"
|
| 36 |
+
except requests.exceptions.RequestException as error:
|
| 37 |
+
return str(error)
|
| 38 |
+
|
| 39 |
+
# Create Gradio interface
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=chat_with_pdf,
|
| 42 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Ask something..."),
|
| 43 |
+
outputs="text",
|
| 44 |
+
title="ChatPDF Query Interface",
|
| 45 |
+
description="Type your question to get answers from PDFs."
|
| 46 |
)
|
| 47 |
+
|
| 48 |
+
# Run the Gradio app
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
iface.launch()
|