Spaces:
Runtime error
Runtime error
Set up the working demo for serving a gemma-2 checkpoint.
Browse files- .pre-commit-config.yaml +60 -0
- app.py +121 -40
- requirements.txt +6 -1
- style.css +11 -0
.pre-commit-config.yaml
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
repos:
|
| 2 |
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
| 3 |
+
rev: v4.6.0
|
| 4 |
+
hooks:
|
| 5 |
+
- id: check-executables-have-shebangs
|
| 6 |
+
- id: check-json
|
| 7 |
+
- id: check-merge-conflict
|
| 8 |
+
- id: check-shebang-scripts-are-executable
|
| 9 |
+
- id: check-toml
|
| 10 |
+
- id: check-yaml
|
| 11 |
+
- id: end-of-file-fixer
|
| 12 |
+
- id: mixed-line-ending
|
| 13 |
+
args: ["--fix=lf"]
|
| 14 |
+
- id: requirements-txt-fixer
|
| 15 |
+
- id: trailing-whitespace
|
| 16 |
+
- repo: https://github.com/myint/docformatter
|
| 17 |
+
rev: v1.7.5
|
| 18 |
+
hooks:
|
| 19 |
+
- id: docformatter
|
| 20 |
+
args: ["--in-place"]
|
| 21 |
+
- repo: https://github.com/pycqa/isort
|
| 22 |
+
rev: 5.13.2
|
| 23 |
+
hooks:
|
| 24 |
+
- id: isort
|
| 25 |
+
args: ["--profile", "black"]
|
| 26 |
+
- repo: https://github.com/pre-commit/mirrors-mypy
|
| 27 |
+
rev: v1.10.1
|
| 28 |
+
hooks:
|
| 29 |
+
- id: mypy
|
| 30 |
+
args: ["--ignore-missing-imports"]
|
| 31 |
+
additional_dependencies:
|
| 32 |
+
[
|
| 33 |
+
"types-python-slugify",
|
| 34 |
+
"types-requests",
|
| 35 |
+
"types-PyYAML",
|
| 36 |
+
"types-pytz",
|
| 37 |
+
]
|
| 38 |
+
- repo: https://github.com/psf/black
|
| 39 |
+
rev: 24.4.2
|
| 40 |
+
hooks:
|
| 41 |
+
- id: black
|
| 42 |
+
language_version: python3.10
|
| 43 |
+
args: ["--line-length", "119"]
|
| 44 |
+
- repo: https://github.com/kynan/nbstripout
|
| 45 |
+
rev: 0.7.1
|
| 46 |
+
hooks:
|
| 47 |
+
- id: nbstripout
|
| 48 |
+
args:
|
| 49 |
+
[
|
| 50 |
+
"--extra-keys",
|
| 51 |
+
"metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
|
| 52 |
+
]
|
| 53 |
+
- repo: https://github.com/nbQA-dev/nbQA
|
| 54 |
+
rev: 1.8.5
|
| 55 |
+
hooks:
|
| 56 |
+
- id: nbqa-black
|
| 57 |
+
- id: nbqa-pyupgrade
|
| 58 |
+
args: ["--py37-plus"]
|
| 59 |
+
- id: nbqa-isort
|
| 60 |
+
args: ["--float-to-top"]
|
app.py
CHANGED
|
@@ -1,63 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("Unispac/Gemma-2-9B-IT-With-Deeper-Safety-Alignment")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
for val in history:
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
demo = gr.ChatInterface(
|
| 46 |
-
respond,
|
| 47 |
additional_inputs=[
|
| 48 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 49 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 50 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 51 |
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
minimum=0.1,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
maximum=1.0,
|
| 54 |
-
value=0.95,
|
| 55 |
step=0.05,
|
| 56 |
-
|
| 57 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
],
|
| 59 |
)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from threading import Thread
|
| 3 |
+
from typing import Iterator
|
| 4 |
+
|
| 5 |
import gradio as gr
|
| 6 |
+
import spaces
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import (
|
| 9 |
+
AutoModelForCausalLM,
|
| 10 |
+
BitsAndBytesConfig,
|
| 11 |
+
GemmaTokenizerFast,
|
| 12 |
+
TextIteratorStreamer,
|
| 13 |
+
)
|
| 14 |
|
| 15 |
+
DESCRIPTION = """\
|
| 16 |
+
# Gemma 2 9B IT
|
| 17 |
+
|
| 18 |
+
Gemma 2 is Google's latest iteration of open LLMs.
|
| 19 |
+
This is a demo of [`google/gemma-2-9b-it`](https://huggingface.co/google/gemma-2-9b-it), fine-tuned for instruction following.
|
| 20 |
+
For more details, please check [our post](https://huggingface.co/blog/gemma2).
|
| 21 |
+
|
| 22 |
+
π Looking for a larger and more powerful version? Try the 27B version in [HuggingChat](https://huggingface.co/chat/models/google/gemma-2-27b-it).
|
| 23 |
"""
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
MAX_MAX_NEW_TOKENS = 2048
|
| 26 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
| 27 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
| 28 |
+
|
| 29 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
| 30 |
|
| 31 |
+
model_id = "Unispac/Gemma-2-9B-IT-With-Deeper-Safety-Alignment"
|
| 32 |
+
tokenizer = GemmaTokenizerFast.from_pretrained(model_id)
|
| 33 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 34 |
+
model_id,
|
| 35 |
+
device_map="auto",
|
| 36 |
+
quantization_config=BitsAndBytesConfig(load_in_8bit=True),
|
| 37 |
+
)
|
| 38 |
+
model.config.sliding_window = 4096
|
| 39 |
+
model.eval()
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
@spaces.GPU(duration=90)
|
| 43 |
+
def generate(
|
| 44 |
+
message: str,
|
| 45 |
+
chat_history: list[tuple[str, str]],
|
| 46 |
+
max_new_tokens: int = 1024,
|
| 47 |
+
temperature: float = 0.6,
|
| 48 |
+
top_p: float = 0.9,
|
| 49 |
+
top_k: int = 50,
|
| 50 |
+
repetition_penalty: float = 1.2,
|
| 51 |
+
) -> Iterator[str]:
|
| 52 |
+
conversation = []
|
| 53 |
+
for user, assistant in chat_history:
|
| 54 |
+
conversation.extend(
|
| 55 |
+
[
|
| 56 |
+
{"role": "user", "content": user},
|
| 57 |
+
{"role": "assistant", "content": assistant},
|
| 58 |
+
]
|
| 59 |
+
)
|
| 60 |
+
conversation.append({"role": "user", "content": message})
|
| 61 |
|
| 62 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
| 63 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
| 64 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
| 65 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
| 66 |
+
input_ids = input_ids.to(model.device)
|
| 67 |
|
| 68 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
| 69 |
+
generate_kwargs = dict(
|
| 70 |
+
{"input_ids": input_ids},
|
| 71 |
+
streamer=streamer,
|
| 72 |
+
max_new_tokens=max_new_tokens,
|
| 73 |
+
do_sample=True,
|
| 74 |
top_p=top_p,
|
| 75 |
+
top_k=top_k,
|
| 76 |
+
temperature=temperature,
|
| 77 |
+
num_beams=1,
|
| 78 |
+
repetition_penalty=repetition_penalty,
|
| 79 |
+
)
|
| 80 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 81 |
+
t.start()
|
| 82 |
|
| 83 |
+
outputs = []
|
| 84 |
+
for text in streamer:
|
| 85 |
+
outputs.append(text)
|
| 86 |
+
yield "".join(outputs)
|
| 87 |
|
| 88 |
+
|
| 89 |
+
chat_interface = gr.ChatInterface(
|
| 90 |
+
fn=generate,
|
|
|
|
|
|
|
| 91 |
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
| 92 |
gr.Slider(
|
| 93 |
+
label="Max new tokens",
|
| 94 |
+
minimum=1,
|
| 95 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
| 96 |
+
step=1,
|
| 97 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
| 98 |
+
),
|
| 99 |
+
gr.Slider(
|
| 100 |
+
label="Temperature",
|
| 101 |
minimum=0.1,
|
| 102 |
+
maximum=4.0,
|
| 103 |
+
step=0.1,
|
| 104 |
+
value=0.6,
|
| 105 |
+
),
|
| 106 |
+
gr.Slider(
|
| 107 |
+
label="Top-p (nucleus sampling)",
|
| 108 |
+
minimum=0.05,
|
| 109 |
maximum=1.0,
|
|
|
|
| 110 |
step=0.05,
|
| 111 |
+
value=0.9,
|
| 112 |
),
|
| 113 |
+
gr.Slider(
|
| 114 |
+
label="Top-k",
|
| 115 |
+
minimum=1,
|
| 116 |
+
maximum=1000,
|
| 117 |
+
step=1,
|
| 118 |
+
value=50,
|
| 119 |
+
),
|
| 120 |
+
gr.Slider(
|
| 121 |
+
label="Repetition penalty",
|
| 122 |
+
minimum=1.0,
|
| 123 |
+
maximum=2.0,
|
| 124 |
+
step=0.05,
|
| 125 |
+
value=1.2,
|
| 126 |
+
),
|
| 127 |
+
],
|
| 128 |
+
stop_btn=None,
|
| 129 |
+
examples=[
|
| 130 |
+
["Hello there! How are you doing?"],
|
| 131 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
| 132 |
+
["Explain the plot of Cinderella in a sentence."],
|
| 133 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
| 134 |
+
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
| 135 |
],
|
| 136 |
)
|
| 137 |
|
| 138 |
+
with gr.Blocks(css="style.css", fill_height=True) as demo:
|
| 139 |
+
gr.Markdown(DESCRIPTION)
|
| 140 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
| 141 |
+
chat_interface.render()
|
| 142 |
|
| 143 |
if __name__ == "__main__":
|
| 144 |
+
demo.queue(max_size=20).launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,6 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
accelerate==0.31.0
|
| 2 |
+
bitsandbytes==0.43.1
|
| 3 |
+
gradio==4.37.1
|
| 4 |
+
spaces==0.28.3
|
| 5 |
+
torch==2.2.0
|
| 6 |
+
transformers==4.42.1
|
style.css
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
h1 {
|
| 2 |
+
text-align: center;
|
| 3 |
+
display: block;
|
| 4 |
+
}
|
| 5 |
+
|
| 6 |
+
#duplicate-button {
|
| 7 |
+
margin: auto;
|
| 8 |
+
color: #fff;
|
| 9 |
+
background: #1565c0;
|
| 10 |
+
border-radius: 100vh;
|
| 11 |
+
}
|