rishisim commited on
Commit
0ddb8ec
1 Parent(s): b46fc84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +107 -48
app.py CHANGED
@@ -1,89 +1,148 @@
1
  import gradio as gr
2
- # from langchain.llms import GooglePalm
3
- from langchain_google_genai import GoogleGenerativeAI
4
- from langchain.document_loaders.csv_loader import CSVLoader
5
- from langchain_huggingface import HuggingFaceEmbeddings
6
- from langchain.vectorstores import FAISS
7
  from langchain.prompts import PromptTemplate
8
  from langchain.chains import RetrievalQA
 
9
  import warnings
10
  from huggingface_hub import login
11
  import os
12
-
13
-
14
  from transformers import pipeline
15
- llm = pipeline("feature-extraction", model="mixedbread-ai/mxbai-embed-large-v1")
16
-
17
- # from transformers import AutoModel
18
- # llm = AutoModel.from_pretrained("Alibaba-NLP/gte-large-en-v1.5", trust_remote_code=True)
19
-
20
- # LLAMA
21
- # from transformers import AutoModelForCausalLM, AutoTokenizer
22
- # from transformers import pipeline
23
-
24
- # hf_token = os.environ['llama_token']
25
-
26
- # login(token=hf_token)
27
-
28
- # llm = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct")
29
- # llm = pipeline("text-generation", model = "meta-llama/Meta-Llama-3-70B-Instruct")
30
-
31
- # MISTRAL
32
- # llm = pipeline("text-generation", model="mistralai/Mixtral-8x22B-Instruct-v0.1")
33
 
 
 
34
 
35
- # TO USE GOOGLE MODELS
36
- # api_key = "AIzaSyCdM_aAIsW_nPbjarOF83mbX1_z1cVX2_M"
37
-
38
- # llm = GoogleGenerativeAI(model="models/text-bison-001", google_api_key=api_key)
39
- # llm = GooglePalm(google_api_key = api_key, temperature=0.7)
40
-
41
- # LOADING CSV FILE
42
- loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
43
  data = loader.load()
44
 
45
- # SUPPRESSING WARNINGS
46
  warnings.filterwarnings("ignore", category=UserWarning, message="TypedStorage is deprecated")
47
  warnings.filterwarnings("ignore", category=FutureWarning, message="`resume_download` is deprecated")
48
 
49
-
50
- # EMBEDDING MODEL
51
  model_name = "BAAI/bge-m3"
52
- instructor_embeddings = HuggingFaceEmbeddings(model_name=model_name)
53
 
54
  # Create FAISS vector store from documents
55
  vectordb = FAISS.from_documents(documents=data, embedding=instructor_embeddings)
56
  retriever = vectordb.as_retriever()
57
 
 
58
  prompt_template = """Given the following context and a question, generate an answer based on the context only.
59
-
60
  In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
61
  If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
62
  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.
63
-
64
  CONTEXT: {context}
65
-
66
  QUESTION: {question}"""
67
 
68
  PROMPT = PromptTemplate(
69
- template = prompt_template, input_variables = ["context", "question"]
70
  )
71
 
 
 
 
 
 
 
 
72
 
73
- chain = RetrievalQA.from_chain_type(llm = llm,
74
- chain_type="stuff",
75
- retriever=retriever,
76
- input_key="query",
77
- return_source_documents=True,
78
- chain_type_kwargs = {"prompt": PROMPT})
79
-
80
  def chatresponse(message, history):
81
  output = chain(message)
82
  return output['result']
83
 
 
84
  gr.ChatInterface(chatresponse).launch()
85
 
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  # import gradio as gr
89
 
 
1
  import gradio as gr
2
+ from langchain_community.document_loaders import CSVLoader # Changed import
3
+ from langchain_community.vectorstores import FAISS # Changed import
 
 
 
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
  from transformers import pipeline
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ # Initialize the LLM using pipeline
13
+ llm = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct") # Adjusted initialization
14
 
15
+ # Load CSV file
16
+ loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column='prompt')
 
 
 
 
 
 
17
  data = loader.load()
18
 
19
+ # Suppress warnings
20
  warnings.filterwarnings("ignore", category=UserWarning, message="TypedStorage is deprecated")
21
  warnings.filterwarnings("ignore", category=FutureWarning, message="`resume_download` is deprecated")
22
 
23
+ # Embedding model
 
24
  model_name = "BAAI/bge-m3"
25
+ instructor_embeddings = HuggingFaceLLM(model_name=model_name) # Adjusted for correct instantiation
26
 
27
  # Create FAISS vector store from documents
28
  vectordb = FAISS.from_documents(documents=data, embedding=instructor_embeddings)
29
  retriever = vectordb.as_retriever()
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
  PROMPT = PromptTemplate(
40
+ template=prompt_template, input_variables=["context", "question"]
41
  )
42
 
43
+ # Initialize the RetrievalQA chain
44
+ chain = RetrievalQA.from_chain_type(llm=llm, # Adjusted initialization
45
+ chain_type="stuff",
46
+ retriever=retriever,
47
+ input_key="query",
48
+ return_source_documents=True,
49
+ chain_type_kwargs={"prompt": PROMPT})
50
 
51
+ # Define the chat response function
 
 
 
 
 
 
52
  def chatresponse(message, history):
53
  output = chain(message)
54
  return output['result']
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
62
+ # from langchain_google_genai import GoogleGenerativeAI
63
+ # from langchain.document_loaders.csv_loader import CSVLoader
64
+ # from langchain_huggingface import HuggingFaceEmbeddings
65
+ # from langchain.vectorstores import FAISS
66
+ # from langchain.prompts import PromptTemplate
67
+ # from langchain.chains import RetrievalQA
68
+ # import warnings
69
+ # from huggingface_hub import login
70
+ # import os
71
+
72
+
73
+ # from transformers import pipeline
74
+ # llm = pipeline("feature-extraction", model="mixedbread-ai/mxbai-embed-large-v1")
75
+
76
+ # # from transformers import AutoModel
77
+ # # llm = AutoModel.from_pretrained("Alibaba-NLP/gte-large-en-v1.5", trust_remote_code=True)
78
+
79
+ # # LLAMA
80
+ # # from transformers import AutoModelForCausalLM, AutoTokenizer
81
+ # # from transformers import pipeline
82
+
83
+ # # hf_token = os.environ['llama_token']
84
+
85
+ # # login(token=hf_token)
86
+
87
+ # # llm = pipeline("text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct")
88
+ # # llm = pipeline("text-generation", model = "meta-llama/Meta-Llama-3-70B-Instruct")
89
+
90
+ # # MISTRAL
91
+ # # llm = pipeline("text-generation", model="mistralai/Mixtral-8x22B-Instruct-v0.1")
92
+
93
+
94
+ # # TO USE GOOGLE MODELS
95
+ # # api_key = "AIzaSyCdM_aAIsW_nPbjarOF83mbX1_z1cVX2_M"
96
+
97
+ # # llm = GoogleGenerativeAI(model="models/text-bison-001", google_api_key=api_key)
98
+ # # llm = GooglePalm(google_api_key = api_key, temperature=0.7)
99
+
100
+ # # LOADING CSV FILE
101
+ # loader = CSVLoader(file_path='aiotsmartlabs_faq.csv', source_column = 'prompt')
102
+ # data = loader.load()
103
+
104
+ # # SUPPRESSING WARNINGS
105
+ # warnings.filterwarnings("ignore", category=UserWarning, message="TypedStorage is deprecated")
106
+ # warnings.filterwarnings("ignore", category=FutureWarning, message="`resume_download` is deprecated")
107
+
108
+
109
+ # # EMBEDDING MODEL
110
+ # model_name = "BAAI/bge-m3"
111
+ # instructor_embeddings = HuggingFaceEmbeddings(model_name=model_name)
112
+
113
+ # # Create FAISS vector store from documents
114
+ # vectordb = FAISS.from_documents(documents=data, embedding=instructor_embeddings)
115
+ # retriever = vectordb.as_retriever()
116
+
117
+ # prompt_template = """Given the following context and a question, generate an answer based on the context only.
118
+
119
+ # In the answer try to provide as much text as possible from "response" section in the source document context without making much changes.
120
+ # If somebody asks "Who are you?" or a similar phrase, state "I am Rishi's assistant built using a Large Language Model!"
121
+ # 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.
122
+
123
+ # CONTEXT: {context}
124
+
125
+ # QUESTION: {question}"""
126
+
127
+ # PROMPT = PromptTemplate(
128
+ # template = prompt_template, input_variables = ["context", "question"]
129
+ # )
130
+
131
+
132
+ # chain = RetrievalQA.from_chain_type(llm = llm,
133
+ # chain_type="stuff",
134
+ # retriever=retriever,
135
+ # input_key="query",
136
+ # return_source_documents=True,
137
+ # chain_type_kwargs = {"prompt": PROMPT})
138
+
139
+ # def chatresponse(message, history):
140
+ # output = chain(message)
141
+ # return output['result']
142
+
143
+ # gr.ChatInterface(chatresponse).launch()
144
+
145
+
146
 
147
  # import gradio as gr
148