File size: 2,369 Bytes
1f289e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_huggingface import HuggingFaceEmbeddings
from langchain.chains import RetrievalQA
from langchain.prompts import PromptTemplate
from langchain_groq import ChatGroq
import gradio as gr

# Load Groq API key from env variables
groq_api_key = os.getenv("GROQ_API_KEY")

def load_and_index_pdf(pdf_path="company_data.pdf"):
    loader = PyPDFLoader(pdf_path)
    documents = loader.load()
    splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    texts = splitter.split_documents(documents)
    embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    db = FAISS.from_documents(texts, embedding)
    db.save_local("company_faiss_index")

def setup_qa():
    embedding = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
    if not os.path.exists("company_faiss_index"):
        load_and_index_pdf()
    db = FAISS.load_local("company_faiss_index", embedding, allow_dangerous_deserialization=True)
    retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})

    llm = ChatGroq(model_name="llama3-70b-8192", api_key=groq_api_key)

    prompt = PromptTemplate.from_template("""

You are a helpful assistant for a digital marketing company.

Try to answer the user's question based on the provided context from the company document.

If the answer is not found in the context, provide a helpful and accurate answer from your own knowledge, focusing on digital marketing topics.



Context:

{context}



Question:

{question}

""")

    qa_chain = RetrievalQA.from_chain_type(
        llm=llm,
        retriever=retriever,
        return_source_documents=False,
        chain_type_kwargs={"prompt": prompt}
    )
    return qa_chain

qa_chain = setup_qa()

def answer_question(query):
    result = qa_chain.invoke(query)
    return result['result']

# Minimal Gradio UI
iface = gr.Interface(
    fn=answer_question,
    inputs=gr.Textbox(lines=2, placeholder="Ask a question about digital marketing..."),
    outputs="text",
    title="CLick Media Lab Chatbot"
)

if __name__ == "__main__":
    iface.launch()