Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,84 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
if response.status_code != 200:
|
| 14 |
-
return "Failed to get
|
| 15 |
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Step 2: Upload to S3
|
| 19 |
with open(file.name, "rb") as f:
|
| 20 |
-
|
| 21 |
|
| 22 |
-
if
|
| 23 |
-
|
|
|
|
| 24 |
else:
|
| 25 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
fn=upload_pdf,
|
| 29 |
-
inputs=gr.File(file_types=[".pdf"]),
|
| 30 |
-
outputs="text",
|
| 31 |
-
title="Upload PDF to S3",
|
| 32 |
-
description="Select a PDF file to upload to AWS S3 via pre-signed URL"
|
| 33 |
-
)
|
| 34 |
|
| 35 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
+
import uuid
|
| 4 |
+
import os
|
| 5 |
+
from datetime import datetime
|
| 6 |
|
| 7 |
+
# API Gateway endpoints (secured, NOT exposed in UI)
|
| 8 |
+
PRESIGN_API_URL = os.environ.get("PRESIGN_API_URL") # e.g., Lambda@API Gateway for presigned S3 URL
|
| 9 |
+
INFERENCE_API_URL = os.environ.get("INFERENCE_API_URL") # e.g., Lambda B endpoint for LLM QA
|
| 10 |
|
| 11 |
+
# Session state
|
| 12 |
+
user_id = "johndoe" # This could also be a login-generated ID
|
| 13 |
+
session_id = datetime.now().strftime("%Y%m%d_%H%M%S") + "_" + str(uuid.uuid4())[:8]
|
| 14 |
+
|
| 15 |
+
upload_success = False
|
| 16 |
|
| 17 |
+
def upload_pdf(file):
|
| 18 |
+
global upload_success
|
| 19 |
+
filename = os.path.basename(file.name)
|
| 20 |
+
|
| 21 |
+
# Step 1: Get presigned URL from API
|
| 22 |
+
response = requests.post(PRESIGN_API_URL, json={
|
| 23 |
+
"filename": filename,
|
| 24 |
+
"user_id": user_id,
|
| 25 |
+
"session_id": session_id
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
if response.status_code != 200:
|
| 29 |
+
return "β Failed to get presigned URL"
|
| 30 |
|
| 31 |
+
data = response.json()
|
| 32 |
+
presigned_url = data["url"]
|
| 33 |
+
content_type = data.get("content_type", "application/pdf")
|
| 34 |
|
| 35 |
# Step 2: Upload to S3
|
| 36 |
with open(file.name, "rb") as f:
|
| 37 |
+
upload_resp = requests.put(presigned_url, data=f, headers={"Content-Type": content_type})
|
| 38 |
|
| 39 |
+
if upload_resp.status_code == 200:
|
| 40 |
+
upload_success = True
|
| 41 |
+
return f"β
Uploaded and indexing triggered! You can now ask questions."
|
| 42 |
else:
|
| 43 |
+
return "β Upload failed!"
|
| 44 |
+
|
| 45 |
+
def chat_with_doc(message, history):
|
| 46 |
+
if not upload_success:
|
| 47 |
+
return "Please upload a PDF first.", history
|
| 48 |
+
|
| 49 |
+
if message.lower() == "exit":
|
| 50 |
+
return "π Session ended. Thank you!", []
|
| 51 |
+
|
| 52 |
+
payload = {
|
| 53 |
+
"question": message,
|
| 54 |
+
"user_id": user_id,
|
| 55 |
+
"session_id": session_id,
|
| 56 |
+
"chat_history": history
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
resp = requests.post(INFERENCE_API_URL, json=payload)
|
| 60 |
+
|
| 61 |
+
if resp.status_code != 200:
|
| 62 |
+
return "β Error calling LLM. Try again later.", history
|
| 63 |
+
|
| 64 |
+
answer = resp.json()["answer"]
|
| 65 |
+
history.append((message, answer))
|
| 66 |
+
return "", history
|
| 67 |
+
|
| 68 |
+
with gr.Blocks() as demo:
|
| 69 |
+
gr.Markdown("## π Upload your PDF and Ask Questions")
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
file_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 73 |
+
upload_button = gr.Button("Upload")
|
| 74 |
+
upload_output = gr.Textbox(label="Upload Status")
|
| 75 |
+
|
| 76 |
+
upload_button.click(upload_pdf, inputs=file_input, outputs=upload_output)
|
| 77 |
+
|
| 78 |
+
chatbot = gr.Chatbot(label="Document Chat")
|
| 79 |
+
question = gr.Textbox(label="Ask a question (or type 'exit' to end)", placeholder="Ask something about the document...")
|
| 80 |
+
ask_button = gr.Button("Send")
|
| 81 |
|
| 82 |
+
ask_button.click(chat_with_doc, inputs=[question, chatbot], outputs=[question, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
demo.launch()
|