hayas commited on
Commit
9b79ef5
1 Parent(s): 2cfbf68
Files changed (6) hide show
  1. .pre-commit-config.yaml +55 -0
  2. .vscode/settings.json +26 -0
  3. README.md +1 -1
  4. app.py +136 -0
  5. requirements.txt +12 -0
  6. style.css +11 -0
.pre-commit-config.yaml ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ repos:
2
+ - repo: https://github.com/pre-commit/pre-commit-hooks
3
+ rev: v4.5.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.7.1
28
+ hooks:
29
+ - id: mypy
30
+ args: ["--ignore-missing-imports"]
31
+ additional_dependencies:
32
+ ["types-python-slugify", "types-requests", "types-PyYAML"]
33
+ - repo: https://github.com/psf/black
34
+ rev: 23.12.0
35
+ hooks:
36
+ - id: black
37
+ language_version: python3.10
38
+ args: ["--line-length", "119"]
39
+ - repo: https://github.com/kynan/nbstripout
40
+ rev: 0.6.1
41
+ hooks:
42
+ - id: nbstripout
43
+ args:
44
+ [
45
+ "--extra-keys",
46
+ "metadata.interpreter metadata.kernelspec cell.metadata.pycharm",
47
+ ]
48
+ - repo: https://github.com/nbQA-dev/nbQA
49
+ rev: 1.7.1
50
+ hooks:
51
+ - id: nbqa-black
52
+ - id: nbqa-pyupgrade
53
+ args: ["--py37-plus"]
54
+ - id: nbqa-isort
55
+ args: ["--float-to-top"]
.vscode/settings.json ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "editor.formatOnSave": true,
3
+ "files.insertFinalNewline": false,
4
+ "[python]": {
5
+ "editor.defaultFormatter": "ms-python.black-formatter",
6
+ "editor.formatOnType": true,
7
+ "editor.codeActionsOnSave": {
8
+ "source.organizeImports": "explicit"
9
+ }
10
+ },
11
+ "[jupyter]": {
12
+ "files.insertFinalNewline": false
13
+ },
14
+ "black-formatter.args": [
15
+ "--line-length=119"
16
+ ],
17
+ "isort.args": ["--profile", "black"],
18
+ "flake8.args": [
19
+ "--max-line-length=119"
20
+ ],
21
+ "ruff.lint.args": [
22
+ "--line-length=119"
23
+ ],
24
+ "notebook.output.scrolling": true,
25
+ "notebook.formatOnCellExecution": true
26
+ }
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: Nekomata 14b Instruction
3
  emoji: 👀
4
  colorFrom: purple
5
  colorTo: indigo
 
1
  ---
2
+ title: Nekomata-14B Instruction
3
  emoji: 👀
4
  colorFrom: purple
5
  colorTo: indigo
app.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ import os
4
+ from threading import Thread
5
+ from typing import Iterator
6
+
7
+ import gradio as gr
8
+ import spaces
9
+ import torch
10
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
11
+
12
+ DESCRIPTION = """# Nekomata-14B Instruction"""
13
+
14
+ if not torch.cuda.is_available():
15
+ DESCRIPTION += "\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>"
16
+
17
+ if torch.cuda.is_available():
18
+ model_id = "rinna/nekomata-14b-instruction"
19
+
20
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
21
+ model = AutoModelForCausalLM.from_pretrained(
22
+ model_id, device_map="auto", trust_remote_code=True, load_in_8bit=True, low_cpu_mem_usage=True
23
+ )
24
+
25
+ MAX_INPUT_TOKENS = 2048
26
+
27
+ PROMPT_TEMPLATE = """
28
+ 以下は、タスクを説明する指示と、文脈のある入力の組み合わせです。要求を適切に満たす応答を書きなさい。
29
+
30
+ ### 指示:
31
+ {instruction}
32
+
33
+ ### 入力:
34
+ {input}
35
+
36
+ ### 応答:
37
+ """
38
+
39
+
40
+ def create_prompt(instruction: str, input_text: str, prompt_template: str = PROMPT_TEMPLATE) -> str:
41
+ return prompt_template.format(instruction=instruction, input=input_text)
42
+
43
+
44
+ @spaces.GPU
45
+ @torch.inference_mode()
46
+ def run(
47
+ instruction: str,
48
+ input_text: str,
49
+ prompt_template: str = PROMPT_TEMPLATE,
50
+ max_new_tokens: int = 256,
51
+ temperature: float = 0.5,
52
+ top_p: float = 0.95,
53
+ repetition_penalty: float = 1.0,
54
+ ) -> Iterator[str]:
55
+ prompt = create_prompt(instruction, input_text, prompt_template)
56
+ input_ids = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
57
+ if input_ids.shape[-1] > MAX_INPUT_TOKENS:
58
+ raise gr.Error(f"Input exceeds maximum number of tokens ({MAX_INPUT_TOKENS})")
59
+
60
+ streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
61
+ generate_kwargs = dict(
62
+ {"input_ids": input_ids.to(model.device)},
63
+ streamer=streamer,
64
+ max_new_tokens=max_new_tokens,
65
+ temperature=temperature,
66
+ top_p=top_p,
67
+ repetition_penalty=repetition_penalty,
68
+ do_sample=True,
69
+ pad_token_id=tokenizer.pad_token_id,
70
+ bos_token_id=tokenizer.bos_token_id,
71
+ eos_token_id=tokenizer.eos_token_id,
72
+ )
73
+ t = Thread(target=model.generate, kwargs=generate_kwargs)
74
+ t.start()
75
+
76
+ outputs = []
77
+ for text in streamer:
78
+ outputs.append(text)
79
+ yield "".join(outputs)
80
+
81
+
82
+ def process_example(instruction: str, input_text: str) -> Iterator[str]:
83
+ yield from run(instruction, input_text)
84
+
85
+
86
+ with gr.Blocks(css="style.css") as demo:
87
+ gr.Markdown(DESCRIPTION)
88
+ gr.DuplicateButton(
89
+ value="Duplicate Space for private use",
90
+ elem_id="duplicate-button",
91
+ visible=os.getenv("SHOW_DUPLICATE_BUTTON") == "1",
92
+ )
93
+
94
+ with gr.Row():
95
+ with gr.Column():
96
+ instruction = gr.Textbox(label="Instruction", lines=5)
97
+ input_text = gr.Textbox(label="Input", lines=5)
98
+ run_button = gr.Button()
99
+
100
+ with gr.Accordion(label="Advanced Options", open=False):
101
+ prompt_template = gr.Textbox(label="Prompt Template", lines=10, value=PROMPT_TEMPLATE)
102
+ max_new_tokens = gr.Slider(label="Max New Tokens", minimum=1, maximum=1024, step=1, value=256)
103
+ temperature = gr.Slider(label="Temperature", minimum=0.0, maximum=2.0, step=0.01, value=0.5)
104
+ top_p = gr.Slider(label="Top P", minimum=0.0, maximum=1.0, step=0.01, value=0.95)
105
+ repetition_penalty = gr.Slider(
106
+ label="Repetition Penalty", minimum=0.0, maximum=2.0, step=0.01, value=1.0
107
+ )
108
+
109
+ with gr.Column():
110
+ output = gr.Textbox(label="Output", lines=10)
111
+
112
+ run_button.click(
113
+ fn=run,
114
+ inputs=[instruction, input_text, prompt_template, max_new_tokens, temperature, top_p, repetition_penalty],
115
+ outputs=output,
116
+ api_name="run",
117
+ )
118
+
119
+ gr.Examples(
120
+ examples=[
121
+ [
122
+ "次の日本語を英語に翻訳してください。",
123
+ "大規模言語モデル(だいきぼげんごモデル、英: large language model、LLM)は、多数のパラメータ(数千万から数十億)を持つ人工ニューラルネットワークで構成されるコンピュータ言語モデルで、膨大なラベルなしテキストを使用して自己教師あり学習または半教師あり学習によって訓練が行われる。",
124
+ ],
125
+ ["以下のトピックに関する詳細な情報を提供してください。", "夢オチとは何かについて教えてください。"],
126
+ ["以下のトピックに関する詳細な情報を提供してください。", "暴れん坊将軍について教えてください。"],
127
+ ],
128
+ inputs=[instruction, input_text],
129
+ outputs=output,
130
+ fn=process_example,
131
+ cache_examples=os.getenv("CACHE_EXAMPLES") == "1",
132
+ api_name=False,
133
+ )
134
+
135
+ if __name__ == "__main__":
136
+ demo.queue(max_size=20).launch()
requirements.txt ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ accelerate==0.25.0
2
+ bitsandbytes==0.41.2.post2
3
+ einops==0.6.1
4
+ gradio==4.11.0
5
+ protobuf==4.25.1
6
+ scipy==1.11.4
7
+ sentencepiece==0.1.99
8
+ spaces==0.19.2
9
+ tiktoken==0.5.2
10
+ torch==2.0.0
11
+ transformers==4.36.2
12
+ transformers-stream-generator==0.0.4
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
+ }