Spaces:
Sleeping
Sleeping
João Lima
commited on
Commit
·
9ef96a3
1
Parent(s):
87fe6ce
updating app.py
Browse files- app.py +39 -45
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -4,49 +4,43 @@ 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 |
-
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)
|
|
|
|
| 4 |
|
| 5 |
vectorstore = None
|
| 6 |
|
| 7 |
+
def load_document(file):
|
| 8 |
global vectorstore
|
| 9 |
+
if file is None:
|
| 10 |
+
return "Please upload a PDF file."
|
| 11 |
+
try:
|
| 12 |
+
vectorstore = process_pdf(file.name)
|
| 13 |
+
return "✓ Document processed successfully."
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"❌ Error: {str(e)}"
|
| 16 |
+
|
| 17 |
+
def ask(question):
|
| 18 |
+
if vectorstore is None:
|
| 19 |
+
return "⚠ Upload a document first", "", ""
|
| 20 |
+
if not question.strip():
|
| 21 |
+
return "⚠ Please enter a question", "", ""
|
| 22 |
+
try:
|
| 23 |
+
return run_rag(question, vectorstore)
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"❌ Error: {str(e)}", "", ""
|
| 26 |
+
|
| 27 |
+
with gr.Blocks() as demo:
|
| 28 |
+
gr.Markdown("# Tech Explainer — RAG with Automatic Evaluation")
|
| 29 |
+
|
| 30 |
+
file = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 31 |
+
load_btn = gr.Button("Process PDF")
|
| 32 |
+
status = gr.Textbox(label="Status")
|
| 33 |
+
|
| 34 |
+
gr.Markdown("---")
|
| 35 |
+
|
| 36 |
+
question = gr.Textbox(label="Question")
|
| 37 |
+
ask_btn = gr.Button("Ask")
|
| 38 |
+
|
| 39 |
+
answer = gr.Textbox(label="Answer", lines=5)
|
| 40 |
+
sources = gr.Textbox(label="Sources", lines=2)
|
| 41 |
+
evaluation = gr.Textbox(label="Evaluation", lines=3)
|
| 42 |
+
|
| 43 |
+
load_btn.click(load_document, inputs=file, outputs=status)
|
| 44 |
+
ask_btn.click(ask, inputs=question, outputs=[answer, sources, evaluation])
|
| 45 |
+
|
| 46 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
gradio==4.44.
|
|
|
|
| 2 |
transformers
|
| 3 |
sentence-transformers
|
| 4 |
faiss-cpu
|
|
|
|
| 1 |
+
gradio==4.44.0
|
| 2 |
+
pydantic==2.9.2
|
| 3 |
transformers
|
| 4 |
sentence-transformers
|
| 5 |
faiss-cpu
|