wq2012's picture
Update app.py
7bbcb7d verified
raw
history blame
2.06 kB
import gradio as gr
from gpt4all import GPT4All
from huggingface_hub import hf_hub_download
from diarizationlm import utils
title = "DiarizationLM GGUF inference on CPU"
description = """
A demo of the DiarizationLM model finetuned from Llama 2. In this demo, we run a 4-bit quantized GGUF model on CPU.
To learn more about DiarizationLM, check our paper: https://arxiv.org/abs/2401.03506
"""
model_path = "models"
model_name = "q4_k_m.gguf"
prompt_suffix = " --> "
completion_suffix = " [eod]"
hf_hub_download(repo_id="google/DiarizationLM-13b-Fisher-v1", filename=model_name, local_dir=model_path, local_dir_use_symlinks=False)
print("Start the model init process")
model = GPT4All(model_name=model_name,
model_path=model_path,
allow_download = False,
device="cpu")
print("Finish the model init process")
def generater(message, history):
prompt = message + prompt_suffix
max_new_tokens = round(len(prompt) / 3.0 * 1.2)
outputs = []
for token in model.generate(prompt=prompt,
temp=0.0,
top_k=50,
top_p=0.9,
max_tokens=max_new_tokens,
streaming=True):
outputs.append(token)
completion = "".join(outputs)
yield completion
if completion.endswith(" [eod]"):
break
transferred_completion = utils.transfer_llm_completion(completion, message)
yield transferred_completion
print("Create chatbot")
chatbot = gr.Chatbot()
print("Created chatbot")
iface = gr.ChatInterface(
fn = generater,
title=title,
description = description,
chatbot=chatbot,
additional_inputs=[],
examples=[
["<speaker:1> Hello, how are you doing <speaker:2> today? I am doing well. What about <speaker:1> you? I'm doing well, too. Thank you."],
]
)
with gr.Blocks() as demo:
iface.render()
if __name__ == "__main__":
demo.queue(max_size=3).launch()