Spaces:
Sleeping
Sleeping
File size: 2,483 Bytes
56b51e8 c5e6176 56b51e8 cfb6902 56b51e8 cfb6902 c5e6176 56b51e8 c5e6176 f7ebc00 c5e6176 f7ebc00 cfb6902 56b51e8 c5e6176 56b51e8 c5e6176 56b51e8 c5e6176 0386526 c5e6176 56b51e8 c5e6176 56b51e8 c5e6176 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import gradio as gr
import requests
import uuid
import os
from datetime import datetime
PRESIGN_API_URL = os.environ.get("PRESIGN_API_URL")
INFERENCE_API_URL = os.environ.get("INFERENCE_API_URL")
user_id = "user123"
session_id = datetime.now().strftime("%Y%m%d_%H%M%S") + "_" + str(uuid.uuid4())[:8]
upload_success = False
def upload_pdf(file):
global upload_success
filename = os.path.basename(file.name)
content_type = "application/pdf"
response = requests.post(url=PRESIGN_API_URL, json={
"filename": filename,
"user_id": user_id,
"session_id": session_id,
"contentType": content_type
})
if response.status_code != 200:
return "β Failed to get presigned URL"
data = response.json()
presigned_url = data["url"]
content_type = data.get("content_type", "application/pdf")
with open(file.name, "rb") as f:
upload_resp = requests.put(presigned_url, data=f, headers={"Content-Type": content_type})
if upload_resp.status_code == 200:
upload_success = True
return f"β
Uploaded and indexing triggered! You can now ask questions."
else:
return "β Upload failed!"
def chat_with_doc(message, history):
if not upload_success:
return "Please upload a PDF first.", history
if message.lower() == "exit":
return "π Session ended. Thank you!", []
payload = {
"question": message,
"user_id": user_id,
"session_id": session_id,
"chat_history": history
}
resp = requests.post(INFERENCE_API_URL, json=payload)
if resp.status_code != 200:
return "β Error calling LLM. Try again later.", history
answer = resp.json()["answer"]
history.append((message, answer))
return "", history
with gr.Blocks() as demo:
gr.Markdown("## π Upload your PDF and Ask Questions")
with gr.Row():
file_input = gr.File(label="Upload PDF", file_types=[".pdf"])
upload_button = gr.Button("Upload")
upload_output = gr.Textbox(label="Upload Status")
upload_button.click(upload_pdf, inputs=file_input, outputs=upload_output)
chatbot = gr.Chatbot(label="Document Chat")
question = gr.Textbox(label="Ask a question (or type 'exit' to end)", placeholder="Ask something about the document...")
ask_button = gr.Button("Send")
ask_button.click(chat_with_doc, inputs=[question, chatbot], outputs=[question, chatbot])
demo.launch()
|