File size: 2,237 Bytes
05fbf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
)

# Chat Interface
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 darshan.kholakiya@dell.com 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 vijay.shah@nividia.com and I want to pitch Darshan to buy semi conductorΒ chipsΒ fromΒ us."]],
        title="Marketing Email Generator")

demo.queue().launch(share=True,debug=True)