Spaces:
Runtime error
Runtime error
update
Browse files- app.py +57 -60
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,77 +1,74 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import spaces
|
|
|
|
| 3 |
import torch
|
| 4 |
-
|
| 5 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
|
| 13 |
@spaces.GPU
|
| 14 |
-
def
|
| 15 |
-
|
| 16 |
-
model_name_or_path = model_to_use
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
load_in_4bit=True,
|
| 21 |
-
bnb_4bit_quant_type="nf4",
|
| 22 |
-
bnb_4bit_compute_dtype="bfloat16",
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
-
model.pad_token_id = model.config.eos_token_id
|
| 33 |
-
|
| 34 |
-
return "Model loaded and ready!"
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if
|
| 39 |
-
|
| 40 |
|
| 41 |
-
|
| 42 |
-
history.append(("User", message))
|
| 43 |
-
|
| 44 |
-
# Generate the model's response
|
| 45 |
-
input_text = " ".join([msg for _, msg in history])
|
| 46 |
-
input_ids = tokenizer(input_text, return_tensors='pt').input_ids.cuda()
|
| 47 |
-
output = model.generate(inputs=input_ids, max_new_tokens=50)
|
| 48 |
-
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 49 |
-
|
| 50 |
-
# Add the model's response to the history
|
| 51 |
-
history.append(("Bot", generated_text))
|
| 52 |
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
threading.Thread(target=load_model).start()
|
| 58 |
|
| 59 |
-
with gr.
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
status_text = gr.Textbox(label="Status", value="Loading model, please wait...")
|
| 63 |
-
send_button = gr.Button("Send", interactive=False) # Disable the send button initially
|
| 64 |
-
|
| 65 |
-
chatbot = gr.Chatbot()
|
| 66 |
-
message = gr.Textbox(label="Your Message")
|
| 67 |
-
|
| 68 |
-
def enable_send_button():
|
| 69 |
-
send_button.interactive = True
|
| 70 |
-
status_text.value = "Model loaded and ready!"
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import sentencepiece
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'max_split_size_mb:120'
|
| 10 |
+
model_id = "thesven/Llama3-8B-SFT-code_bagel-bnb-4bit"
|
| 11 |
+
tokenizer_path = "./"
|
| 12 |
+
|
| 13 |
+
DESCRIPTION = """
|
| 14 |
+
# thesven/Llama3-8B-SFT-code_bagel-bnb-4bit
|
| 15 |
+
"""
|
| 16 |
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, device_map="auto", trust_remote_code=True)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype=torch.bfloat16, trust_remote_code=True)
|
| 19 |
|
| 20 |
+
def format_prompt(user_message, system_message="You are an expert developer in all programming languages. Help me with my code. Answer any questions I have with code examples."):
|
| 21 |
+
prompt = f"<|im_start|>assistant\n{system_message}<|im_end|>\n<|im_start|>\nuser\n{user_message}<|im_end|>\nassistant\n"
|
| 22 |
+
return prompt
|
| 23 |
|
| 24 |
@spaces.GPU
|
| 25 |
+
def predict(message, system_message, max_new_tokens=600, temperature=3.5, top_p=0.9, top_k=40, do_sample=False):
|
| 26 |
+
formatted_prompt = format_prompt(message, system_message)
|
|
|
|
| 27 |
|
| 28 |
+
input_ids = tokenizer.encode(formatted_prompt, return_tensors='pt')
|
| 29 |
+
input_ids = input_ids.to(model.device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
response_ids = model.generate(
|
| 32 |
+
input_ids,
|
| 33 |
+
max_length=max_new_tokens + input_ids.shape[1],
|
| 34 |
+
temperature=temperature,
|
| 35 |
+
top_p=top_p,
|
| 36 |
+
top_k=top_k,
|
| 37 |
+
no_repeat_ngram_size=9,
|
| 38 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 39 |
+
do_sample=do_sample
|
| 40 |
)
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
response = tokenizer.decode(response_ids[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 43 |
+
truncate_str = "<|im_end|>"
|
| 44 |
+
if truncate_str and truncate_str in response:
|
| 45 |
+
response = response.split(truncate_str)[0]
|
| 46 |
|
| 47 |
+
return [("bot", response)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown(DESCRIPTION)
|
| 51 |
+
with gr.Group():
|
| 52 |
+
system_prompt = gr.Textbox(placeholder='Provide a System Prompt In The First Person', label='System Prompt', lines=2, value="ou are an expert developer in all programming languages. Help me with my code. Answer any questions I have with code examples.")
|
| 53 |
|
| 54 |
+
with gr.Group():
|
| 55 |
+
chatbot = gr.Chatbot(label='thesven/Llama3-8B-SFT-code_bagel-bnb-4bit')
|
|
|
|
| 56 |
|
| 57 |
+
with gr.Group():
|
| 58 |
+
textbox = gr.Textbox(placeholder='Your Message Here', label='Your Message', lines=2)
|
| 59 |
+
submit_button = gr.Button('Submit', variant='primary')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
with gr.Accordion(label='Advanced options', open=False):
|
| 62 |
+
max_new_tokens = gr.Slider(label='Max New Tokens', minimum=1, maximum=55000, step=1, value=4056)
|
| 63 |
+
temperature = gr.Slider(label='Temperature', minimum=0.1, maximum=4.0, step=0.1, value=1.2)
|
| 64 |
+
top_p = gr.Slider(label='Top-P (nucleus sampling)', minimum=0.05, maximum=1.0, step=0.05, value=0.9)
|
| 65 |
+
top_k = gr.Slider(label='Top-K', minimum=1, maximum=1000, step=1, value=40)
|
| 66 |
+
do_sample_checkbox = gr.Checkbox(label='Disable for faster inference', value=True)
|
| 67 |
+
|
| 68 |
+
submit_button.click(
|
| 69 |
+
fn=predict,
|
| 70 |
+
inputs=[textbox, system_prompt, max_new_tokens, temperature, top_p, top_k, do_sample_checkbox],
|
| 71 |
+
outputs=chatbot
|
| 72 |
+
)
|
| 73 |
|
| 74 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
accelerate
|
| 2 |
bitsandbytes
|
| 3 |
transformers
|
| 4 |
-
spaces
|
|
|
|
|
|
| 1 |
accelerate
|
| 2 |
bitsandbytes
|
| 3 |
transformers
|
| 4 |
+
spaces
|
| 5 |
+
sentencepiece
|