Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -24,7 +24,7 @@ if hasattr(rag, "model"):
|
|
| 24 |
|
| 25 |
def extract_text_from_pdf_url(url):
|
| 26 |
try:
|
| 27 |
-
response = requests.get(url)
|
| 28 |
response.raise_for_status()
|
| 29 |
doc = fitz.open(stream=response.content, filetype="pdf")
|
| 30 |
text = ""
|
|
@@ -35,26 +35,35 @@ def extract_text_from_pdf_url(url):
|
|
| 35 |
return f"[Error loading PDF: {str(e)}]"
|
| 36 |
|
| 37 |
def generate_answer(query, pdf_urls_str):
|
|
|
|
|
|
|
|
|
|
| 38 |
pdf_urls = [url.strip() for url in pdf_urls_str.strip().split("\n") if url.strip()]
|
| 39 |
sources = []
|
| 40 |
feedback = "### PDF Load Report:\n"
|
| 41 |
|
| 42 |
for url in pdf_urls:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
if not sources:
|
| 54 |
return feedback + "\n❌ No valid PDFs were successfully processed."
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
iface = gr.Interface(
|
| 60 |
fn=generate_answer,
|
|
@@ -64,8 +73,9 @@ iface = gr.Interface(
|
|
| 64 |
],
|
| 65 |
outputs=gr.Markdown(label="Model Response"),
|
| 66 |
title="Pleias RAG PDF QA",
|
| 67 |
-
description="Ask a question and get answers grounded in the content of the uploaded PDF URLs using the Pleias RAG model."
|
|
|
|
| 68 |
)
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
iface.launch(
|
|
|
|
| 24 |
|
| 25 |
def extract_text_from_pdf_url(url):
|
| 26 |
try:
|
| 27 |
+
response = requests.get(url, timeout=10) # Added timeout
|
| 28 |
response.raise_for_status()
|
| 29 |
doc = fitz.open(stream=response.content, filetype="pdf")
|
| 30 |
text = ""
|
|
|
|
| 35 |
return f"[Error loading PDF: {str(e)}]"
|
| 36 |
|
| 37 |
def generate_answer(query, pdf_urls_str):
|
| 38 |
+
if not query or not pdf_urls_str: # Added input validation
|
| 39 |
+
return "Please provide both a question and at least one PDF URL"
|
| 40 |
+
|
| 41 |
pdf_urls = [url.strip() for url in pdf_urls_str.strip().split("\n") if url.strip()]
|
| 42 |
sources = []
|
| 43 |
feedback = "### PDF Load Report:\n"
|
| 44 |
|
| 45 |
for url in pdf_urls:
|
| 46 |
+
try:
|
| 47 |
+
text = extract_text_from_pdf_url(url)
|
| 48 |
+
if not text.startswith("[Error"):
|
| 49 |
+
sources.append({
|
| 50 |
+
"text": text,
|
| 51 |
+
"metadata": {"source": url}
|
| 52 |
+
})
|
| 53 |
+
feedback += f"- ✅ Loaded: {url[:80]}\n"
|
| 54 |
+
else:
|
| 55 |
+
feedback += f"- ❌ Failed: {url[:80]}\n"
|
| 56 |
+
except Exception as e:
|
| 57 |
+
feedback += f"- ❌ Error processing {url[:80]}: {str(e)}\n"
|
| 58 |
|
| 59 |
if not sources:
|
| 60 |
return feedback + "\n❌ No valid PDFs were successfully processed."
|
| 61 |
|
| 62 |
+
try:
|
| 63 |
+
response = rag.generate(query, sources)
|
| 64 |
+
return feedback + f"\n\n### Answer:\n{response['raw_response']}\n\n_Backend used: {response['backend_used']}_"
|
| 65 |
+
except Exception as e:
|
| 66 |
+
return feedback + f"\n\n❌ Error generating answer: {str(e)}"
|
| 67 |
|
| 68 |
iface = gr.Interface(
|
| 69 |
fn=generate_answer,
|
|
|
|
| 73 |
],
|
| 74 |
outputs=gr.Markdown(label="Model Response"),
|
| 75 |
title="Pleias RAG PDF QA",
|
| 76 |
+
description="Ask a question and get answers grounded in the content of the uploaded PDF URLs using the Pleias RAG model.",
|
| 77 |
+
allow_flagging="never" # Disable flagging to simplify interface
|
| 78 |
)
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
+
iface.launch(server_port=7860, server_name="0.0.0.0", show_error=True) # Added explicit server settings
|