Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from PyPDF2 import PdfReader
|
4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
5 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
6 |
+
from langchain.vectorstores import FAISS
|
7 |
+
from groq import Groq
|
8 |
+
|
9 |
+
# Set up Groq client
|
10 |
+
client = Groq(
|
11 |
+
api_key="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"),
|
12 |
+
)
|
13 |
+
|
14 |
+
# Streamlit app
|
15 |
+
st.title("RAG-based PDF QA Application")
|
16 |
+
|
17 |
+
# Step 1: Upload PDF document
|
18 |
+
uploaded_file = st.file_uploader("Upload a PDF document", type="pdf")
|
19 |
+
|
20 |
+
if uploaded_file:
|
21 |
+
# Step 2: Extract text from PDF
|
22 |
+
pdf_reader = PdfReader(uploaded_file)
|
23 |
+
text = "\n".join(page.extract_text() for page in pdf_reader.pages if page.extract_text())
|
24 |
+
|
25 |
+
# Step 3: Split text into chunks
|
26 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
27 |
+
chunk_size=1000, chunk_overlap=200
|
28 |
+
)
|
29 |
+
chunks = text_splitter.split_text(text)
|
30 |
+
|
31 |
+
# Step 4: Generate embeddings
|
32 |
+
st.text("Generating embeddings...")
|
33 |
+
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
|
34 |
+
vector_db = FAISS.from_texts(chunks, embeddings)
|
35 |
+
|
36 |
+
st.success("Embeddings generated and stored in vector database.")
|
37 |
+
|
38 |
+
# Step 5: User interaction
|
39 |
+
query = st.text_input("Ask a question based on the uploaded document:")
|
40 |
+
if query:
|
41 |
+
# Retrieve relevant chunks from vector DB
|
42 |
+
docs = vector_db.similarity_search(query, k=3)
|
43 |
+
context = "\n".join(doc.page_content for doc in docs)
|
44 |
+
|
45 |
+
# Use Groq API for response generation
|
46 |
+
chat_completion = client.chat.completions.create(
|
47 |
+
messages=[
|
48 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
49 |
+
{"role": "user", "content": query},
|
50 |
+
{"role": "assistant", "content": context},
|
51 |
+
],
|
52 |
+
model="llama3-8b-8192",
|
53 |
+
stream=False,
|
54 |
+
)
|
55 |
+
|
56 |
+
answer = chat_completion.choices[0].message.content
|
57 |
+
st.text_area("Answer:", value=answer, height=200)
|
58 |
+
|
59 |
+
# Footer
|
60 |
+
st.caption("Powered by Open Source Models and Groq API.")
|