RAG_bot / app.py
varun1128's picture
Update app.py
5a9124c verified
Raw History Blame Contribute Delete
3.67 kB
import gradio as gr
import os
import fitz # PyMuPDF
import tempfile
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import TextLoader
from langchain.chains import RetrievalQA
from langchain.llms.base import LLM
from typing import List, Optional
from groq import Groq
# --------------------- Custom LLM Class ---------------------
class GroqLLM(LLM):
model: str = "llama3-8b-8192"
api_key: str = os.getenv("GROQ_API_KEY") # Get securely from environment
temperature: float = 0.0
def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str:
client = Groq(api_key=self.api_key)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
response = client.chat.completions.create(
model=self.model,
messages=messages,
temperature=self.temperature,
)
return response.choices[0].message.content
@property
def _llm_type(self) -> str:
return "groq-llm"
# --------------------- Global State ---------------------
qa_chain = None
summary_text = ""
# --------------------- PDF Processor ---------------------
def process_pdf(file):
global qa_chain, summary_text
# Save uploaded file
temp_dir = tempfile.mkdtemp()
pdf_path = os.path.join(temp_dir, "uploaded.pdf")
with open(pdf_path, "wb") as f:
f.write(file.read())
# Extract text using PyMuPDF
doc = fitz.open(pdf_path)
text = "\n".join([page.get_text() for page in doc])
# Save as .txt for loader
txt_path = os.path.join(temp_dir, "temp.txt")
with open(txt_path, "w", encoding="utf-8") as f:
f.write(text)
# LangChain pipeline
loader = TextLoader(txt_path)
documents = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
docs = text_splitter.split_documents(documents)
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
vectorstore = Chroma.from_documents(docs, embeddings, persist_directory=os.path.join(temp_dir, "chroma_db"))
retriever = vectorstore.as_retriever()
groq_llm = GroqLLM()
qa_chain = RetrievalQA.from_chain_type(
llm=groq_llm,
retriever=retriever,
return_source_documents=True
)
# Generate auto-summary
summary_prompt = "Summarize what this document is about in 5-6 lines."
summary_text = qa_chain({"query": summary_prompt})["result"]
return summary_text
# --------------------- Question Answering ---------------------
def answer_question(query):
global qa_chain
if not qa_chain:
return "Please upload and process a PDF first."
result = qa_chain({"query": query})
return result["result"]
# --------------------- Gradio UI ---------------------
with gr.Blocks() as demo:
gr.Markdown("## 📄 PDF Summarizer & Q&A using Groq LLM (LLaMA3-8B)")
with gr.Row():
pdf_input = gr.File(label="Upload a PDF", type="binary")
summary_output = gr.Textbox(label="Auto Summary", lines=8)
with gr.Row():
process_button = gr.Button("Process PDF")
with gr.Row():
question = gr.Textbox(label="Ask a question about the PDF")
answer = gr.Textbox(label="Answer", lines=6)
process_button.click(fn=process_pdf, inputs=pdf_input, outputs=summary_output)
question.submit(fn=answer_question, inputs=question, outputs=answer)
demo.queue().launch()