File size: 7,766 Bytes
a716434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
827dffc
 
a716434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b804fb
a716434
 
0b804fb
a716434
 
 
 
 
 
 
 
 
2c007f8
 
 
 
 
 
 
 
 
 
 
 
0b804fb
a716434
 
 
 
 
 
 
 
 
 
 
 
 
1fcf650
 
a716434
1fcf650
a716434
 
 
 
 
 
 
 
 
 
0b804fb
a716434
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cc7f59
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
import os
from typing import Optional
from threading import Thread

import torch
import gradio as gr
from langchain.llms.base import LLM
from langchain.prompts import PromptTemplate
from langchain_community.vectorstores import Pinecone
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationalRetrievalChain
from langchain_community.embeddings import HuggingFaceBgeEmbeddings
from langchain_community.llms.huggingface_pipeline import HuggingFacePipeline
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextIteratorStreamer, pipeline


os.environ["TOKENIZERS_PARALLELISM"] = "false"

def initialize_model_and_tokenizer(model_name="mistralai/Mistral-7B-Instruct-v0.2"):   
    quantization_config = BitsAndBytesConfig(
        load_in_4bit=True,
        bnb_4bit_compute_dtype=torch.float16,
        bnb_4bit_quant_type="nf4",
        bnb_4bit_use_double_quant=True,
    )
    model = AutoModelForCausalLM.from_pretrained(
        model_name,
        trust_remote_code=True,
        torch_dtype=torch.float16,
        device_map='auto',
        quantization_config=quantization_config
    )
    model.eval()
    tokenizer = AutoTokenizer.from_pretrained(model_name)
    tokenizer.pad_token = tokenizer.eos_token
    return model, tokenizer

def init_chain(model, tokenizer, db, embed, temp, max_new_tokens, top_p, top_k, r_penalty):
    class CustomLLM(LLM):

        """Streamer Object"""

        streamer: Optional[TextIteratorStreamer] = None

        def _call(self, prompt, stop=None, run_manager=None) -> str:
            self.streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, Timeout=5)
            inputs = tokenizer(prompt, return_tensors="pt")
            input_ids = inputs["input_ids"].to('cuda')
            generate_kwargs = dict(
                temperature=float(temp),
                max_new_tokens=int(max_new_tokens),
                top_p=float(top_p),
                top_k=int(top_k),
                repetition_penalty=float(r_penalty),
                do_sample=True
                )
            kwargs = dict(input_ids=input_ids, streamer=self.streamer, **generate_kwargs)
            thread = Thread(target=model.generate, kwargs=kwargs)
            thread.start()
            return ""

        @property
        def _llm_type(self) -> str:
            return "custom"

    llm = CustomLLM()
    memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
    questionprompt = PromptTemplate.from_template(
        """<s>[INST] 
        Use the following pieces of context to answer the question at the end. 
        If you don't know the answer, just say that you don't know, don't try to make up an answer.
        Instead of they/them refer to Dmytro Kisil as he/him. 
        CONTEXT: {context} 
        CHAT HISTORY: {chat_history} 
        QUESTION: {question} 
        Helpful Answer: 
        [/INST]
        """
    )
    llm_chain = ConversationalRetrievalChain.from_llm(
      llm=llm,
      retriever=db.as_retriever(search_kwargs={"k": 5}),
      memory=memory,
      condense_question_prompt=questionprompt,
    )
    
    return llm_chain, llm

index_name = "resume-demo"

queries = [["Which masters degree Dmytro Kisil has?"],
           ["Which amount of salary does Dmytro Kisil is looking for?"],
           ["How long does Dmytro Kisil looking for a job?"],
           ["Why Dmytro Kisil moved to the Netherlands?"],
           ["When Dmytro Kisil left Ukraine?"],
           ["Where Dmytro Kisil live now?"],
           ["How many years of working experience in total does Dmytro Kisil have?"],
           ["How fast Dmytro Kisil can start working for my company?"]]

embed = HuggingFaceBgeEmbeddings(model_name='BAAI/bge-small-en-v1.5')

db = Pinecone.from_existing_index(index_name, embed)

model, tokenizer = initialize_model_and_tokenizer(model_name="mistralai/Mistral-7B-Instruct-v0.2")

with gr.Blocks() as demo:
    with gr.Column():
        gr.HTML("""
                <center>
                <h1>Ask about my work experience!<h1>
                </center>
                <center>
                Start typing your question or choose one of the examples below to start from
                </center>
                """)
        with gr.Row():
            gr.Markdown("[Resume]('https://drive.google.com/file/d/1OejkWuQKcjP73_uH6sfnj9u4hfmXQ-Oy/view?usp=sharing')")
            gr.Markdown("[LinkedIn]('https://www.linkedin.com/in/dmytro-kisil/')")
            gr.Markdown("[HuggingFace profile]('https://huggingface.co/Oysiyl')")
    with gr.Column():
        chatbot = gr.Chatbot()
        with gr.Row():
            msg = gr.Textbox(scale=9)
            submit_b = gr.Button("Submit", scale=1)
        with gr.Row():
            retry_b = gr.Button("Retry")
            undo_b = gr.Button("Undo")
            clear_b = gr.Button("Clear")
    examples = gr.Examples(queries, msg)
    with gr.Accordion("Additional options", open=False):
        temp = gr.Slider(
            label="Temperature",
            value=0.0001,
            minimum=0.0001,
            maximum=1.00,
            step=0.0001,
            interactive=True,
            info="Higher values produce more diverse outputs",
        )
        max_new_tokens = gr.Slider(
            label="Max new tokens",
            value=1024,
            minimum=64,
            maximum=8192,
            step=64,
            interactive=True,
            info="The maximum number of new tokens",
        )
        top_p = gr.Slider(
            label="Top-p (nucleus sampling)",
            value=0.95,
            minimum=0.00,
            maximum=1.00,
            step=0.01,
            interactive=True,
            info="Higher values sample more low-probability tokens",
        )
        top_k = gr.Slider(
            label="Top-k",
            value=40,
            minimum=0,
            maximum=100,
            step=1,
            interactive=True,
            info="select from top 0 tokens (because zero, relies on top_p)",
        )
        r_penalty = gr.Slider(
            label="Repetition penalty",
            value=1.15,
            minimum=1.0,
            maximum=2.0,
            step=0.01,
            interactive=True,
            info="Penalize repeated tokens",
        )

    def user(user_message, history):
        return "", history + [[user_message, None]]
    
    def undo(history):
        return history[:-1].copy()

    def retry(user_message, history):
        try:
            prev_user_message = history[-1][0]
        except:
            prev_user_message = ""
        return prev_user_message, history + [[prev_user_message, None]]

    def bot(history, temp, max_new_tokens, top_p, top_k, r_penalty):
        llm_chain, llm = init_chain(model, tokenizer, db, embed, temp, max_new_tokens, top_p, top_k, r_penalty)
        llm_chain.run(question=history[-1][0])
        history[-1][1] = ""
        for character in llm.streamer:
            history[-1][1] += character
            yield history
        llm_chain, llm = init_chain(model, tokenizer, db, embed, temp, max_new_tokens, top_p, top_k, r_penalty)  
    
    msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, [chatbot, temp, max_new_tokens, top_p, top_k, r_penalty], chatbot)
    submit_b.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, [chatbot, temp, max_new_tokens, top_p, top_k, r_penalty], chatbot)
    retry_b.click(retry, [msg, chatbot], [msg, chatbot], queue=False).then(bot, [chatbot, temp, max_new_tokens, top_p, top_k, r_penalty], chatbot)
    clear_b.click(lambda: None, None, chatbot, queue=False)
    undo_b.click(undo, chatbot, chatbot, queue=False)
    
demo.queue()
demo.launch()