Upload app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,63 @@
|
|
1 |
-
"""## WEBSITE CHAT BOT"""
|
2 |
-
|
3 |
-
%%capture
|
4 |
-
!pip install langchain
|
5 |
-
!pip install bitsandbytes accelerate transformers
|
6 |
-
!pip install sentence_transformers
|
7 |
-
!pip install unstructured
|
8 |
-
!pip install faiss-cpu
|
9 |
-
|
10 |
-
!huggingface-cli login
|
11 |
-
|
12 |
-
%%capture
|
13 |
-
!pip install numpy==1.24.4
|
14 |
-
|
15 |
-
%%capture
|
16 |
-
pip install -U langchain-community
|
17 |
-
|
18 |
-
from langchain.document_loaders import UnstructuredURLLoader
|
19 |
-
|
20 |
-
URLs = [
|
21 |
-
"https://fullstackacademy.in/"
|
22 |
-
]
|
23 |
-
|
24 |
-
loaders=UnstructuredURLLoader(urls=URLs)
|
25 |
-
|
26 |
-
data=loaders.load()
|
27 |
-
|
28 |
-
# creating the chunks
|
29 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
30 |
-
|
31 |
-
text_chunks=RecursiveCharacterTextSplitter(chunk_size=1000,chunk_overlap=200)
|
32 |
-
|
33 |
-
chunks=text_chunks.split_documents(data)
|
34 |
-
|
35 |
-
len(chunks)
|
36 |
-
|
37 |
-
# calling huggingfaceembeddings class
|
38 |
-
from langchain.embeddings import HuggingFaceEmbeddings
|
39 |
-
|
40 |
-
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2')
|
41 |
-
|
42 |
-
# calling faiss vector database from langchain
|
43 |
-
from langchain.vectorstores import FAISS
|
44 |
-
|
45 |
-
vectordatabase=FAISS.from_documents(chunks,embeddings)
|
46 |
-
|
47 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM,pipeline
|
48 |
-
from langchain import HuggingFacePipeline
|
49 |
-
|
50 |
-
model="google/flan-t5-large"
|
51 |
-
|
52 |
-
tokenizer = AutoTokenizer.from_pretrained(model)
|
53 |
-
model1 = AutoModelForSeq2SeqLM.from_pretrained(model)
|
54 |
-
pipe = pipeline("text2text-generation", model=model1, tokenizer=tokenizer)
|
55 |
-
llm = HuggingFacePipeline(
|
56 |
-
pipeline = pipe,
|
57 |
-
model_kwargs={"temperature": 0, "max_length": 500},
|
58 |
-
)
|
59 |
-
|
60 |
-
from langchain.prompts import PromptTemplate
|
61 |
-
|
62 |
-
template = """use the context to provide a concise answer and if you don't know just say don't now.
|
63 |
-
{context}
|
64 |
-
Question: {question}
|
65 |
-
Helpful Answer:"""
|
66 |
-
QA_CHAIN_PROMPT = PromptTemplate.from_template(template)
|
67 |
-
|
68 |
-
from langchain.chains import RetrievalQA
|
69 |
-
|
70 |
-
qa_chain = RetrievalQA.from_chain_type(
|
71 |
-
llm, retriever=vectordatabase.as_retriever(), chain_type="stuff",chain_type_kwargs={"prompt": QA_CHAIN_PROMPT}
|
72 |
-
)
|
73 |
-
|
74 |
-
question = "Who is the co-founder?"
|
75 |
-
result = qa_chain({"query": question})
|
76 |
-
result["result"]
|
77 |
-
|
78 |
-
question = "What is the data science course duration?"
|
79 |
-
result = qa_chain({"query": question})
|
80 |
-
result["result"]
|
81 |
-
|
82 |
-
%%capture
|
83 |
-
pip install gradio
|
84 |
-
|
85 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
def fetch(question, history):
|
88 |
-
result = qa_chain({"query": question})
|
89 |
-
return result["result"]
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
chatbot=gr.Chatbot(height=300),
|
94 |
-
textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),
|
95 |
-
title="Yes Man",
|
96 |
-
description="Ask Yes Man any question",
|
97 |
-
theme="soft",
|
98 |
-
examples=["contact details", "data science bootcamp fee?", "placements info","MERN fee"],
|
99 |
-
cache_examples=True,
|
100 |
-
retry_btn=None,
|
101 |
-
undo_btn="Delete Previous",
|
102 |
-
clear_btn="Clear",
|
103 |
-
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
|
4 |
+
"""
|
5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
+
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
+
|
9 |
+
|
10 |
+
def respond(
|
11 |
+
message,
|
12 |
+
history: list[tuple[str, str]],
|
13 |
+
system_message,
|
14 |
+
max_tokens,
|
15 |
+
temperature,
|
16 |
+
top_p,
|
17 |
+
):
|
18 |
+
messages = [{"role": "system", "content": system_message}]
|
19 |
+
|
20 |
+
for val in history:
|
21 |
+
if val[0]:
|
22 |
+
messages.append({"role": "user", "content": val[0]})
|
23 |
+
if val[1]:
|
24 |
+
messages.append({"role": "assistant", "content": val[1]})
|
25 |
+
|
26 |
+
messages.append({"role": "user", "content": message})
|
27 |
+
|
28 |
+
response = ""
|
29 |
+
|
30 |
+
for message in client.chat_completion(
|
31 |
+
messages,
|
32 |
+
max_tokens=max_tokens,
|
33 |
+
stream=True,
|
34 |
+
temperature=temperature,
|
35 |
+
top_p=top_p,
|
36 |
+
):
|
37 |
+
token = message.choices[0].delta.content
|
38 |
+
|
39 |
+
response += token
|
40 |
+
yield response
|
41 |
+
|
42 |
+
"""
|
43 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
44 |
+
"""
|
45 |
+
demo = gr.ChatInterface(
|
46 |
+
respond,
|
47 |
+
additional_inputs=[
|
48 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
49 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
50 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
51 |
+
gr.Slider(
|
52 |
+
minimum=0.1,
|
53 |
+
maximum=1.0,
|
54 |
+
value=0.95,
|
55 |
+
step=0.05,
|
56 |
+
label="Top-p (nucleus sampling)",
|
57 |
+
),
|
58 |
+
],
|
59 |
+
)
|
60 |
|
|
|
|
|
|
|
61 |
|
62 |
+
if __name__ == "__main__":
|
63 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|