weblab-geniac-spaces commited on
Commit
0bbcccf
1 Parent(s): 952243f

first commit

Browse files
Files changed (2) hide show
  1. app.py +180 -46
  2. requirements.txt +6 -1
app.py CHANGED
@@ -1,62 +1,196 @@
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
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("HuggingFaceH4/zephyr-7b-beta")
8
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
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
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
27
 
28
- response = ""
 
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
41
 
 
 
 
 
 
42
  """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
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
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
  if __name__ == "__main__":
 
1
+ import os
2
+ import time
3
+ import spaces
4
+ from threading import Thread
5
+ import torch
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
7
  import gradio as gr
 
8
 
9
+ MODEL = "weblab-GENIAC/Tanuki-8B-dpo-v1.0"
10
+ HF_TOKEN = os.environ.get("HF_TOKEN", None)
11
+
12
+ TITLE = "<h1><center>Tanuki-8B-dpo-v1.0</center></h1>"
13
+
14
+ DESCRIPTION = """
15
+ <div class="model-description">
16
+ <p>
17
+ 🦡 <a href="https://huggingface.co/weblab-GENIAC/Tanuki-8B-dpo-v1.0"><b>Tanuki 8B</b>(weblab-GENIAC/Tanuki-8B-dpo-v1.0)</a>は、
18
+ 経産省及びNEDOが推進する日本国内の生成AI基盤モデル開発を推進する「GENIAC」プロジェクトにおいて、松尾・岩澤研究室が開発・公開したLLMとなります。
19
+ 本プロジェクトは松尾研が提供する大規模言語モデル講座(2023年9月開催、2,000名が受講)の修了生及び一般公募によって集まった有志の開発者(⺠間企業・研究者・学⽣で構成)が、最新の研究成果や技術的な知見を取り入れ、開発を行ったモデルです。
20
+ </p>
21
+ <p>🤖 このデモでは、Tanuki 8Bとチャットを行うことが可能です。(注:フルバーションの<a href="https://huggingface.co/weblab-GENIAC/Tanuki-8x8B-dpo-v1.0">Tanuki 8x8B</a>ではございません。)</p>
22
+ <p>📄 モデルの詳細については、<a href="http://weblab.t.u-tokyo.ac.jp/2024-08-30">プレスリリース</a>をご覧ください。お問い合わせは<a href="https://weblab.t.u-tokyo.ac.jp/contact/">こちら</a>までどうぞ。</p>
23
+ <p>関連サイト: <a href="https://weblab.t.u-tokyo.ac.jp/geniac_llm">GENIAC 松尾研 LLM開発プロジェクト</a></p>
24
+ </div>
25
  """
 
 
 
26
 
27
+ PLACEHOLDER = """
28
+ <div class="image-placeholder">
29
+ <img src="https://weblab.t.u-tokyo.ac.jp/wp-content/uploads/2024/06/GENIAC-image-cutting3-1.jpg" alt="Tanuki-8B Image">
30
+ <h1>Tanuki-8B</h1>
31
+ </div>
32
+ """
33
 
34
+ CSS = """
35
+ .duplicate-button {
36
+ margin: auto !important;
37
+ color: white !important;
38
+ background: black !important;
39
+ border-radius: 100vh !important;
40
+ }
 
 
41
 
42
+ h3 {
43
+ text-align: center;
44
+ }
 
 
45
 
46
+ .model-description {
47
+ padding: 0.5em 1em;
48
+ margin: 2em 0;
49
+ border-top: solid 5px #5d627b;
50
+ box-shadow: 0 1px 1px rgba(0, 0, 0, 0.22);
51
+ border-radius: 5px;
52
+ }
53
 
54
+ .model-description p {
55
+ margin: 0;
56
+ padding: 0;
57
+ color: #5d627b;
58
+ }
59
 
60
+ .image-placeholder {
61
+ text-align: center;
62
+ display: flex;
63
+ flex-direction: column;
64
+ align-items: center;
65
+ }
 
 
66
 
67
+ .image-placeholder img {
68
+ width: 100%;
69
+ height: auto;
70
+ opacity: 0.55;
71
+ }
72
 
73
+ .image-placeholder h1 {
74
+ font-size: 28px;
75
+ margin-bottom: 2px;
76
+ opacity: 0.55;
77
+ }
78
  """
79
+
80
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
81
+ model = AutoModelForCausalLM.from_pretrained(
82
+ MODEL,
83
+ torch_dtype=torch.bfloat16,
84
+ device_map="auto",
 
 
 
 
 
 
 
 
 
 
85
  )
86
+ print(model)
87
+
88
+ @spaces.GPU()
89
+ def stream_chat(
90
+ message: str,
91
+ history: list,
92
+ system_prompt: str,
93
+ temperature: float = 0.3,
94
+ max_new_tokens: int = 1024,
95
+ top_p: float = 1.0,
96
+ top_k: int = 20,
97
+ ):
98
+ print(f'message: {message}')
99
+ print(f'history: {history}')
100
+
101
+ conversation = [
102
+ {"role": "system", "content": system_prompt}
103
+ ]
104
+ for prompt, answer in history:
105
+ conversation.extend([
106
+ {"role": "user", "content": prompt},
107
+ {"role": "assistant", "content": answer},
108
+ ])
109
+
110
+ conversation.append({"role": "user", "content": message})
111
+
112
+ input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt").to(model.device)
113
+
114
+ streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
115
+
116
+ generate_kwargs = dict(
117
+ input_ids=input_ids,
118
+ max_new_tokens = max_new_tokens,
119
+ do_sample = False if temperature == 0 else True,
120
+ top_p = top_p,
121
+ top_k = top_k,
122
+ temperature = temperature,
123
+ streamer=streamer,
124
+ )
125
+
126
+ with torch.no_grad():
127
+ thread = Thread(target=model.generate, kwargs=generate_kwargs)
128
+ thread.start()
129
+
130
+ buffer = ""
131
+ for new_text in streamer:
132
+ buffer += new_text
133
+ yield buffer
134
+
135
+
136
+ chatbot = gr.Chatbot(height=600, placeholder=PLACEHOLDER)
137
+
138
+ with gr.Blocks(css=CSS, theme="soft") as demo:
139
+ gr.HTML(TITLE)
140
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_classes="duplicate-button")
141
+ gr.Markdown(DESCRIPTION)
142
+ gr.ChatInterface(
143
+ fn=stream_chat,
144
+ chatbot=chatbot,
145
+ fill_height=True,
146
+ additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
147
+ additional_inputs=[
148
+ gr.Textbox(
149
+ value="あなたは役に立つアシスタントです。",
150
+ label="System Prompt",
151
+ render=False,
152
+ ),
153
+ gr.Slider(
154
+ minimum=0,
155
+ maximum=1,
156
+ step=0.1,
157
+ value=0.3,
158
+ label="Temperature",
159
+ render=False,
160
+ ),
161
+ gr.Slider(
162
+ minimum=128,
163
+ maximum=8192,
164
+ step=1,
165
+ value=1024,
166
+ label="Max new tokens",
167
+ render=False,
168
+ ),
169
+ gr.Slider(
170
+ minimum=0.0,
171
+ maximum=1.0,
172
+ step=0.1,
173
+ value=1.0,
174
+ label="top_p",
175
+ render=False,
176
+ ),
177
+ gr.Slider(
178
+ minimum=1,
179
+ maximum=20,
180
+ step=1,
181
+ value=20,
182
+ label="top_k",
183
+ render=False,
184
+ ),
185
+ ],
186
+ examples=[
187
+ ["日本で有名なものと言えば"],
188
+ ["人工知能とは何ですか"],
189
+ ["C言語で素数を判定するコードを書いて"],
190
+ ["たぬきが主人公の物語を書いて"]
191
+ ],
192
+ cache_examples=False,
193
+ )
194
 
195
 
196
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -1 +1,6 @@
1
- huggingface_hub==0.22.2
 
 
 
 
 
 
1
+ accelerate==0.33.0
2
+ bitsandbytes==0.43.3
3
+ torch==2.2.0
4
+ transformers==4.44.0
5
+ einops==0.8.0
6
+ sentencepiece==0.2.0