Spaces:
Runtime error
Runtime error
First Release
Browse files- .env +1 -0
- app.py +81 -0
- requirements.txt +7 -0
.env
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
GOOGLE_API_KEY=AIzaSyDz-X-hSJYnnllhZ6odQM03J7vSO6pdMYA
|
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain.prompts import PromptTemplate
|
3 |
+
from langchain.chains.question_answering import load_qa_chain
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.vectorstores import Chroma
|
6 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings, ChatGoogleGenerativeAI
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
import PyPDF2
|
9 |
+
import os
|
10 |
+
import io
|
11 |
+
|
12 |
+
st.title("Chat Your PDFs") # Updated title
|
13 |
+
|
14 |
+
# Load environment variables from .env file
|
15 |
+
load_dotenv()
|
16 |
+
|
17 |
+
# Retrieve API key from environment variable
|
18 |
+
google_api_key = os.getenv("GOOGLE_API_KEY")
|
19 |
+
|
20 |
+
# Check if the API key is available
|
21 |
+
if google_api_key is None:
|
22 |
+
st.warning("API key not found. Please set the google_api_key environment variable.")
|
23 |
+
st.stop()
|
24 |
+
|
25 |
+
# File Upload with user-defined name
|
26 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=["pdf"])
|
27 |
+
|
28 |
+
if uploaded_file is not None:
|
29 |
+
st.text("PDF File Uploaded Successfully!")
|
30 |
+
|
31 |
+
# PDF Processing (using PyPDF2 directly)
|
32 |
+
pdf_data = uploaded_file.read()
|
33 |
+
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_data))
|
34 |
+
pdf_pages = pdf_reader.pages
|
35 |
+
|
36 |
+
# Create Context
|
37 |
+
context = "\n\n".join(page.extract_text() for page in pdf_pages)
|
38 |
+
|
39 |
+
# Split Texts
|
40 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=10000, chunk_overlap=200)
|
41 |
+
texts = text_splitter.split_text(context)
|
42 |
+
|
43 |
+
# Chroma Embeddings
|
44 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
45 |
+
vector_index = Chroma.from_texts(texts, embeddings).as_retriever()
|
46 |
+
|
47 |
+
# Get User Question
|
48 |
+
user_question = st.text_input("Ask a Question:")
|
49 |
+
|
50 |
+
if st.button("Get Answer"):
|
51 |
+
if user_question:
|
52 |
+
# Get Relevant Documents
|
53 |
+
docs = vector_index.get_relevant_documents(user_question)
|
54 |
+
|
55 |
+
# Define Prompt Template
|
56 |
+
prompt_template = """
|
57 |
+
Answer the question as detailed as possible from the provided context,
|
58 |
+
make sure to provide all the details, if the answer is not in
|
59 |
+
provided context just say, "answer is not available in the context",
|
60 |
+
don't provide the wrong answer\n\n
|
61 |
+
Context:\n {context}?\n
|
62 |
+
Question: \n{question}\n
|
63 |
+
Answer:
|
64 |
+
"""
|
65 |
+
|
66 |
+
# Create Prompt
|
67 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=['context', 'question'])
|
68 |
+
|
69 |
+
# Load QA Chain
|
70 |
+
model = ChatGoogleGenerativeAI(model="gemini-pro", temperature=0.3, api_key=google_api_key)
|
71 |
+
chain = load_qa_chain(model, chain_type="stuff", prompt=prompt)
|
72 |
+
|
73 |
+
# Get Response
|
74 |
+
response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True)
|
75 |
+
|
76 |
+
# Display Answer
|
77 |
+
st.subheader("Answer:")
|
78 |
+
st.write(response['output_text'])
|
79 |
+
|
80 |
+
else:
|
81 |
+
st.warning("Please enter a question.")
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
google-generativeai
|
3 |
+
streamlit
|
4 |
+
langchain-google-genai
|
5 |
+
python-dotenv
|
6 |
+
chromadb
|
7 |
+
pypdf
|