Spaces:
Sleeping
Sleeping
Hamid Omarov
commited on
Commit
·
0ebeee2
1
Parent(s):
3a703d4
Add Gradio RAG UI
Browse files- day3/gradio_rag.py +44 -0
- day3/requirements.txt +6 -0
day3/gradio_rag.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# day3/gradio_rag.py
|
2 |
+
import gradio as gr
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
from rag_system import RAGPipeline
|
5 |
+
import traceback
|
6 |
+
import os
|
7 |
+
|
8 |
+
load_dotenv() # ensure GROQ_API_KEY is loaded for ChatGroq
|
9 |
+
|
10 |
+
# Use a writable persistent dir
|
11 |
+
rag = RAGPipeline(persist_dir="./chroma_db_space", collection_name="pdf_docs")
|
12 |
+
|
13 |
+
def chat_with_pdf(pdf_path: str, question: str):
|
14 |
+
try:
|
15 |
+
if not pdf_path:
|
16 |
+
return "Please upload a PDF."
|
17 |
+
if not question or not question.strip():
|
18 |
+
return "Please enter a question."
|
19 |
+
|
20 |
+
# Index the uploaded PDF (path is a string because of type='filepath')
|
21 |
+
rag.index_document(pdf_path, doc_id_prefix="upload")
|
22 |
+
|
23 |
+
# Ask
|
24 |
+
out = rag.query(question, k=4)
|
25 |
+
return out["answer"]
|
26 |
+
except Exception as e:
|
27 |
+
# Surface the exact error to the UI for debugging
|
28 |
+
return f"Error: {e}\n\n{traceback.format_exc()}"
|
29 |
+
|
30 |
+
demo = gr.Interface(
|
31 |
+
fn=chat_with_pdf,
|
32 |
+
inputs=[
|
33 |
+
gr.File(label="Upload PDF", file_types=[".pdf"], type="filepath"),
|
34 |
+
gr.Textbox(label="Ask a question", placeholder="What does the PDF say?"),
|
35 |
+
],
|
36 |
+
outputs=gr.Textbox(label="Answer"),
|
37 |
+
title="PDF RAG (Chroma + Groq)",
|
38 |
+
description="Upload a PDF and ask a question. Uses Chroma for retrieval and Groq LLM for answers."
|
39 |
+
)
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
# Optional: show whether the env var is visible to this process
|
43 |
+
print("GROQ key present:", bool(os.getenv("GROQ_API_KEY")))
|
44 |
+
demo.launch()
|
day3/requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
chromadb
|
3 |
+
sentence-transformers
|
4 |
+
langchain-groq
|
5 |
+
pypdf
|
6 |
+
python-dotenv
|