Update app.py
Browse files
app.py
CHANGED
@@ -1,61 +1,126 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
from langchain.prompts import PromptTemplate
|
5 |
-
from langchain.chains import RetrievalQA
|
6 |
-
from langchain.llms import HuggingFaceLLM # Adjusted for correct instantiation
|
7 |
-
import warnings
|
8 |
-
from huggingface_hub import login
|
9 |
import os
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
data = loader.load()
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
# Define the prompt template
|
32 |
-
prompt_template = """Given the following context and a question, generate an answer based on the context only.
|
33 |
In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
34 |
If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
|
35 |
If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at [email protected]" Don't try to make up an answer.
|
|
|
36 |
CONTEXT: {context}
|
37 |
-
QUESTION: {question}"""
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
chain_type_kwargs={"prompt": PROMPT})
|
50 |
|
51 |
# Define the chat response function
|
52 |
def chatresponse(message, history):
|
53 |
-
output =
|
54 |
-
|
|
|
55 |
|
56 |
# Launch the Gradio chat interface
|
57 |
gr.ChatInterface(chatresponse).launch()
|
58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
|
60 |
# import gradio as gr
|
61 |
# # from langchain.llms import GooglePalm
|
|
|
1 |
import gradio as gr
|
2 |
+
|
3 |
+
|
|
|
|
|
|
|
|
|
|
|
4 |
import os
|
5 |
+
os.environ["hftoken"] = hftoken
|
6 |
+
|
7 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
8 |
+
|
9 |
+
repo_id = "mistralai/Mistral-7B-Instruct-v0.3"
|
10 |
+
llm = HuggingFaceEndpoint(repo_id = repo_id, max_new_tokens = 128, temperature = 0.7, huggingfacehub_api_token = hftoken)
|
11 |
|
12 |
+
from langchain_core.output_parsers import StrOutputParser
|
13 |
+
from langchain_core.prompts import ChatPromptTemplate
|
14 |
|
15 |
+
prompt = ChatPromptTemplate.from_template("tell me a joke about {topic}")
|
16 |
+
chain = prompt | llm | StrOutputParser()
|
17 |
+
|
18 |
+
from langchain.document_loaders.csv_loader import CSVLoader
|
19 |
+
|
20 |
+
loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
|
21 |
data = loader.load()
|
22 |
|
23 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
24 |
+
from langchain_chroma import Chroma
|
25 |
+
from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
|
26 |
|
27 |
+
# CHECK MTEB LEADERBOARD & FIND BEST EMBEDDING MODEL
|
28 |
+
model = "BAAI/bge-m3"
|
29 |
+
embeddings = HuggingFaceEndpointEmbeddings(model = model)
|
30 |
|
31 |
+
vectorstore = Chroma.from_documents(documents = data, embedding = embeddings)
|
32 |
+
retriever = vectorstore.as_retriever()
|
33 |
+
|
34 |
+
# from langchain.prompts import PromptTemplate
|
35 |
+
|
36 |
+
from langchain_core.prompts import ChatPromptTemplate
|
37 |
+
|
38 |
+
prompt = ChatPromptTemplate.from_template("""Given the following context and a question, generate an answer based on the context only.
|
39 |
|
|
|
|
|
40 |
In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
41 |
If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
|
42 |
If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at [email protected]" Don't try to make up an answer.
|
43 |
+
|
44 |
CONTEXT: {context}
|
|
|
45 |
|
46 |
+
QUESTION: {question}""")
|
47 |
+
|
48 |
+
from langchain_core.runnables import RunnablePassthrough
|
49 |
|
50 |
+
rag_chain = (
|
51 |
+
{"context": retriever, "question": RunnablePassthrough()}
|
52 |
+
| prompt
|
53 |
+
| llm
|
54 |
+
| StrOutputParser()
|
55 |
+
)
|
|
|
56 |
|
57 |
# Define the chat response function
|
58 |
def chatresponse(message, history):
|
59 |
+
output = rag_chain.invoke(message)
|
60 |
+
response = output.split('ANSWER: ')[-1].strip()
|
61 |
+
print(response)
|
62 |
|
63 |
# Launch the Gradio chat interface
|
64 |
gr.ChatInterface(chatresponse).launch()
|
65 |
|
66 |
+
# import gradio as gr
|
67 |
+
# from langchain_community.document_loaders import CSVLoader # Changed import
|
68 |
+
# from langchain_community.vectorstores import FAISS # Changed import
|
69 |
+
# from langchain.prompts import PromptTemplate
|
70 |
+
# from langchain.chains import RetrievalQA
|
71 |
+
# from langchain.llms import HuggingFaceLLM # Adjusted for correct instantiation
|
72 |
+
# import warnings
|
73 |
+
# from huggingface_hub import login
|
74 |
+
# import os
|
75 |
+
# from transformers import pipeline
|
76 |
+
|
77 |
+
# # Initialize the LLM using pipeline
|
78 |
+
# llm = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct") # Adjusted initialization
|
79 |
+
|
80 |
+
# # Load CSV file
|
81 |
+
# loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column='prompt')
|
82 |
+
# data = loader.load()
|
83 |
+
|
84 |
+
# # Suppress warnings
|
85 |
+
# warnings.filterwarnings("ignore", category=UserWarning, message="TypedStorage is deprecated")
|
86 |
+
# warnings.filterwarnings("ignore", category=FutureWarning, message="`resume_download` is deprecated")
|
87 |
+
|
88 |
+
# # Embedding model
|
89 |
+
# model_name = "BAAI/bge-m3"
|
90 |
+
# instructor_embeddings = HuggingFaceLLM(model_name=model_name) # Adjusted for correct instantiation
|
91 |
+
|
92 |
+
# # Create FAISS vector store from documents
|
93 |
+
# vectordb = FAISS.from_documents(documents=data, embedding=instructor_embeddings)
|
94 |
+
# retriever = vectordb.as_retriever()
|
95 |
+
|
96 |
+
# # Define the prompt template
|
97 |
+
# prompt_template = """Given the following context and a question, generate an answer based on the context only.
|
98 |
+
# In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
|
99 |
+
# If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
|
100 |
+
# If the answer is not found in the context, kindly state "I don't know. Please ask Rishi on Discord. Discord Invite Link: https://discord.gg/6ezpZGeCcM. Or email at [email protected]" Don't try to make up an answer.
|
101 |
+
# CONTEXT: {context}
|
102 |
+
# QUESTION: {question}"""
|
103 |
+
|
104 |
+
# PROMPT = PromptTemplate(
|
105 |
+
# template=prompt_template, input_variables=["context", "question"]
|
106 |
+
# )
|
107 |
+
|
108 |
+
# # Initialize the RetrievalQA chain
|
109 |
+
# chain = RetrievalQA.from_chain_type(llm=llm, # Adjusted initialization
|
110 |
+
# chain_type="stuff",
|
111 |
+
# retriever=retriever,
|
112 |
+
# input_key="query",
|
113 |
+
# return_source_documents=True,
|
114 |
+
# chain_type_kwargs={"prompt": PROMPT})
|
115 |
+
|
116 |
+
# # Define the chat response function
|
117 |
+
# def chatresponse(message, history):
|
118 |
+
# output = chain(message)
|
119 |
+
# return output['result']
|
120 |
+
|
121 |
+
# # Launch the Gradio chat interface
|
122 |
+
# gr.ChatInterface(chatresponse).launch()
|
123 |
+
|
124 |
|
125 |
# import gradio as gr
|
126 |
# # from langchain.llms import GooglePalm
|