|
|
import gradio as gr |
|
|
from langchain_community.document_loaders import PyPDFLoader |
|
|
from langchain_community.vectorstores import Chroma |
|
|
from langchain_community.embeddings import HuggingFaceEmbeddings |
|
|
from langchain.text_splitter import RecursiveCharacterTextSplitter |
|
|
from langchain.chains import create_retrieval_chain |
|
|
from langchain.chains.combine_documents import create_stuff_documents_chain |
|
|
from langchain_groq import ChatGroq |
|
|
from langchain.prompts import PromptTemplate |
|
|
import os |
|
|
GROQ_API = os.getenv("GROQ_API") |
|
|
|
|
|
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2") |
|
|
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) |
|
|
|
|
|
|
|
|
llm = ChatGroq( |
|
|
model="mixtral-8x7b-32768", |
|
|
temperature=1.25, |
|
|
max_tokens=512, |
|
|
timeout=None, |
|
|
api_key=GROQ_API |
|
|
) |
|
|
|
|
|
|
|
|
custom_prompt = PromptTemplate.from_template(""" |
|
|
You are a helpful AI assistant. |
|
|
Answer the question using only the following context: |
|
|
|
|
|
{context} |
|
|
|
|
|
Question: {input} |
|
|
|
|
|
Provide a detailed and accurate response. |
|
|
""") |
|
|
|
|
|
|
|
|
document_chain = create_stuff_documents_chain( |
|
|
llm=llm, |
|
|
prompt=custom_prompt, |
|
|
) |
|
|
|
|
|
|
|
|
def process_pdf(file): |
|
|
|
|
|
loader = PyPDFLoader(file.name) |
|
|
pages = loader.load_and_split(text_splitter) |
|
|
|
|
|
|
|
|
vector_store = Chroma.from_documents(pages, embeddings) |
|
|
|
|
|
|
|
|
retriever = vector_store.as_retriever(search_type="similarity", search_kwargs={"k": 5}) |
|
|
|
|
|
|
|
|
qa_chain = create_retrieval_chain( |
|
|
retriever=retriever, |
|
|
combine_docs_chain=document_chain |
|
|
) |
|
|
|
|
|
return qa_chain |
|
|
|
|
|
|
|
|
def get_answer(file, user_prompt): |
|
|
|
|
|
qa_chain = process_pdf(file) |
|
|
|
|
|
|
|
|
response = qa_chain.invoke({"input": user_prompt}) |
|
|
return response["answer"] |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# PDF Q&A with ChatGroq and LangChain") |
|
|
|
|
|
with gr.Row(): |
|
|
pdf_input = gr.File(label="Upload PDF", type="filepath") |
|
|
question_input = gr.Textbox(label="Ask a question", placeholder="Type your question here...") |
|
|
|
|
|
output = gr.Textbox(label="Answer", interactive=False) |
|
|
|
|
|
submit_button = gr.Button("Submit") |
|
|
submit_button.click(fn=get_answer, inputs=[pdf_input, question_input], outputs=output) |
|
|
|
|
|
|
|
|
demo.launch() |