Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from ingestion.pdf import process_pdf | |
| from rag.pipeline import run_rag | |
| vectorstore = None | |
| def load_document(file): | |
| global vectorstore | |
| if file is None: | |
| return "Please upload a PDF file." | |
| try: | |
| vectorstore = process_pdf(file.name) | |
| return "✓ Document processed successfully." | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}" | |
| def ask(question): | |
| if vectorstore is None: | |
| return "⚠ Upload a document first", "", "" | |
| if not question.strip(): | |
| return "⚠ Please enter a question", "", "" | |
| try: | |
| return run_rag(question, vectorstore) | |
| except Exception as e: | |
| return f"❌ Error: {str(e)}", "", "" | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Tech Explainer — RAG with Automatic Evaluation") | |
| file = gr.File(label="Upload PDF", file_types=[".pdf"]) | |
| load_btn = gr.Button("Process PDF") | |
| status = gr.Textbox(label="Status") | |
| gr.Markdown("---") | |
| question = gr.Textbox(label="Question") | |
| ask_btn = gr.Button("Ask") | |
| answer = gr.Textbox(label="Answer", lines=5) | |
| sources = gr.Textbox(label="Sources", lines=2) | |
| evaluation = gr.Textbox(label="Evaluation", lines=3) | |
| load_btn.click(load_document, inputs=file, outputs=status) | |
| ask_btn.click(ask, inputs=question, outputs=[answer, sources, evaluation]) | |
| demo.launch() |