File size: 2,607 Bytes
f7f8e9a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | import gradio as gr
import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
from groq import Groq
# Read API key
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
client = Groq(api_key=GROQ_API_KEY)
# Global vector database
vectorstore = None
def process_pdf(pdf_file):
global vectorstore
if pdf_file is None:
return "Please upload a PDF."
# Load PDF
loader = PyPDFLoader(pdf_file.name)
documents = loader.load()
# Split text
splitter = RecursiveCharacterTextSplitter(
chunk_size=1000,
chunk_overlap=200
)
docs = splitter.split_documents(documents)
# Embeddings
embeddings = HuggingFaceEmbeddings(
model_name="sentence-transformers/all-MiniLM-L6-v2"
)
# Create vector DB
vectorstore = FAISS.from_documents(
docs,
embeddings
)
return "PDF processed successfully."
def ask_question(question):
global vectorstore
if vectorstore is None:
return "Please process a PDF first."
# Retrieve relevant chunks
docs = vectorstore.similarity_search(
question,
k=3
)
context = "\n\n".join(
[doc.page_content for doc in docs]
)
prompt = f"""
Answer the question only using the context below.
Context:
{context}
Question:
{question}
Answer:
"""
# Groq response
response = client.chat.completions.create(
model="llama-3.1-8b-instant",
messages=[
{
"role": "user",
"content": prompt
}
],
temperature=0.2,
max_tokens=400
)
return response.choices[0].message.content
with gr.Blocks() as demo:
gr.Markdown("# PDF Q&A Chatbot")
pdf_input = gr.File(
label="Upload PDF",
file_types=[".pdf"]
)
process_button = gr.Button("Process PDF")
status_output = gr.Textbox(
label="Status"
)
process_button.click(
fn=process_pdf,
inputs=pdf_input,
outputs=status_output
)
question_input = gr.Textbox(
label="Ask Question",
placeholder="Ask anything from the PDF..."
)
ask_button = gr.Button("Get Answer")
answer_output = gr.Textbox(
lines=10,
label="Answer"
)
ask_button.click(
fn=ask_question,
inputs=question_input,
outputs=answer_output
)
demo.launch() |