Spaces:
Runtime error
Runtime error
Create app
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import fitz # PyMuPDF
|
| 4 |
+
from pleias_rag_interface import RAGWithCitations
|
| 5 |
+
|
| 6 |
+
# Initialize the Pleias RAG model
|
| 7 |
+
rag = RAGWithCitations(model_path_or_name="PleIAs/Pleias-RAG-350M")
|
| 8 |
+
|
| 9 |
+
def extract_text_from_pdf_url(url):
|
| 10 |
+
try:
|
| 11 |
+
response = requests.get(url)
|
| 12 |
+
response.raise_for_status()
|
| 13 |
+
doc = fitz.open(stream=response.content, filetype="pdf")
|
| 14 |
+
text = ""
|
| 15 |
+
for page in doc:
|
| 16 |
+
text += page.get_text()
|
| 17 |
+
return text.strip()
|
| 18 |
+
except Exception as e:
|
| 19 |
+
return f"[Error loading PDF: {str(e)}]"
|
| 20 |
+
|
| 21 |
+
def generate_answer(query, pdf_urls_str):
|
| 22 |
+
pdf_urls = [url.strip() for url in pdf_urls_str.strip().split("\n") if url.strip()]
|
| 23 |
+
|
| 24 |
+
sources = []
|
| 25 |
+
for url in pdf_urls:
|
| 26 |
+
text = extract_text_from_pdf_url(url)
|
| 27 |
+
if not text.startswith("[Error"):
|
| 28 |
+
sources.append({
|
| 29 |
+
"text": text,
|
| 30 |
+
"metadata": {"source": url}
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
if not sources:
|
| 34 |
+
return "No valid PDFs found or unable to extract text."
|
| 35 |
+
|
| 36 |
+
response = rag.generate(query, sources)
|
| 37 |
+
return f"### Query:\n{query}\n\n### Answer:\n{response['raw_response']}\n\n### Source Info:\nBackend used: {response['backend_used']}"
|
| 38 |
+
|
| 39 |
+
# Gradio UI
|
| 40 |
+
iface = gr.Interface(
|
| 41 |
+
fn=generate_answer,
|
| 42 |
+
inputs=[
|
| 43 |
+
gr.Textbox(label="Your Question", placeholder="What is this document about?"),
|
| 44 |
+
gr.Textbox(lines=5, label="PDF URLs (one per line)", placeholder="https://example.com/doc1.pdf\nhttps://example.com/doc2.pdf")
|
| 45 |
+
],
|
| 46 |
+
outputs=gr.Markdown(label="Model Response"),
|
| 47 |
+
title="Pleias RAG PDF QA",
|
| 48 |
+
description="Ask a question and get answers grounded in the content of the uploaded PDF URLs using the Pleias RAG model."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
iface.launch()
|