Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,90 +1,78 @@
|
|
1 |
-
import streamlit as st
|
2 |
import os
|
|
|
|
|
|
|
3 |
from langchain.vectorstores import FAISS
|
4 |
-
from langchain.embeddings import OpenAIEmbeddings
|
5 |
from langchain.document_loaders import PyPDFLoader
|
6 |
-
from
|
7 |
-
|
8 |
-
|
9 |
-
GROQ_API_KEY = "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"
|
10 |
-
OPENAI_API_KEY = "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"
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
os.environ["OPENAI_API_KEY"] = OPENAI_API_KEY
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
-
def
|
25 |
-
|
26 |
-
|
27 |
-
messages=[
|
28 |
-
{
|
29 |
-
"role": "user",
|
30 |
-
"content": query,
|
31 |
-
}
|
32 |
-
],
|
33 |
-
model=model_name,
|
34 |
-
)
|
35 |
-
return chat_completion.choices[0].message.content
|
36 |
-
|
37 |
-
# Load and process PDF
|
38 |
-
def process_pdf(pdf_path):
|
39 |
-
"""Load and split the PDF into documents."""
|
40 |
-
loader = PyPDFLoader(pdf_path)
|
41 |
-
documents = loader.load_and_split()
|
42 |
return documents
|
43 |
|
44 |
-
# Create FAISS vector database
|
45 |
def create_vector_db(documents):
|
46 |
-
|
47 |
-
embeddings =
|
48 |
vector_db = FAISS.from_documents(documents, embeddings)
|
49 |
return vector_db
|
50 |
|
51 |
-
#
|
52 |
-
def
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
# Streamlit
|
58 |
def main():
|
59 |
-
st.title("
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
if uploaded_pdf:
|
65 |
-
with open("uploaded_act.pdf", "wb") as f:
|
66 |
-
f.write(uploaded_pdf.read())
|
67 |
-
documents = process_pdf("uploaded_act.pdf")
|
68 |
-
st.success("PDF Loaded and Processed Successfully!")
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
groq_client = initialize_groq_client()
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
if query:
|
81 |
-
with st.spinner("Fetching Answer..."):
|
82 |
-
# Use Groq API to generate answer
|
83 |
-
answer = generate_response(groq_client, query)
|
84 |
-
|
85 |
-
# Display Answer
|
86 |
-
st.write("### Answer:")
|
87 |
-
st.write(answer)
|
88 |
|
89 |
if __name__ == "__main__":
|
90 |
main()
|
|
|
|
|
1 |
import os
|
2 |
+
import streamlit as st
|
3 |
+
from groq import Groq
|
4 |
+
from langchain.embeddings import BaseEmbedding
|
5 |
from langchain.vectorstores import FAISS
|
|
|
6 |
from langchain.document_loaders import PyPDFLoader
|
7 |
+
from langchain.chains.question_answering import load_qa_chain
|
8 |
+
from langchain.llms import OpenAI # Keep this if you're still using OpenAI for QA model, otherwise replace it
|
9 |
+
from langchain.prompts import PromptTemplate
|
|
|
|
|
10 |
|
11 |
+
# Groq API setup for embeddings
|
12 |
+
class GroqEmbedding(BaseEmbedding):
|
13 |
+
def __init__(self, api_key: str):
|
14 |
+
self.client = Groq(api_key=api_key)
|
|
|
15 |
|
16 |
+
def embed_documents(self, texts: list) -> list:
|
17 |
+
embeddings = []
|
18 |
+
for text in texts:
|
19 |
+
response = self.client.embeddings.create(input=text)
|
20 |
+
embeddings.append(response['data'])
|
21 |
+
return embeddings
|
22 |
|
23 |
+
# Load documents from uploaded PDF file
|
24 |
+
def load_documents(uploaded_file):
|
25 |
+
loader = PyPDFLoader(uploaded_file)
|
26 |
+
documents = loader.load()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
return documents
|
28 |
|
29 |
+
# Create FAISS vector database with Groq embeddings
|
30 |
def create_vector_db(documents):
|
31 |
+
# Use Groq embeddings
|
32 |
+
embeddings = GroqEmbedding(api_key="your-groq-api-key") # Pass your Groq API key
|
33 |
vector_db = FAISS.from_documents(documents, embeddings)
|
34 |
return vector_db
|
35 |
|
36 |
+
# Function to perform QA with the uploaded documents
|
37 |
+
def perform_qa(vector_db, query):
|
38 |
+
# Set up the prompt and model for QA
|
39 |
+
prompt_template = "Answer the following question based on the documents: {question}"
|
40 |
+
prompt = PromptTemplate(input_variables=["question"], template=prompt_template)
|
41 |
+
qa_chain = load_qa_chain(OpenAI(), chain_type="stuff", prompt=prompt) # Keep OpenAI model for QA
|
42 |
+
|
43 |
+
# Query the vector DB to retrieve the most relevant documents
|
44 |
+
results = vector_db.similarity_search(query)
|
45 |
+
|
46 |
+
# Perform QA using the chain
|
47 |
+
answer = qa_chain.run(input_documents=results, question=query)
|
48 |
+
return answer
|
49 |
|
50 |
+
# Streamlit UI setup
|
51 |
def main():
|
52 |
+
st.title("Document Upload and Question Answering")
|
53 |
+
|
54 |
+
# Upload PDF file
|
55 |
+
uploaded_file = st.file_uploader("Choose a PDF file", type=["pdf"])
|
56 |
+
if uploaded_file:
|
57 |
+
st.write("File uploaded successfully!")
|
58 |
+
|
59 |
+
try:
|
60 |
+
# Load documents from the uploaded PDF
|
61 |
+
documents = load_documents(uploaded_file)
|
62 |
|
63 |
+
# Create a vector DB using Groq embeddings
|
64 |
+
vector_db = create_vector_db(documents)
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
+
# User query for Q&A
|
67 |
+
query = st.text_input("Ask a question based on the uploaded document:")
|
|
|
68 |
|
69 |
+
if query:
|
70 |
+
# Get the answer for the query
|
71 |
+
answer = perform_qa(vector_db, query)
|
72 |
+
st.write("Answer:", answer)
|
73 |
|
74 |
+
except Exception as e:
|
75 |
+
st.error(f"Error loading client or processing query: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
if __name__ == "__main__":
|
78 |
main()
|