File size: 21,399 Bytes
78b2609 c4b1c4a cb7a285 b2962e2 680ca5c 78b2609 17fe3a9 e3ec85b d46516b 2f51135 d46516b e3ec85b 17fe3a9 78b2609 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# -*- coding: utf-8 -*-
conda install -c conda-forge ffmpeg -y
import os
import contextlib
@contextlib.contextmanager
def new_cd(x):
d = os.getcwd()
# This could raise an exception, but it's probably
# best to let it propagate and let the caller
# deal with it, since they requested x
os.chdir(x)
try:
yield
finally:
# This could also raise an exception, but you *really*
# aren't equipped to figure out what went wrong if the
# old working directory can't be restored.
os.chdir(d)
from ipex_llm.transformers import AutoModelForCausalLM
from transformers import LlamaTokenizer
llm = AutoModelForCausalLM.from_pretrained("checkpoints\\Llama-2-7b-chat-hf",load_in_low_bit="sym_int4")
llm.save_low_bit("checkpoints\\Llama-2-7b-chat-hf-INT4")
tokenizer = LlamaTokenizer.from_pretrained("checkpoints\\Llama-2-7b-chat-hf\\")
tokenizer.save_pretrained("checkpoints\\Llama-2-7b-chat-hf-INT4")
from huggingface_hub import snapshot_download
# Clip
snapshot_download(repo_id='openai/clip-vit-base-patch32',
local_dir="./checkpoints/clip-vit-base-patch32")
# LLM
snapshot_download(repo_id='meta-llama/Llama-2-7b-chat-hf',
local_dir="./checkpoints/Llama-2-7b-chat-hf", token=hf_token)
# Translation
snapshot_download(repo_id='Helsinki-NLP/opus-mt-en-zh',
local_dir="./checkpoints/Helsinki-NLP-opus-mt-en-zh")
snapshot_download(repo_id='Helsinki-NLP/opus-mt-zh-en',
local_dir="./checkpoints/Helsinki-NLP-opus-mt-zh-en")
# Embeddings
snapshot_download(repo_id='sentence-transformers/all-MiniLM-L12-v2',
local_dir="./checkpoints/all-MiniLM-L12-v2")
import argparse
import gradio as gr
import os
from models.helperbot_bigdl import Chat
from models.sum_model import Sum
from models.whisper_model import AudioTranslator
from models.llm_model import LlmReasoner
import os
from langchain.chains import ConversationalRetrievalChain, StuffDocumentsChain
from langchain.prompts import PromptTemplate
from ipex_llm.langchain.llms import TransformersLLM
from langchain.vectorstores import FAISS
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
from ipex_llm.langchain.embeddings import TransformersEmbeddings
from langchain import LLMChain
from utils.utils import new_cd
from ipex_llm.langchain.llms import TransformersLLM
from langchain import LLMChain
from langchain.chains.summarize import load_summarize_chain
from langchain.docstore.document import Document
from langchain.prompts import PromptTemplate
from langchain.chains.combine_documents.stuff import StuffDocumentsChain
from langchain.chains import MapReduceDocumentsChain, ReduceDocumentsChain
from langchain.text_splitter import CharacterTextSplitter, RecursiveCharacterTextSplitter
import whisper
from ipex_llm import optimize_model
def has_intersection(t1, t2):
if t1[1] < t2[0] or t2[1] < t1[0]:
return False
else:
return True
class AudioTranslator():
def __init__(self, args):
self.model = whisper.load_model(args.whisper_version, download_root='checkpoints')
self.model = optimize_model(self.model)
def __call__(self, video_path):
"""
input: video_path (str)
output: audio_results (list)
"""
print("Extract the audio results.")
audio_results = self.model.transcribe(video_path, task = 'translate')["segments"]
print("Finished.")
return audio_results
def match(self, audio_results):
transcript = ''
for res in audio_results:
transcript += res['text'] + ' '
# if has_intersection((start, end), (res["start"], res["end"])):
# transcript += res['text'] + ' '
return transcript
class Sum():
def __init__(self, args):
self.llm_version = args.llm_version
# self.max_tokens = args.qa_max_new_tokens
def summarize_refine(self, script):
text_splitter = CharacterTextSplitter(chunk_size=1024, separator="\n", chunk_overlap=0)
texts = text_splitter.split_text(script)
docs = [Document(page_content=t) for t in texts]
llm = TransformersLLM.from_model_id_low_bit(f"checkpoint\\{self.llm_version}")
prompt_template = """Write a concise summary of the following:
{text}
CONCISE SUMMARY:"""
prompt = PromptTemplate.from_template(prompt_template)
refine_template = (
"Your job is to produce a final summary\n"
"We have provided an existing summary up to a certain point: {existing_answer}\n"
"We have the opportunity to refine the existing summary"
"(only if needed) with some more context below.\n"
"------------\n"
"{text}\n"
"------------\n"
"If the context isn't useful, return the original summary."
)
refine_prompt = PromptTemplate.from_template(refine_template)
chain = load_summarize_chain(
llm=llm,
chain_type="refine",
question_prompt=prompt,
refine_prompt=refine_prompt,
return_intermediate_steps=True,
input_key="input_documents",
output_key="output_text",
)
result = chain({"input_documents": docs}, return_only_outputs=True)
return result
def summarize_mapreduce(self, script):
text_splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=0)
texts = text_splitter.split_text(script)
text = [Document(page_content=t) for t in texts]
llm = TransformersLLM.from_model_id_low_bit(f"checkpoint\\{self.llm_version}")
# Map
map_template = """The following is a meeting recording
=========
{texts}
=========
Based on this list of recordings, please summary the main idea briefly
Helpful Answer:"""
map_prompt = PromptTemplate.from_template(map_template)
map_chain = LLMChain(llm=llm, prompt=map_prompt, llm_kwargs={"max_new_tokens": 512})
# Reduce
reduce_template = """The following is set of summaries:
=========
{texts}
=========
Take these and distill it into a final, consolidated summary of the meeting.
Helpful Answer:"""
reduce_prompt = PromptTemplate.from_template(reduce_template)
reduce_chain = LLMChain(llm=llm, prompt=reduce_prompt, llm_kwargs={"max_new_tokens": 4096})
# Takes a list of documents, combines them into a single string, and passes this to an LLMChain
combine_documents_chain = StuffDocumentsChain(
llm_chain=reduce_chain, document_variable_name="texts"
)
# Combines and iteratively reduces the mapped documents
reduce_documents_chain = ReduceDocumentsChain(
combine_documents_chain=combine_documents_chain,
collapse_documents_chain=combine_documents_chain,
token_max=4000,
)
# Combining documents by mapping a chain over them, then combining results
map_reduce_chain = MapReduceDocumentsChain(
llm_chain=map_chain,
reduce_documents_chain=reduce_documents_chain,
document_variable_name="texts",
return_intermediate_steps=False,
)
result = map_reduce_chain({"input_documents": text}, return_only_outputs=True)
# print("-." * 40)
# print(result)
result = result['output_text'].split("Helpful Answer:").strip()[-1]
return result
def summarize(self, script):
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1024, chunk_overlap=0)
texts = text_splitter.split_text(script)
prompt_template = """The following is a piece of meeting recording:
<<<{text}>>>
Based on recording, summary the main idea fluently.
JUST SUMMARY!NO OTHER WORDS!
SUMMARY:"""
reduce_template = """The following is a meeting recording pieces:
<<<{text}>>>
Take these and distill it into a final, consolidated summary of the meeting.
JUST SUMMARY!NO OTHER WORDS!
SUMMARY:"""
print(len(texts))
for text in texts:
print(text)
print("\n")
llm = TransformersLLM.from_model_id_low_bit(
f"checkpoint\\{self.llm_version}")
sum_split = []
for text in texts:
response = llm(prompt=prompt_template.format(text=text), max_new_tokens=1024)
print(response)
response_answer = response.split("SUMMARY:")
sum_split.append(response_answer[1])
sum_all = "\n".join(sum_split)
result = llm(prompt=reduce_template.format(text=sum_all), max_new_tokens=4000)
result_split = result.split("SUMMARY:")
return result_split[1]
# # for test
# import argparse
#
# parser = argparse.ArgumentParser()
# parser.add_argument("--llm_version", default="Llama-2-7b-chat-hf-INT4", help="LLM model version")
# args = parser.parse_args()
# file_path = "../test.txt"
# with open(file_path, "r", encoding="utf-8") as file:
# content = file.read()
# Sumbot = Sum(args)
# result = Sumbot.summarize_map(content)
# print("-." * 20)
# print(result)
parent_dir = os.path.dirname(__file__)
condense_template = """
Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
You can assume the discussion is about the video content.
REMEMBER: If there is no relevant information within the context, just say "Hmm, I'm \
not sure." Don't try to make up an answer. \
Chat History:
{chat_history}
Follow Up Question: {question}
Standalone question:
"""
qa_template = """
You are an AI assistant designed for answering questions about a meeting.
You are given a word records of this meeting.
Try to comprehend the dialogs and provide a answer based on it.
=========
{context}
=========
Question: {question}
Answer:
"""
# CONDENSE_QUESTION_PROMPT 用于将聊天历史记录和下一个问题压缩为一个独立的问题
CONDENSE_QUESTION_PROMPT = PromptTemplate.from_template(condense_template)
# QA_PROMPT为机器人设定基调和目的
QA_PROMPT = PromptTemplate(template=qa_template, input_variables=["question", "context"])
# DOC_PROMPT = PromptTemplate.from_template("Video Clip {video_clip}: {page_content}")
DOC_PROMPT = PromptTemplate.from_template("{page_content}")
class LlmReasoner():
def __init__(self, args):
self.history = []
self.llm_version = args.llm_version
self.embed_version = args.embed_version
self.qa_chain = None
self.vectorstore = None
self.top_k = args.top_k
self.qa_max_new_tokens = args.qa_max_new_tokens
self.init_model()
def init_model(self):
with new_cd(parent_dir):
self.llm = TransformersLLM.from_model_id_low_bit(
f"..\\checkpoints\\{self.llm_version}")
self.llm.streaming = False
self.embeddings = TransformersEmbeddings.from_model_id(
model_id=f"..\\checkpoints\\{self.embed_version}")
def create_qa_chain(self, args, input_log):
self.top_k = args.top_k
self.qa_max_new_tokens = args.qa_max_new_tokens
self.question_generator = LLMChain(llm=self.llm, prompt=CONDENSE_QUESTION_PROMPT)
self.answer_generator = LLMChain(llm=self.llm, prompt=QA_PROMPT,
llm_kwargs={"max_new_tokens": self.qa_max_new_tokens})
self.doc_chain = StuffDocumentsChain(llm_chain=self.answer_generator, document_prompt=DOC_PROMPT,
document_variable_name='context')
# 拆分查看字符的文本, 创建一个新的文本分割器
# self.text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0, keep_separator=True)
self.text_splitter = RecursiveCharacterTextSplitter(chunk_size=2048, chunk_overlap=0)
texts = self.text_splitter.split_text(input_log)
self.vectorstore = FAISS.from_texts(texts, self.embeddings,
metadatas=[{"video_clip": str(i)} for i in range(len(texts))])
retriever = self.vectorstore.as_retriever(search_kwargs={"k": self.top_k})
self.qa_chain = ConversationalRetrievalChain(retriever=retriever,
question_generator=self.question_generator,
combine_docs_chain=self.doc_chain,
return_generated_question=True,
return_source_documents=True,
rephrase_question=False)
def __call__(self, question):
response = self.qa_chain({"question": question, "chat_history": self.history})
answer = response["answer"]
generated_question = response["generated_question"]
source_documents = response["source_documents"]
self.history.append([question, answer])
return self.history, generated_question, source_documents
def clean_history(self):
self.history = []
class Chat:
def __init__(self, args) -> None:
self.args = args
def init_model(self):
print('\033[1;33m' + "Initializing models...".center(50, '-') + '\033[0m')
self.audio_translator = AudioTranslator(self.args)
self.llm_reasoner = LlmReasoner(self.args)
print('\033[1;32m' + "Model initialization finished!".center(50, '-') + '\033[0m')
def video2log(self, video_path):
audio_results = self.audio_translator(video_path)
en_log_result = []
en_log_result_tmp = ""
audio_transcript = self.audio_translator.match(audio_results)
en_log_result_tmp += f"\n{audio_transcript}"
en_log_result.append(en_log_result_tmp)
en_log_result = "\n\n".join(en_log_result)
print(f"\033[1;34mLog: \033[0m\n{en_log_result}\n")
return en_log_result
def chat2video(self, args, user_input, en_log_result):
self.llm_reasoner.create_qa_chain(args, en_log_result)
en_user_input = user_input
print("\n\033[1;32mGnerating response...\033[0m")
answer, generated_question, source_documents = self.llm_reasoner(en_user_input)
print(f"\033[1;32mQuestion: \033[0m{user_input}")
print(f"\033[1;32mAnswer: \033[0m{answer[0][1]}")
self.clean_history()
return answer, generated_question, source_documents
def clean_history(self):
self.llm_reasoner.clean_history()
return
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
parser = argparse.ArgumentParser()
# whisper model arguments
parser.add_argument("--whisper_version", default="small", help="Whisper model version for video asr")
# llm model arguments
parser.add_argument("--llm_version", default="Llama-2-7b-chat-hf-INT4", help="LLM model version")
parser.add_argument("--embed_version", default="all-MiniLM-L12-v2", help="Embedding model version")
parser.add_argument("--top_k", default=3, type=int, help="Return top k relevant contexts to llm")
parser.add_argument("--qa_max_new_tokens", default=128, type=int, help="Number of max new tokens for llm")
# general arguments
parser.add_argument("--port", type=int, default=7860, help="Gradio server port")
args = parser.parse_args()
chat = Chat(args)
sumbot = Sum(args)
chat.init_model()
global_chat_history = []
global_result = ""
global_summary = ""
def clean_conversation():
global global_chat_history
chat.clean_history()
global_chat_history = []
return '', gr.update(value=None, interactive=True), None, gr.update(value=None, visible=True), gr.update(value=None,
visible=True)
def clean_chat_history():
global global_chat_history
chat.clean_history()
global_chat_history = []
return '', None
def submit_message(message, max_tokens, top_p):
args.qa_max_new_tokens = max_tokens
args.top_k = top_p
print(args)
chat_history, generated_question, source_documents = chat.chat2video(args, message, global_result)
global_chat_history.append((message, chat_history[0][1]))
return '', global_chat_history
def gen_script(vid_path):
print(vid_path)
global global_result
if vid_path is None:
log_text = "===== Please upload video! ====="
gr.update(value=log_text, visible=True)
else:
global_result = chat.video2log(vid_path)
# script_pth = download_script_file()
return gr.update(value=global_result, visible=True), download_script_file()
def download_script_file():
try:
with open("script_result.txt", "w") as file:
file.write(global_result)
return "script_result.txt"
except Exception as e:
return f"Error preparing file for download: {str(e)}"
def download_sum_file():
try:
with open("sum_result.txt", "w") as file:
file.write(global_summary)
return "sum_result.txt"
except Exception as e:
return f"Error preparing file for download: {str(e)}"
def upload_file(files):
global global_result
file_paths = [file.name for file in files][0]
try:
with open(file_paths, "r", encoding="utf-8") as file:
file_content = file.read()
global_result = file_content
except FileNotFoundError:
print("File not found")
except IOError:
print("Error occurred while reading the file")
return file_content, download_script_file()
def summary():
global global_summary
global_summary = sumbot.summarize(global_result)
return gr.update(value=global_summary, visible=True), download_sum_file()
css = """
#col-container {max-width: 80%; margin-left: auto; margin-right: auto;}
#video_inp {min-height: 100px}
#chatbox {min-height: 100px;}
#header {text-align: center;}
#hint {font-size: 1.0em; padding: 0.5em; margin: 0;}
.message { font-size: 1.2em; }
"""
with gr.Blocks(css=css) as demo:
with gr.Column(elem_id="col-container"):
gr.Markdown(""" ## Meeting Helper Bot
Upload meeting recording in mp3/mp4/txt format and you can get the summary and chat based on content
(You can adjust parameters based on your needs)
Powered by BigDL, Llama, Whisper, and LangChain""",
elem_id="header")
with gr.Column() as advanced_column:
max_new_tokens = gr.Slider(label="Max new tokens", minimum=1, maximum=1024, step=1, value=128)
top_k = gr.Slider(label="Top-k", minimum=1, maximum=50, step=1, value=3)
with gr.Row():
with gr.Column():
video_inp = gr.Video(label="1.Upload MP3/MP4 File")
# file_inp = gr.File(label="file/doc_input")
upload_button = gr.UploadButton("1. Or Click to Upload a txt File", file_types=["doc", "txt"],
file_count="multiple")
gen_btn = gr.Button("2. Generate Script")
sum_outp = gr.Textbox(label="Summerization output", lines=15)
# save_sum_btn = gr.Button("Save Summarization to txt file")
save_sum_dl = gr.outputs.File(label="Download Summary")
# save_sum_btn.click(download_sum_file, [], outputs=[gr.outputs.File(label="Download Summary")])
with gr.Column():
script_outp = gr.Textbox(label="Script output", lines=30)
with gr.Row():
script_summarization_btn = gr.Button("3.Script Summarization ")
# save_script_btn = gr.Button("Save Script to txt file")
save_script_dl = gr.outputs.File(label="Download Script")
# save_script_btn.click(download_script_file, [], outputs=[gr.outputs.File(label="Download Script")])
with gr.Column():
chatbot = gr.Chatbot(elem_id="chatbox")
input_message = gr.Textbox(show_label=False, placeholder="Enter text and press enter", visible=True)
btn_submit = gr.Button("Submit")
with gr.Row():
btn_clean_chat_history = gr.Button("Clean Chat History")
btn_clean_conversation = gr.Button("Start New Conversation")
upload_button.upload(upload_file, upload_button, [script_outp, save_script_dl])
gen_btn.click(gen_script, [video_inp], [script_outp, save_script_dl])
script_summarization_btn.click(summary, [], [sum_outp, save_sum_dl])
btn_submit.click(submit_message, [input_message, max_new_tokens, top_k], [input_message, chatbot])
input_message.submit(submit_message, [input_message, max_new_tokens, top_k], [input_message, chatbot])
btn_clean_conversation.click(clean_conversation, [], [input_message, video_inp, chatbot, sum_outp, script_outp])
btn_clean_chat_history.click(clean_chat_history, [], [input_message, chatbot])
demo.load(queur=False)
demo.queue(concurrency_count=1)
demo.launch(height='800px', server_port=args.port, debug=True, share=False) |