Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,93 +9,91 @@ from langchain.chains import RetrievalQA
|
|
| 9 |
|
| 10 |
from pinecone import Pinecone, ServerlessSpec
|
| 11 |
|
| 12 |
-
#
|
| 13 |
INDEX_NAME = "rag-demo-index"
|
|
|
|
| 14 |
|
| 15 |
-
def process_rag(
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
if not pdf_file:
|
| 20 |
-
return "β Please upload a PDF file."
|
| 21 |
|
| 22 |
try:
|
| 23 |
-
# Step 1: Load and
|
| 24 |
loader = PyPDFLoader(pdf_file.name)
|
| 25 |
documents = loader.load()
|
| 26 |
|
| 27 |
-
|
| 28 |
-
docs =
|
| 29 |
|
| 30 |
-
# Step 2:
|
| 31 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 32 |
model="models/embedding-001",
|
| 33 |
-
google_api_key=
|
| 34 |
)
|
| 35 |
|
| 36 |
-
# Step 3:
|
| 37 |
-
pc = Pinecone(api_key=
|
| 38 |
|
| 39 |
-
# Step 4: Create index if it doesn't exist
|
| 40 |
if INDEX_NAME not in pc.list_indexes().names():
|
| 41 |
pc.create_index(
|
| 42 |
name=INDEX_NAME,
|
| 43 |
-
dimension=
|
| 44 |
metric="cosine",
|
| 45 |
-
spec=ServerlessSpec(
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
-
# Step
|
| 49 |
-
|
| 50 |
-
docs,
|
| 51 |
embedding=embeddings,
|
| 52 |
-
index_name=INDEX_NAME
|
| 53 |
-
api_key=api_key_pinecone,
|
| 54 |
-
environment=pinecone_region
|
| 55 |
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
retriever = vectordb.as_retriever()
|
| 59 |
|
| 60 |
-
# Step
|
| 61 |
llm = ChatGoogleGenerativeAI(
|
| 62 |
model="gemini-pro",
|
| 63 |
-
google_api_key=
|
| 64 |
temperature=0
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
qa_chain = RetrievalQA.from_chain_type(
|
| 68 |
llm=llm,
|
| 69 |
retriever=retriever,
|
| 70 |
return_source_documents=False
|
| 71 |
)
|
| 72 |
|
| 73 |
-
# Step 8: Ask the question
|
| 74 |
result = qa_chain({"query": user_question})
|
| 75 |
return result["result"]
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
return f"β Error: {str(e)}"
|
| 79 |
|
| 80 |
-
# Gradio
|
| 81 |
-
with gr.Blocks() as
|
| 82 |
-
gr.Markdown("##
|
| 83 |
|
| 84 |
with gr.Row():
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
pdf_file = gr.File(label="π Upload your PDF", file_types=[".pdf"])
|
| 90 |
-
user_question = gr.Textbox(label="β Ask a Question")
|
| 91 |
-
answer_output = gr.Textbox(label="π€ Gemini Answer", lines=10)
|
| 92 |
|
| 93 |
-
ask_button = gr.Button("
|
| 94 |
|
| 95 |
-
ask_button.click(
|
| 96 |
-
fn=process_rag,
|
| 97 |
-
inputs=[gemini_key, pinecone_key, pinecone_region, pdf_file, user_question],
|
| 98 |
-
outputs=answer_output
|
| 99 |
-
)
|
| 100 |
|
| 101 |
-
|
|
|
|
| 9 |
|
| 10 |
from pinecone import Pinecone, ServerlessSpec
|
| 11 |
|
| 12 |
+
# Constants
|
| 13 |
INDEX_NAME = "rag-demo-index"
|
| 14 |
+
DIMENSION = 768 # Use 768 for Gemini embeddings
|
| 15 |
|
| 16 |
+
def process_rag(pdf_file, user_question):
|
| 17 |
+
# π Load from Hugging Face Secrets
|
| 18 |
+
pinecone_api_key = os.environ.get("PINECONE_API_KEY")
|
| 19 |
+
pinecone_env = os.environ.get("PINECONE_ENVIRONMENT")
|
| 20 |
+
google_api_key = os.environ.get("GOOGLE_API_KEY")
|
| 21 |
+
|
| 22 |
+
if not all([pinecone_api_key, pinecone_env, google_api_key]):
|
| 23 |
+
return "β Missing required secrets. Check PINECONE_API_KEY, PINECONE_ENVIRONMENT, or GOOGLE_API_KEY."
|
| 24 |
|
| 25 |
if not pdf_file:
|
| 26 |
+
return "β Please upload a PDF file first."
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
# Step 1: Load PDF and chunk it
|
| 30 |
loader = PyPDFLoader(pdf_file.name)
|
| 31 |
documents = loader.load()
|
| 32 |
|
| 33 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 34 |
+
docs = text_splitter.split_documents(documents)
|
| 35 |
|
| 36 |
+
# Step 2: Embeddings via Gemini
|
| 37 |
embeddings = GoogleGenerativeAIEmbeddings(
|
| 38 |
model="models/embedding-001",
|
| 39 |
+
google_api_key=google_api_key
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Step 3: Connect to Pinecone v3
|
| 43 |
+
pc = Pinecone(api_key=pinecone_api_key)
|
| 44 |
|
|
|
|
| 45 |
if INDEX_NAME not in pc.list_indexes().names():
|
| 46 |
pc.create_index(
|
| 47 |
name=INDEX_NAME,
|
| 48 |
+
dimension=DIMENSION,
|
| 49 |
metric="cosine",
|
| 50 |
+
spec=ServerlessSpec(
|
| 51 |
+
cloud="aws",
|
| 52 |
+
region=pinecone_env
|
| 53 |
+
)
|
| 54 |
)
|
| 55 |
|
| 56 |
+
# Step 4: Store docs in Pinecone
|
| 57 |
+
vectorstore = LangChainPinecone.from_documents(
|
| 58 |
+
documents=docs,
|
| 59 |
embedding=embeddings,
|
| 60 |
+
index_name=INDEX_NAME
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
+
retriever = vectorstore.as_retriever()
|
|
|
|
| 64 |
|
| 65 |
+
# Step 5: Gemini chat model
|
| 66 |
llm = ChatGoogleGenerativeAI(
|
| 67 |
model="gemini-pro",
|
| 68 |
+
google_api_key=google_api_key,
|
| 69 |
temperature=0
|
| 70 |
)
|
| 71 |
|
| 72 |
+
# Step 6: RAG chain
|
| 73 |
qa_chain = RetrievalQA.from_chain_type(
|
| 74 |
llm=llm,
|
| 75 |
retriever=retriever,
|
| 76 |
return_source_documents=False
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
result = qa_chain({"query": user_question})
|
| 80 |
return result["result"]
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
return f"β Error: {str(e)}"
|
| 84 |
|
| 85 |
+
# Gradio UI
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("## π Ask Questions from PDF using Gemini + Pinecone RAG")
|
| 88 |
|
| 89 |
with gr.Row():
|
| 90 |
+
pdf_input = gr.File(label="π Upload PDF", file_types=[".pdf"])
|
| 91 |
+
question_input = gr.Textbox(label="β Ask your question")
|
| 92 |
+
|
| 93 |
+
answer_output = gr.Textbox(label="π€ Gemini Answer", lines=8)
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
+
ask_button = gr.Button("π Run RAG")
|
| 96 |
|
| 97 |
+
ask_button.click(fn=process_rag, inputs=[pdf_input, question_input], outputs=answer_output)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
+
demo.launch()
|