Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,52 +1,52 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import requests
|
| 3 |
-
|
| 4 |
-
BACKEND_URL = "
|
| 5 |
-
|
| 6 |
-
def upload_document(file, url):
|
| 7 |
-
if file:
|
| 8 |
-
with open(file.name, "rb") as f:
|
| 9 |
-
response = requests.post(f"{BACKEND_URL}/upload", files = {"file": f})
|
| 10 |
-
elif url:
|
| 11 |
-
response = requests.post(f"{BACKEND_URL}/upload", data = {"url": url})
|
| 12 |
-
else:
|
| 13 |
-
return None, "Provide a file or URL"
|
| 14 |
-
|
| 15 |
-
session_id = response.json()["session_id"]
|
| 16 |
-
return session_id, "Document uploaded successfully!"
|
| 17 |
-
|
| 18 |
-
def chat(question, session_id, history):
|
| 19 |
-
if not session_id:
|
| 20 |
-
return history, "Please upload a document first."
|
| 21 |
-
|
| 22 |
-
history.append({"role": "user", "content": question})
|
| 23 |
-
history.append({"role": "assistant", "content": ""})
|
| 24 |
-
|
| 25 |
-
with requests.post(
|
| 26 |
-
f"{BACKEND_URL}/chat",
|
| 27 |
-
data={"session_id": session_id, "question": question},
|
| 28 |
-
stream=True
|
| 29 |
-
) as response:
|
| 30 |
-
for token in response.iter_content(chunk_size=None, decode_unicode=True):
|
| 31 |
-
history[-1]["content"] += token
|
| 32 |
-
yield history, ""
|
| 33 |
-
|
| 34 |
-
with gr.Blocks() as demo:
|
| 35 |
-
session_id = gr.State(None)
|
| 36 |
-
|
| 37 |
-
gr.Markdown('# Document Q&A')
|
| 38 |
-
|
| 39 |
-
with gr.Row():
|
| 40 |
-
file_input = gr.File(label = "Upload PDF")
|
| 41 |
-
url_input = gr.Textbox(label = "Or paste a URL")
|
| 42 |
-
|
| 43 |
-
upload_btn = gr.Button("Upload")
|
| 44 |
-
upload_status = gr.Textbox(label = "Status", interactive = False)
|
| 45 |
-
|
| 46 |
-
chatbot = gr.Chatbot()
|
| 47 |
-
question_input = gr.Textbox(label = "Ask a question")
|
| 48 |
-
|
| 49 |
-
upload_btn.click(upload_document, inputs=[file_input, url_input], outputs=[session_id, upload_status])
|
| 50 |
-
question_input.submit(chat, inputs=[question_input, session_id, chatbot], outputs=[chatbot, question_input])
|
| 51 |
-
|
| 52 |
-
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
BACKEND_URL = "https://document-qa-lmr9.onrender.com"
|
| 5 |
+
|
| 6 |
+
def upload_document(file, url):
|
| 7 |
+
if file:
|
| 8 |
+
with open(file.name, "rb") as f:
|
| 9 |
+
response = requests.post(f"{BACKEND_URL}/upload", files = {"file": f})
|
| 10 |
+
elif url:
|
| 11 |
+
response = requests.post(f"{BACKEND_URL}/upload", data = {"url": url})
|
| 12 |
+
else:
|
| 13 |
+
return None, "Provide a file or URL"
|
| 14 |
+
|
| 15 |
+
session_id = response.json()["session_id"]
|
| 16 |
+
return session_id, "Document uploaded successfully!"
|
| 17 |
+
|
| 18 |
+
def chat(question, session_id, history):
|
| 19 |
+
if not session_id:
|
| 20 |
+
return history, "Please upload a document first."
|
| 21 |
+
|
| 22 |
+
history.append({"role": "user", "content": question})
|
| 23 |
+
history.append({"role": "assistant", "content": ""})
|
| 24 |
+
|
| 25 |
+
with requests.post(
|
| 26 |
+
f"{BACKEND_URL}/chat",
|
| 27 |
+
data={"session_id": session_id, "question": question},
|
| 28 |
+
stream=True
|
| 29 |
+
) as response:
|
| 30 |
+
for token in response.iter_content(chunk_size=None, decode_unicode=True):
|
| 31 |
+
history[-1]["content"] += token
|
| 32 |
+
yield history, ""
|
| 33 |
+
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
session_id = gr.State(None)
|
| 36 |
+
|
| 37 |
+
gr.Markdown('# Document Q&A')
|
| 38 |
+
|
| 39 |
+
with gr.Row():
|
| 40 |
+
file_input = gr.File(label = "Upload PDF")
|
| 41 |
+
url_input = gr.Textbox(label = "Or paste a URL")
|
| 42 |
+
|
| 43 |
+
upload_btn = gr.Button("Upload")
|
| 44 |
+
upload_status = gr.Textbox(label = "Status", interactive = False)
|
| 45 |
+
|
| 46 |
+
chatbot = gr.Chatbot()
|
| 47 |
+
question_input = gr.Textbox(label = "Ask a question")
|
| 48 |
+
|
| 49 |
+
upload_btn.click(upload_document, inputs=[file_input, url_input], outputs=[session_id, upload_status])
|
| 50 |
+
question_input.submit(chat, inputs=[question_input, session_id, chatbot], outputs=[chatbot, question_input])
|
| 51 |
+
|
| 52 |
+
demo.launch()
|