Spaces:
Runtime error
Runtime error
Commit
·
5009b7f
1
Parent(s):
87bf273
Upload 2 files
Browse files- app.py +115 -0
- requirements.txt +8 -0
app.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline, StoppingCriteria, StoppingCriteriaList, TextIteratorStreamer
|
4 |
+
import time
|
5 |
+
import numpy as np
|
6 |
+
from torch.nn import functional as F
|
7 |
+
import os
|
8 |
+
from threading import Thread
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
model_path = "ayoolaolafenwa/ChatLM"
|
13 |
+
access_token = "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"
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, use_auth_token = access_token)
|
15 |
+
|
16 |
+
model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code = True, device_map = "auto",
|
17 |
+
torch_dtype=torch.bfloat16, load_in_8bit=True, use_auth_token = access_token)
|
18 |
+
|
19 |
+
class StopOnTokens(StoppingCriteria):
|
20 |
+
def __call__(self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs) -> bool:
|
21 |
+
stop_ids = [0]
|
22 |
+
for stop_id in stop_ids:
|
23 |
+
if input_ids[0][-1] == stop_id:
|
24 |
+
return True
|
25 |
+
return False
|
26 |
+
|
27 |
+
|
28 |
+
def user(message, history):
|
29 |
+
# Append the user's message to the conversation history
|
30 |
+
return "", history + [[message, ""]]
|
31 |
+
|
32 |
+
|
33 |
+
def chat(curr_system_message, history):
|
34 |
+
# Initialize a StopOnTokens object
|
35 |
+
stop = StopOnTokens()
|
36 |
+
|
37 |
+
# Construct the input message string for the model by concatenating the current system message and conversation history
|
38 |
+
messages = curr_system_message + \
|
39 |
+
"".join(["".join(["<user>: "+item[0], "<chatbot>: "+item[1]])
|
40 |
+
for item in history])
|
41 |
+
|
42 |
+
# Tokenize the messages string
|
43 |
+
tokens = tokenizer([messages], return_tensors="pt").to("cuda")
|
44 |
+
streamer = TextIteratorStreamer(
|
45 |
+
tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
46 |
+
|
47 |
+
token_ids = tokens.input_ids
|
48 |
+
attention_mask=tokens.attention_mask
|
49 |
+
|
50 |
+
generate_kwargs = dict(
|
51 |
+
input_ids=token_ids,
|
52 |
+
attention_mask = attention_mask,
|
53 |
+
streamer = streamer,
|
54 |
+
max_length=2048,
|
55 |
+
do_sample=True,
|
56 |
+
num_return_sequences=1,
|
57 |
+
eos_token_id=tokenizer.eos_token_id,
|
58 |
+
temperature = 0.7,
|
59 |
+
stopping_criteria=StoppingCriteriaList([stop])
|
60 |
+
)
|
61 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
62 |
+
t.start()
|
63 |
+
|
64 |
+
#Initialize an empty string to store the generated text
|
65 |
+
partial_text = ""
|
66 |
+
for new_text in streamer:
|
67 |
+
# print(new_text)
|
68 |
+
partial_text += new_text
|
69 |
+
history[-1][1] = partial_text
|
70 |
+
# Yield an empty string to cleanup the message textbox and the updated conversation history
|
71 |
+
yield history
|
72 |
+
return partial_text
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
with gr.Blocks() as demo:
|
78 |
+
# history = gr.State([])
|
79 |
+
gr.Markdown("# ChatLM")
|
80 |
+
|
81 |
+
with gr.Row():
|
82 |
+
with gr.Column():
|
83 |
+
gr.Markdown(
|
84 |
+
"""
|
85 |
+
ChatLM is a chat Large Language model finetuned with pretrained [Falcon-1B model](https://huggingface.co/tiiuae/falcon-rw-1b)
|
86 |
+
and trained on [chat-bot-instructions prompts dataset](https://huggingface.co/datasets/ayoolaolafenwa/sft-data).
|
87 |
+
ChatLM was trained on a dataset containing normal day to day human conversations, due to limited data used in training
|
88 |
+
it is not suitable for tasks like coding and current affairs.
|
89 |
+
"""
|
90 |
+
)
|
91 |
+
|
92 |
+
chatbot = gr.Chatbot().style(height=500)
|
93 |
+
with gr.Row():
|
94 |
+
with gr.Column():
|
95 |
+
msg = gr.Textbox(label="Chat Message Box", placeholder="Chat Message Box",
|
96 |
+
show_label=False).style(container=False)
|
97 |
+
with gr.Column():
|
98 |
+
with gr.Row():
|
99 |
+
submit = gr.Button("Run")
|
100 |
+
stop = gr.Button("Stop")
|
101 |
+
clear = gr.Button("Clear")
|
102 |
+
system_msg = gr.Textbox(
|
103 |
+
label="Response Message", interactive=False, visible=False)
|
104 |
+
|
105 |
+
submit_event = msg.submit(fn=user, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False).then(
|
106 |
+
fn=chat, inputs=[system_msg, chatbot], outputs=[chatbot], queue=True)
|
107 |
+
submit_click_event = submit.click(fn=user, inputs=[msg, chatbot], outputs=[msg, chatbot], queue=False).then(
|
108 |
+
fn=chat, inputs=[system_msg, chatbot], outputs=[chatbot], queue=True)
|
109 |
+
stop.click(fn=None, inputs=None, outputs=None, cancels=[
|
110 |
+
submit_event, submit_click_event], queue=False)
|
111 |
+
clear.click(lambda: None, None, [chatbot], queue=False)
|
112 |
+
|
113 |
+
|
114 |
+
demo.queue(max_size=32, concurrency_count=2)
|
115 |
+
demo.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
torch
|
3 |
+
transformers
|
4 |
+
numpy
|
5 |
+
einops
|
6 |
+
accelerate
|
7 |
+
bitsandbytes
|
8 |
+
scipy
|