Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,8 @@
|
|
| 1 |
-
import subprocess
|
| 2 |
-
subprocess.run(
|
| 3 |
-
'pip install flash-attn --no-build-isolation',
|
| 4 |
-
env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"},
|
| 5 |
-
shell=True
|
| 6 |
-
)
|
| 7 |
-
|
| 8 |
import os
|
| 9 |
import time
|
| 10 |
import spaces
|
| 11 |
import torch
|
| 12 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer,
|
| 13 |
import gradio as gr
|
| 14 |
from threading import Thread
|
| 15 |
|
|
@@ -24,7 +17,6 @@ PLACEHOLDER = """
|
|
| 24 |
</center>
|
| 25 |
"""
|
| 26 |
|
| 27 |
-
|
| 28 |
CSS = """
|
| 29 |
.duplicate-button {
|
| 30 |
margin: auto !important;
|
|
@@ -37,27 +29,20 @@ h3 {
|
|
| 37 |
}
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
device = "
|
| 41 |
-
|
| 42 |
-
quantization_config = BitsAndBytesConfig(
|
| 43 |
-
load_in_4bit=True,
|
| 44 |
-
bnb_4bit_compute_dtype=torch.bfloat16,
|
| 45 |
-
bnb_4bit_use_double_quant=True,
|
| 46 |
-
bnb_4bit_quant_type= "nf4")
|
| 47 |
|
| 48 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 49 |
model = AutoModelForCausalLM.from_pretrained(
|
| 50 |
MODEL,
|
| 51 |
-
torch_dtype=torch.
|
| 52 |
device_map="auto",
|
| 53 |
-
|
| 54 |
-
quantization_config=quantization_config)
|
| 55 |
|
| 56 |
# Ensure `pad_token_id` is set
|
| 57 |
if tokenizer.pad_token_id is None:
|
| 58 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 59 |
|
| 60 |
-
@spaces.
|
| 61 |
def stream_chat(
|
| 62 |
message: str,
|
| 63 |
history: list,
|
|
@@ -82,19 +67,19 @@ def stream_chat(
|
|
| 82 |
|
| 83 |
conversation.append({"role": "user", "content": message})
|
| 84 |
|
| 85 |
-
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(
|
| 86 |
|
| 87 |
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 88 |
|
| 89 |
generate_kwargs = dict(
|
| 90 |
input_ids=input_ids,
|
| 91 |
-
max_new_tokens
|
| 92 |
-
do_sample
|
| 93 |
-
top_p
|
| 94 |
-
top_k
|
| 95 |
-
eos_token_id
|
| 96 |
-
pad_token_id
|
| 97 |
-
temperature
|
| 98 |
repetition_penalty=penalty,
|
| 99 |
streamer=streamer,
|
| 100 |
)
|
|
@@ -107,7 +92,6 @@ def stream_chat(
|
|
| 107 |
for new_text in streamer:
|
| 108 |
buffer += new_text
|
| 109 |
yield buffer
|
| 110 |
-
|
| 111 |
|
| 112 |
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
|
| 113 |
|
|
@@ -138,7 +122,7 @@ with gr.Blocks(css=CSS, theme="soft") as demo:
|
|
| 138 |
minimum=128,
|
| 139 |
maximum=8192,
|
| 140 |
step=1,
|
| 141 |
-
value=
|
| 142 |
label="Max new tokens",
|
| 143 |
render=False,
|
| 144 |
),
|
|
@@ -176,6 +160,5 @@ with gr.Blocks(css=CSS, theme="soft") as demo:
|
|
| 176 |
cache_examples=False,
|
| 177 |
)
|
| 178 |
|
| 179 |
-
|
| 180 |
if __name__ == "__main__":
|
| 181 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import time
|
| 3 |
import spaces
|
| 4 |
import torch
|
| 5 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
import gradio as gr
|
| 7 |
from threading import Thread
|
| 8 |
|
|
|
|
| 17 |
</center>
|
| 18 |
"""
|
| 19 |
|
|
|
|
| 20 |
CSS = """
|
| 21 |
.duplicate-button {
|
| 22 |
margin: auto !important;
|
|
|
|
| 29 |
}
|
| 30 |
"""
|
| 31 |
|
| 32 |
+
device = "cpu" # Changed to CPU
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
| 35 |
model = AutoModelForCausalLM.from_pretrained(
|
| 36 |
MODEL,
|
| 37 |
+
torch_dtype=torch.float32, # Changed to float32 for better CPU compatibility
|
| 38 |
device_map="auto",
|
| 39 |
+
).to(device) # Explicitly move to CPU
|
|
|
|
| 40 |
|
| 41 |
# Ensure `pad_token_id` is set
|
| 42 |
if tokenizer.pad_token_id is None:
|
| 43 |
tokenizer.pad_token_id = tokenizer.eos_token_id
|
| 44 |
|
| 45 |
+
@spaces.CPU() # Changed to CPU decorator
|
| 46 |
def stream_chat(
|
| 47 |
message: str,
|
| 48 |
history: list,
|
|
|
|
| 67 |
|
| 68 |
conversation.append({"role": "user", "content": message})
|
| 69 |
|
| 70 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(device)
|
| 71 |
|
| 72 |
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 73 |
|
| 74 |
generate_kwargs = dict(
|
| 75 |
input_ids=input_ids,
|
| 76 |
+
max_new_tokens=max_new_tokens,
|
| 77 |
+
do_sample=False if temperature == 0 else True,
|
| 78 |
+
top_p=top_p,
|
| 79 |
+
top_k=top_k,
|
| 80 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 81 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 82 |
+
temperature=temperature,
|
| 83 |
repetition_penalty=penalty,
|
| 84 |
streamer=streamer,
|
| 85 |
)
|
|
|
|
| 92 |
for new_text in streamer:
|
| 93 |
buffer += new_text
|
| 94 |
yield buffer
|
|
|
|
| 95 |
|
| 96 |
chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
|
| 97 |
|
|
|
|
| 122 |
minimum=128,
|
| 123 |
maximum=8192,
|
| 124 |
step=1,
|
| 125 |
+
value=2500,
|
| 126 |
label="Max new tokens",
|
| 127 |
render=False,
|
| 128 |
),
|
|
|
|
| 160 |
cache_examples=False,
|
| 161 |
)
|
| 162 |
|
|
|
|
| 163 |
if __name__ == "__main__":
|
| 164 |
demo.launch()
|