Spaces:
Runtime error
Runtime error
added comments
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
from pathlib import Path
|
3 |
|
@@ -16,9 +17,11 @@ from langchain.vectorstores import Chroma
|
|
16 |
import gradio as gr
|
17 |
import time
|
18 |
from transformers import AutoTokenizer, GenerationConfig, TextStreamer, pipeline
|
|
|
19 |
questions_dir=Path("Microsoft_QA")
|
20 |
questions_dir.mkdir(exist_ok=True, parents=True)
|
21 |
|
|
|
22 |
def write_file(question, answer, file_path):
|
23 |
text = f"""
|
24 |
Q: {question}
|
@@ -237,22 +240,26 @@ write_file(
|
|
237 |
answer="""Microsoft Q&A doesn't move or store customer data out of the region it's deployed in.""".strip(),
|
238 |
file_path="question_40.txt",
|
239 |
)
|
|
|
240 |
model_name = "TheBloke/Nous-Hermes-13B-GPTQ"
|
241 |
model_basename = "nous-hermes-13b-GPTQ-4bit-128g.no-act.order"
|
|
|
242 |
|
243 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast= True)
|
244 |
-
|
245 |
model = AutoGPTQForCausalLM.from_quantized(
|
246 |
model_name,
|
247 |
model_basename= model_basename,
|
248 |
use_safetensors=True,
|
249 |
Trust_remote_code=True,
|
250 |
)
|
251 |
-
|
252 |
generation_config = GenerationConfig.from_pretrained(model_name)
|
|
|
253 |
streamer = TextStreamer(
|
254 |
tokenizer, skip_prompt = True, skip_special_tokens=True, use_multiprocessing = False
|
255 |
)
|
|
|
256 |
pipe = pipeline(
|
257 |
"text-generation",
|
258 |
model=model,
|
@@ -266,17 +273,23 @@ pipe = pipeline(
|
|
266 |
batch_size=1,
|
267 |
|
268 |
)
|
|
|
269 |
llm=HuggingFacePipeline(pipeline=pipe)
|
|
|
270 |
embeddings = HuggingFaceEmbeddings(
|
271 |
model_name= 'embaas/sentence-transformers-multilingual-e5-base'
|
272 |
|
273 |
)
|
|
|
274 |
loader = DirectoryLoader("./Microsoft_QA/", glob="**/*txt")
|
275 |
documents = loader.load()
|
276 |
-
|
|
|
277 |
text_splitter = CharacterTextSplitter(chunk_size=512, chunk_overlap=0)
|
278 |
texts = text_splitter.split_documents(documents)
|
|
|
279 |
db = Chroma.from_documents(texts, embeddings)
|
|
|
280 |
template = """
|
281 |
### Instruction: You're a microsoft QA platform support agent who is talking to user giving them information about the platform. Use only the chat history and the following information
|
282 |
{context}
|
@@ -288,9 +301,10 @@ Keep your replies short, compassionate and informative.
|
|
288 |
### Responses:
|
289 |
""".strip()
|
290 |
prompt = PromptTemplate(input_variables=["context","question","chat_history"], template=template)
|
291 |
-
|
292 |
|
293 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
|
|
294 |
qa = ConversationalRetrievalChain.from_llm(
|
295 |
llm,
|
296 |
db.as_retriever(),
|
@@ -302,15 +316,15 @@ qa = ConversationalRetrievalChain.from_llm(
|
|
302 |
|
303 |
import gradio as gr
|
304 |
import time
|
305 |
-
|
306 |
with gr.Blocks() as demo:
|
307 |
chatbot = gr.Chatbot()
|
308 |
msg = gr.Textbox()
|
309 |
clear = gr.ClearButton([msg, chatbot])
|
310 |
-
|
311 |
def user(user_message, history):
|
312 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
313 |
-
|
314 |
def bot(history):
|
315 |
response = qa(history[-1][0])
|
316 |
response = response['answer']
|
@@ -320,10 +334,12 @@ with gr.Blocks() as demo:
|
|
320 |
history[-1][1] += character
|
321 |
time.sleep(0.05)
|
322 |
yield history
|
323 |
-
|
324 |
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
325 |
bot, chatbot, chatbot
|
326 |
)
|
|
|
327 |
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
|
|
328 |
demo.queue()
|
329 |
demo.launch(share=True)
|
|
|
1 |
+
# Import necessary libraries
|
2 |
import os
|
3 |
from pathlib import Path
|
4 |
|
|
|
17 |
import gradio as gr
|
18 |
import time
|
19 |
from transformers import AutoTokenizer, GenerationConfig, TextStreamer, pipeline
|
20 |
+
# Defining directory for storing questions
|
21 |
questions_dir=Path("Microsoft_QA")
|
22 |
questions_dir.mkdir(exist_ok=True, parents=True)
|
23 |
|
24 |
+
# Define function for writing question and answer to file
|
25 |
def write_file(question, answer, file_path):
|
26 |
text = f"""
|
27 |
Q: {question}
|
|
|
240 |
answer="""Microsoft Q&A doesn't move or store customer data out of the region it's deployed in.""".strip(),
|
241 |
file_path="question_40.txt",
|
242 |
)
|
243 |
+
# Define model and tokenizer names
|
244 |
model_name = "TheBloke/Nous-Hermes-13B-GPTQ"
|
245 |
model_basename = "nous-hermes-13b-GPTQ-4bit-128g.no-act.order"
|
246 |
+
# Load tokenizer from model name
|
247 |
|
248 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast= True)
|
249 |
+
# Load quantized model from model name and basename
|
250 |
model = AutoGPTQForCausalLM.from_quantized(
|
251 |
model_name,
|
252 |
model_basename= model_basename,
|
253 |
use_safetensors=True,
|
254 |
Trust_remote_code=True,
|
255 |
)
|
256 |
+
# Load generation configuration from model name
|
257 |
generation_config = GenerationConfig.from_pretrained(model_name)
|
258 |
+
# Create TextStreamer object with specified tokenizer and settings
|
259 |
streamer = TextStreamer(
|
260 |
tokenizer, skip_prompt = True, skip_special_tokens=True, use_multiprocessing = False
|
261 |
)
|
262 |
+
# Create text generation pipeline with specified model, tokenizer, and generation parameters
|
263 |
pipe = pipeline(
|
264 |
"text-generation",
|
265 |
model=model,
|
|
|
273 |
batch_size=1,
|
274 |
|
275 |
)
|
276 |
+
# Create huggingfacepipeline object with specified pipeline
|
277 |
llm=HuggingFacePipeline(pipeline=pipe)
|
278 |
+
# Create HuggingFaceEmbeddings object with specified model name
|
279 |
embeddings = HuggingFaceEmbeddings(
|
280 |
model_name= 'embaas/sentence-transformers-multilingual-e5-base'
|
281 |
|
282 |
)
|
283 |
+
# Load documents from directory using DirectoryLoader class
|
284 |
loader = DirectoryLoader("./Microsoft_QA/", glob="**/*txt")
|
285 |
documents = loader.load()
|
286 |
+
|
287 |
+
# Split documents into chunks using CharacterTextSplitter class
|
288 |
text_splitter = CharacterTextSplitter(chunk_size=512, chunk_overlap=0)
|
289 |
texts = text_splitter.split_documents(documents)
|
290 |
+
# Create Chroma database from documents and embeddings
|
291 |
db = Chroma.from_documents(texts, embeddings)
|
292 |
+
# Define prompt template for generating responses
|
293 |
template = """
|
294 |
### Instruction: You're a microsoft QA platform support agent who is talking to user giving them information about the platform. Use only the chat history and the following information
|
295 |
{context}
|
|
|
301 |
### Responses:
|
302 |
""".strip()
|
303 |
prompt = PromptTemplate(input_variables=["context","question","chat_history"], template=template)
|
304 |
+
# Create ConversationBufferMemory object to store chat history
|
305 |
|
306 |
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
|
307 |
+
# Create ConversationalRetrievalChain object from LLM, database retriever, memory, and prompt
|
308 |
qa = ConversationalRetrievalChain.from_llm(
|
309 |
llm,
|
310 |
db.as_retriever(),
|
|
|
316 |
|
317 |
import gradio as gr
|
318 |
import time
|
319 |
+
# Create Gradio user interface with Chatbot and Textbox components and ClearButton for clearing input/output
|
320 |
with gr.Blocks() as demo:
|
321 |
chatbot = gr.Chatbot()
|
322 |
msg = gr.Textbox()
|
323 |
clear = gr.ClearButton([msg, chatbot])
|
324 |
+
# Define function for handling user input and updating chat history
|
325 |
def user(user_message, history):
|
326 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
327 |
+
# Define function for generating bot response using ConversationalRetrievalChain object and updating chat history
|
328 |
def bot(history):
|
329 |
response = qa(history[-1][0])
|
330 |
response = response['answer']
|
|
|
334 |
history[-1][1] += character
|
335 |
time.sleep(0.05)
|
336 |
yield history
|
337 |
+
# Submit user input to user function and update chat history, then generate bot response using bot function and update chat history
|
338 |
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
339 |
bot, chatbot, chatbot
|
340 |
)
|
341 |
+
# Update Gradio interface to be interactive after bot response is generated
|
342 |
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
343 |
+
# Launch Gradio interface with sharing enabled
|
344 |
demo.queue()
|
345 |
demo.launch(share=True)
|