Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +117 -105
- requirements.txt +3 -3
app.py
CHANGED
|
@@ -8,132 +8,144 @@ import spaces
|
|
| 8 |
import torch
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 10 |
|
| 11 |
-
MAX_MAX_NEW_TOKENS =
|
| 12 |
-
DEFAULT_MAX_NEW_TOKENS =
|
| 13 |
-
total_count=0
|
| 14 |
-
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "
|
| 15 |
|
| 16 |
DESCRIPTION = """\
|
| 17 |
# DeepSeek-R1-Chat
|
| 18 |
|
| 19 |
-
This space demonstrates model [DeepSeek-
|
| 20 |
|
| 21 |
**You can also try our R1 model in [official homepage](https://r1.deepseek.com/chat).**
|
| 22 |
"""
|
| 23 |
|
| 24 |
if not torch.cuda.is_available():
|
| 25 |
-
|
| 26 |
|
| 27 |
|
| 28 |
if torch.cuda.is_available():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
|
| 35 |
|
| 36 |
@spaces.GPU
|
| 37 |
def generate(
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
) -> Iterator[str]:
|
| 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 |
chat_interface = gr.ChatInterface(
|
| 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 |
-
with gr.Blocks(css="style.css") as demo:
|
| 135 |
-
|
| 136 |
-
|
| 137 |
|
| 138 |
if __name__ == "__main__":
|
| 139 |
-
|
|
|
|
| 8 |
import torch
|
| 9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 10 |
|
| 11 |
+
MAX_MAX_NEW_TOKENS = 2048
|
| 12 |
+
DEFAULT_MAX_NEW_TOKENS = 2048
|
| 13 |
+
total_count = 0
|
| 14 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "128000"))
|
| 15 |
|
| 16 |
DESCRIPTION = """\
|
| 17 |
# DeepSeek-R1-Chat
|
| 18 |
|
| 19 |
+
This space demonstrates model [DeepSeek-Coder](https://huggingface.co/deepseek-ai/deepseek-coder-r1) by DeepSeek, a code model with 6XXB parameters fine-tuned for chat instructions.
|
| 20 |
|
| 21 |
**You can also try our R1 model in [official homepage](https://r1.deepseek.com/chat).**
|
| 22 |
"""
|
| 23 |
|
| 24 |
if not torch.cuda.is_available():
|
| 25 |
+
DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
|
| 26 |
|
| 27 |
|
| 28 |
if torch.cuda.is_available():
|
| 29 |
+
model_id = "deepseek-ai/deepseek-r1"
|
| 30 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype = torch.bfloat16, device_map = "auto")
|
| 31 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 32 |
+
tokenizer.use_default_system_prompt = False
|
| 33 |
+
|
| 34 |
|
| 35 |
|
| 36 |
@spaces.GPU
|
| 37 |
def generate(
|
| 38 |
+
message: str,
|
| 39 |
+
chat_history: list[tuple[str, str]],
|
| 40 |
+
system_prompt: str,
|
| 41 |
+
max_new_tokens: int = 2048,
|
| 42 |
+
temperature: float = 0,
|
| 43 |
+
top_p: float = 0,
|
| 44 |
+
top_k: int = 50,
|
| 45 |
+
repetition_penalty: float = 2,
|
| 46 |
) -> Iterator[str]:
|
| 47 |
+
global total_count
|
| 48 |
+
total_count += 1
|
| 49 |
+
print(total_count)
|
| 50 |
+
os.system("nvidia-smi")
|
| 51 |
+
conversation = []
|
| 52 |
+
if system_prompt:
|
| 53 |
+
conversation.append({
|
| 54 |
+
"role": "system", "content": system_prompt
|
| 55 |
+
})
|
| 56 |
+
for user, assistant in chat_history:
|
| 57 |
+
conversation.extend([{
|
| 58 |
+
"role": "user", "content": user
|
| 59 |
+
}, {
|
| 60 |
+
"role": "assistant", "content": assistant
|
| 61 |
+
}])
|
| 62 |
+
conversation.append({
|
| 63 |
+
"role": "user", "content": message
|
| 64 |
+
})
|
| 65 |
+
|
| 66 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors = "pt")
|
| 67 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 68 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 69 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {
|
| 70 |
+
MAX_INPUT_TOKEN_LENGTH
|
| 71 |
+
} tokens.")
|
| 72 |
+
input_ids = input_ids.to(model.device)
|
| 73 |
+
|
| 74 |
+
streamer = TextIteratorStreamer(tokenizer, timeout = 10.0, skip_prompt = True, skip_special_tokens = True)
|
| 75 |
+
generate_kwargs = dict(
|
| 76 |
+
{
|
| 77 |
+
"input_ids": input_ids
|
| 78 |
+
},
|
| 79 |
+
streamer = streamer,
|
| 80 |
+
max_new_tokens = max_new_tokens,
|
| 81 |
+
do_sample = False,
|
| 82 |
+
top_p = top_p,
|
| 83 |
+
top_k = top_k,
|
| 84 |
+
num_beams = 1,
|
| 85 |
+
# temperature=temperature,
|
| 86 |
+
repetition_penalty = repetition_penalty,
|
| 87 |
+
eos_token_id = 32021
|
| 88 |
+
)
|
| 89 |
+
t = Thread(target = model.generate, kwargs = generate_kwargs)
|
| 90 |
+
t.start()
|
| 91 |
+
|
| 92 |
+
outputs = []
|
| 93 |
+
for text in streamer:
|
| 94 |
+
outputs.append(text)
|
| 95 |
+
yield "".join(outputs).replace("<|EOT|>","")
|
| 96 |
|
| 97 |
|
| 98 |
chat_interface = gr.ChatInterface(
|
| 99 |
+
fn = generate,
|
| 100 |
+
additional_inputs = [
|
| 101 |
+
gr.Textbox(label = "System prompt", lines = 6),
|
| 102 |
+
gr.Slider(
|
| 103 |
+
label = "Max new tokens",
|
| 104 |
+
minimum = 1,
|
| 105 |
+
maximum = MAX_MAX_NEW_TOKENS,
|
| 106 |
+
step = 1,
|
| 107 |
+
value = DEFAULT_MAX_NEW_TOKENS,
|
| 108 |
+
),
|
| 109 |
+
gr.Slider(
|
| 110 |
+
label="Temperature",
|
| 111 |
+
minimum=0,
|
| 112 |
+
maximum=4.0,
|
| 113 |
+
step=0.01,
|
| 114 |
+
value=0,
|
| 115 |
+
),
|
| 116 |
+
gr.Slider(
|
| 117 |
+
label = "Top-p (nucleus sampling)",
|
| 118 |
+
minimum = 0,
|
| 119 |
+
maximum = 4.0,
|
| 120 |
+
step = 0.01,
|
| 121 |
+
value = 0,
|
| 122 |
+
),
|
| 123 |
+
gr.Slider(
|
| 124 |
+
label = "Top-k",
|
| 125 |
+
minimum = 1,
|
| 126 |
+
maximum = 1000,
|
| 127 |
+
step = 0.01,
|
| 128 |
+
value = 50,
|
| 129 |
+
),
|
| 130 |
+
gr.Slider(
|
| 131 |
+
label = "Repetition penalty",
|
| 132 |
+
minimum = 0,
|
| 133 |
+
maximum = 2.0,
|
| 134 |
+
step = 0.01,
|
| 135 |
+
value = 2,
|
| 136 |
+
),
|
| 137 |
+
],
|
| 138 |
+
stop_btn = gr.Button("Stop"),
|
| 139 |
+
examples = [
|
| 140 |
+
["implement snake game using pygame"],
|
| 141 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
| 142 |
+
["write a program to find the factorial of a number"],
|
| 143 |
+
],
|
| 144 |
)
|
| 145 |
|
| 146 |
+
with gr.Blocks(css = "style.css") as demo:
|
| 147 |
+
gr.Markdown(DESCRIPTION)
|
| 148 |
+
chat_interface.render()
|
| 149 |
|
| 150 |
if __name__ == "__main__":
|
| 151 |
+
demo.queue(max_size = 20).launch()
|
requirements.txt
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
https://github.com/huggingface/accelerate/archive/main.tar.gz
|
| 2 |
https://github.com/bitsandbytes-foundation/bitsandbytes/archive/main.tar.gz
|
| 3 |
https://github.com/gradio-app/gradio/archive/main.tar.gz
|
| 4 |
-
|
| 5 |
-
https://
|
| 6 |
git+https://github.com/google/sentencepiece@master#sub_directory=python
|
| 7 |
https://github.com/huggingface/huggingface_hub/archive/main.tar.gz
|
| 8 |
-
https://github.com/pytorch/pytorch
|
| 9 |
https://github.com/huggingface/transformers/archive/main.tar.gz
|
|
|
|
| 1 |
https://github.com/huggingface/accelerate/archive/main.tar.gz
|
| 2 |
https://github.com/bitsandbytes-foundation/bitsandbytes/archive/main.tar.gz
|
| 3 |
https://github.com/gradio-app/gradio/archive/main.tar.gz
|
| 4 |
+
protobuf
|
| 5 |
+
https://github.com/scipy/scipy/archive/main.tar.gz
|
| 6 |
git+https://github.com/google/sentencepiece@master#sub_directory=python
|
| 7 |
https://github.com/huggingface/huggingface_hub/archive/main.tar.gz
|
| 8 |
+
https://github.com/pytorch/pytorch
|
| 9 |
https://github.com/huggingface/transformers/archive/main.tar.gz
|