Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,63 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
|
28 |
-
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
41 |
|
42 |
-
""
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
gr.
|
52 |
-
|
53 |
-
maximum=1
|
54 |
-
value=0.
|
55 |
-
step=0.05,
|
56 |
-
|
57 |
-
)
|
58 |
-
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
|
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from unsloth import FastLanguageModel
|
4 |
+
from transformers import TextStreamer
|
5 |
+
from unsloth.chat_templates import get_chat_template
|
6 |
|
7 |
+
# Initialize the model
|
8 |
+
max_seq_length = 2048
|
9 |
+
dtype = None
|
10 |
+
load_in_4bit = True
|
11 |
|
12 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
13 |
+
model_name="umair894/llama3",
|
14 |
+
max_seq_length=max_seq_length,
|
15 |
+
dtype=dtype,
|
16 |
+
load_in_4bit=load_in_4bit,
|
17 |
+
)
|
18 |
|
19 |
+
tokenizer = get_chat_template(
|
20 |
+
tokenizer,
|
21 |
+
chat_template="llama-3",
|
22 |
+
mapping={"role": "from", "content": "value", "user": "human", "assistant": "gpt"},
|
23 |
+
map_eos_token=True,
|
24 |
+
)
|
|
|
|
|
|
|
25 |
|
26 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# VIKK introduction prompt
|
29 |
+
vikk_intro = """Consider you self a legal assistant in USA and your name is VIKK. You are very knowledgeable about all aspects of the law...
|
30 |
+
"""
|
31 |
+
|
32 |
+
# Function to get chat response
|
33 |
+
def get_response(message, history, system_message, max_tokens, temperature, top_p):
|
34 |
+
messages = [{"role": "system", "content": system_message}] if system_message else []
|
35 |
+
if not history:
|
36 |
+
history = [{"role": "assistant", "content": vikk_intro}]
|
37 |
+
|
38 |
+
for msg in history:
|
39 |
+
if msg[0]:
|
40 |
+
messages.append({"role": "user", "content": msg[0]})
|
41 |
+
if msg[1]:
|
42 |
+
messages.append({"role": "assistant", "content": msg[1]})
|
43 |
+
|
44 |
messages.append({"role": "user", "content": message})
|
45 |
|
46 |
+
formatted_messages = [{"from": "assistant", "value": vikk_intro}]
|
47 |
+
for msg in messages[1:]:
|
48 |
+
role = "human" if msg["role"] == "user" else "assistant"
|
49 |
+
formatted_messages.append({"from": role, "value": msg["content"]})
|
50 |
|
51 |
+
inputs = tokenizer.apply_chat_template(
|
52 |
+
formatted_messages,
|
53 |
+
tokenize=True,
|
54 |
+
add_generation_prompt=True,
|
55 |
+
return_tensors="pt",
|
56 |
+
).to("cuda")
|
|
|
|
|
57 |
|
58 |
+
text_streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
59 |
+
|
60 |
+
output = ""
|
61 |
+
for out in model.generate(input_ids=inputs["input_ids"], streamer=text_streamer, max_new_tokens=max_tokens, use_cache=True):
|
62 |
+
output += out
|
63 |
|
64 |
+
response = tokenizer.decode(output, skip_special_tokens=True).split(">>> Assistant: ")[-1].strip()
|
65 |
+
|
66 |
+
return response
|
67 |
+
|
68 |
+
# Gradio interface
|
69 |
+
with gr.Blocks() as demo:
|
70 |
+
gr.Markdown("# Chatbot Interface")
|
71 |
+
|
72 |
+
with gr.Row():
|
73 |
+
with gr.Column():
|
74 |
+
system_message = gr.Textbox(value="You are a friendly Chatbot.", label="System message")
|
75 |
+
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
76 |
+
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
77 |
+
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
78 |
+
|
79 |
+
with gr.Column():
|
80 |
+
chatbot = gr.Chatbot()
|
81 |
+
|
82 |
+
user_input = gr.Textbox(label="You:")
|
83 |
+
send_button = gr.Button("Send")
|
84 |
+
|
85 |
+
def respond(message, history, system_message, max_tokens, temperature, top_p):
|
86 |
+
response = get_response(message, history, system_message, max_tokens, temperature, top_p)
|
87 |
+
history.append((message, response))
|
88 |
+
return history
|
89 |
|
90 |
+
send_button.click(respond, [user_input, chatbot, system_message, max_tokens, temperature, top_p], chatbot)
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
+
demo.launch()
|