"""## WEBSITE CHAT BOT""" # Commented out IPython magic to ensure Python compatibility. # %%capture # !pip install langchain # !pip install bitsandbytes accelerate transformers # !pip install sentence_transformers # !pip install unstructured # !pip install faiss-cpu !huggingface-cli login # Commented out IPython magic to ensure Python compatibility. # %%capture # !pip install numpy==1.24.4 # Commented out IPython magic to ensure Python compatibility. # %%capture # pip install -U langchain-community from langchain.document_loaders import UnstructuredURLLoader URLs = [ "https://fullstackacademy.in/" ] loaders=UnstructuredURLLoader(urls=URLs) data=loaders.load() # creating the chunks from langchain.text_splitter import RecursiveCharacterTextSplitter text_chunks=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200) chunks=text_chunks.split_documents(data) len(chunks) # calling huggingfaceembeddings class from langchain.embeddings import HuggingFaceEmbeddings embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') # calling faiss vector database from langchain from langchain.vectorstores import FAISS vectordatabase=FAISS.from_documents(chunks,embeddings) from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,pipeline from langchain import HuggingFacePipeline model="google/flan-t5-large" tokenizer = AutoTokenizer.from_pretrained(model) model1 = AutoModelForSeq2SeqLM.from_pretrained(model) pipe = pipeline("text2text-generation", model=model1, tokenizer=tokenizer) llm = HuggingFacePipeline( pipeline = pipe, model_kwargs={"temperature": 0, "max_length": 500}, ) from langchain.prompts import PromptTemplate template = """use the context to provide a concise answer and if you don't know just say don't now. {context} Question: {question} Helpful Answer:""" QA_CHAIN_PROMPT = PromptTemplate.from_template(template) from langchain.chains import RetrievalQA qa_chain = RetrievalQA.from_chain_type( llm, retriever=vectordatabase.as_retriever(), chain_type="stuff",chain_type_kwargs={"prompt": QA_CHAIN_PROMPT} ) question = "Who is the co-founder?" result = qa_chain({"query": question}) result["result"] question = "What is the data science course duration?" result = qa_chain({"query": question}) result["result"] # Commented out IPython magic to ensure Python compatibility. # %%capture # pip install gradio import gradio as gr def fetch(question, history): result = qa_chain({"query": question}) return result["result"] gr.ChatInterface( fetch, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7), title="Yes Man", description="Ask Yes Man any question", theme="soft", examples=["contact details", "data science bootcamp fee?", "placements info","MERN fee"], cache_examples=True, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear", ).launch(share=True)