|
import torch |
|
from transformers import AutoModelForCausalLM, AutoTokenizer, GenerationConfig, pipeline, TextIteratorStreamer |
|
import gradio as gr |
|
from torch import bfloat16 |
|
from threading import Thread |
|
|
|
MODEL_NAME = "HuggingFaceH4/zephyr-7b-beta" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
MODEL_NAME, device_map="auto", torch_dtype=torch.float16, load_in_4bit=True |
|
) |
|
|
|
|
|
system_prompt = "You are a helpful assistant who helps user to complete its query. If you don't know the answer be honet don't provide false information." |
|
|
|
def prompt_build(system_prompt, user_inp, hist): |
|
prompt = f"""<|system|>\n{system_prompt}\n""" |
|
|
|
for pair in hist: |
|
prompt += f"""<|user|>\n{pair[0]}\n<|assistant|>\n{pair[1]}""" |
|
|
|
prompt += f"""<|user|>\n{user_inp}\n<|assistant|>\n""" |
|
return prompt |
|
|
|
def chat(user_input, history): |
|
if not user_input: |
|
yield "Please write your query so that I can assist you even better." |
|
return |
|
|
|
prompt = prompt_build(system_prompt, user_input, history) |
|
model_inputs = tokenizer([prompt], return_tensors="pt").to("cuda") |
|
|
|
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True) |
|
|
|
generate_kwargs = dict( |
|
model_inputs, |
|
streamer=streamer, |
|
max_new_tokens=2000, |
|
do_sample=True, |
|
top_p=0.7, |
|
temperature=0.8 |
|
) |
|
t = Thread(target=model.generate, kwargs=generate_kwargs) |
|
t.start() |
|
|
|
model_output = "" |
|
for new_text in streamer: |
|
model_output += new_text |
|
yield model_output |
|
return model_output |
|
|
|
|
|
with gr.Blocks() as demo: |
|
chatbot = gr.ChatInterface(fn=chat,examples=[["Hello, Good Morning!"],["Who is Virat Kohli?"],["Write an email to Client call Darshan Kholakiya whose email address is [email protected] and address is Alaknanda Building, Rawalapada, Borivali East, Mumbai-400008. My name is Vijay and I am a sales person from Nividia, my phone number is 7710020978 and email is [email protected] and I want to pitch Darshan to buy semi conductorΒ chipsΒ fromΒ us."]], |
|
title="Marketing Email Generator") |
|
|
|
demo.queue().launch(share=True,debug=True) |
|
|
|
|