File size: 1,930 Bytes
3a709b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain_community.document_loaders import PyPDFLoader
from langchain.schema import prompt
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.prompts import ChatPromptTemplate
from langchain_community.vectorstores import FAISS
from langchain.schema.runnable import RunnablePassthrough
from langchain_community.embeddings import HuggingFaceEmbeddings
from langchain_groq import ChatGroq
import gradio as gr

GROQ_API_KEY = "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"

loader = PyPDFLoader("Bhagavad-Gita.pdf")
docs = loader.load()

text_sp = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_sp.split_documents(docs)

# Extract text content from Document objects
texts = [doc.page_content for doc in splits]

embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2", model_kwargs={'device': "cpu"})

prompt_template = """You are an AI trained on Bhagvad Geeta, a sacred Hindu scripture. You provide readings from the text and offer wisdom and guidance based on its teachings.
Your responses should reflect the spiritual and philosophical nature of the Bhagvad Gita, offering deep insights into life's questions.
When asked a question, reference specific verses when appropriate and explain their relevance to the query.
Given below is the context and question of the user,
context = {context}
question = {question}
"""

prompt = ChatPromptTemplate.from_template(prompt_template)

vector_store = FAISS.from_texts(texts, embedding=embeddings)
retriever = vector_store.as_retriever()

llm = ChatGroq(model="llama3-8b-8192", 
               groq_api_key=GROQ_API_KEY)

rag_chain = {"context": retriever, "question": RunnablePassthrough()} | prompt | llm

def demo(name):
    return rag_chain.invoke(name).content

demo = gr.Interface(fn=demo, inputs="textbox", outputs="textbox", title="Fidem.AI")
demo.launch(share=True)