Spaces:
Sleeping
Sleeping
João Lima
commited on
Commit
·
87fe6ce
1
Parent(s):
39e8d67
updating app.py
Browse files
app.py
CHANGED
|
@@ -4,49 +4,49 @@ from rag.pipeline import run_rag
|
|
| 4 |
|
| 5 |
vectorstore = None
|
| 6 |
|
| 7 |
-
def
|
| 8 |
global vectorstore
|
| 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 |
-
)
|
|
|
|
| 4 |
|
| 5 |
vectorstore = None
|
| 6 |
|
| 7 |
+
def process_and_query(file, question):
|
| 8 |
global vectorstore
|
| 9 |
+
|
| 10 |
+
status = ""
|
| 11 |
+
answer = ""
|
| 12 |
+
sources = ""
|
| 13 |
+
evaluation = ""
|
| 14 |
+
|
| 15 |
+
# Processar PDF se fornecido
|
| 16 |
+
if file is not None:
|
| 17 |
+
try:
|
| 18 |
+
vectorstore = process_pdf(file.name)
|
| 19 |
+
status = "✓ Document processed successfully"
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return f"❌ Error processing PDF: {str(e)}", "", "", ""
|
| 22 |
+
|
| 23 |
+
# Responder pergunta
|
| 24 |
+
if question and question.strip():
|
| 25 |
+
if vectorstore is None:
|
| 26 |
+
answer = "⚠ Please upload a PDF first"
|
| 27 |
+
else:
|
| 28 |
+
try:
|
| 29 |
+
answer, sources, evaluation = run_rag(question, vectorstore)
|
| 30 |
+
except Exception as e:
|
| 31 |
+
answer = f"❌ Error: {str(e)}"
|
| 32 |
+
|
| 33 |
+
return status, answer, sources, evaluation
|
| 34 |
+
|
| 35 |
+
demo = gr.Interface(
|
| 36 |
+
fn=process_and_query,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.File(label="Upload PDF", file_types=[".pdf"]),
|
| 39 |
+
gr.Textbox(label="Question", placeholder="Ask about the document...")
|
| 40 |
+
],
|
| 41 |
+
outputs=[
|
| 42 |
+
gr.Textbox(label="Status"),
|
| 43 |
+
gr.Textbox(label="Answer", lines=5),
|
| 44 |
+
gr.Textbox(label="Sources", lines=2),
|
| 45 |
+
gr.Textbox(label="Evaluation", lines=3)
|
| 46 |
+
],
|
| 47 |
+
title="Tech Explainer — RAG with Automatic Evaluation",
|
| 48 |
+
description="Upload a PDF and ask questions about it",
|
| 49 |
+
theme=gr.themes.Soft()
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
demo.launch(share=False, server_name="0.0.0.0", server_port=7860)
|