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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
from datetime import datetime, timezone, timedelta
import os
import time
from typing import AsyncGenerator
import uuid
import asyncio
import logging
import textwrap

import boto3
from botocore.config import Config
import gradio as gr
import pandas as pd
import torch

from model_vllm import get_input_token_length, run

logging.basicConfig(encoding='utf-8', level=logging.ERROR)
logger = logging.getLogger(__name__)

JST = timezone(timedelta(hours=+9), 'JST')

DEFAULT_SYSTEM_PROMPT = 'あなたは誠実で優秀な日本人のアシスタントです。'
MAX_MAX_NEW_TOKENS = 2048
DEFAULT_MAX_NEW_TOKENS = 512
MAX_INPUT_TOKEN_LENGTH = 4000

TITLE = '# ELYZA-japanese-Llama-2-13b-instruct'
DESCRIPTION = """
## 概要
- [ELYZA-japanese-Llama-2-13b](https://huggingface.co/elyza/ELYZA-japanese-Llama-2-13b)は、[株式会社ELYZA](https://elyza.ai/) (以降「当社」と呼称) が[Llama2](https://ai.meta.com/llama/)をベースとして日本語能力を拡張するために事前学習を行ったモデルです。
- [ELYZA-japanese-Llama-2-13b-instruct](https://huggingface.co/elyza/ELYZA-japanese-Llama-2-13b-instruct)は ELYZA-japanese-Llama-2-13b を弊社独自のinstruction tuning用データセットで事後学習したモデルです。
    - 本デモではこのモデルが使われています。
- 詳細は[Blog記事](https://note.com/elyza/n/n5d42686b60b7)を参照してください。
- 本デモではこちらの[Llama-2 7B Chat](https://huggingface.co/spaces/huggingface-projects/llama-2-7b-chat)のデモをベースにさせていただきました。

## License
- Llama 2 is licensed under the LLAMA 2 Community License, Copyright (c) Meta Platforms, Inc. All Rights Reserved.

## 免責事項
- 当社は、本デモについて、ユーザーの特定の目的に適合すること、期待する機能・正確性・有用性を有すること、出力データが完全性、正確性、有用性を有すること、ユーザーによる本サービスの利用がユーザーに適用のある法令等に適合すること、継続的に利用できること、及び不具合が生じないことについて、明示又は黙示を問わず何ら保証するものではありません。
- 当社は、本デモに関してユーザーが被った損害等につき、一切の責任を負わないものとし、ユーザーはあらかじめこれを承諾するものとします。
- 当社は、本デモを通じて、ユーザー又は第三者の個人情報を取得することを想定しておらず、ユーザーは、本デモに、ユーザー又は第三者の氏名その他の特定の個人を識別することができる情報等を入力等してはならないものとします。
- ユーザーは、当社が本デモ又は本デモに使用されているアルゴリズム等の改善・向上に使用することを許諾するものとします。

## 本デモで入力・出力されたデータの記録・利用に関して
- 本デモで入力・出力されたデータは当社にて記録させていただき、今後の本デモ又は本デモに使用されているアルゴリズム等の改善・向上に使用させていただく場合がございます。

## We are hiring!
- 当社 (株式会社ELYZA) に興味のある方、ぜひお話ししませんか?
- 機械学習エンジニア・インターン募集: https://open.talentio.com/r/1/c/elyza/homes/2507
- カジュアル面談はこちら: https://chillout.elyza.ai/elyza-japanese-llama2-13b
"""

_format_example = lambda s: textwrap.dedent(s).strip()

examples = list(map(_format_example, [
    """
        「キムチプリン」という新商品を考えています。この商品に対する世間の意見として想像されるものを箇条書きで3つ教えて
    """,
    """
        「メタリック」から「気分上々」までが自然につながるように、あいだの単語を連想してください。
    """,
    """
       自律神経や副交感神経が乱れている、とはどのような状態ですか?科学的に教えて 
    """,
    """
        日本国内で観光に行きたいと思っています。東京、名古屋、大阪、京都、福岡の特徴を表にまとめてください。
        列名は「都道府県」「おすすめスポット」「おすすめグルメ」にしてください。
    """,
    """
        私の考えた創作料理について、想像して説明を書いてください。

        1. トマトマット
        2. 餃子風もやし炒め
        3. おにぎりすぎ
    """,
]))

if not torch.cuda.is_available():
    DESCRIPTION += '\n<p>Running on CPU 🥶 This demo does not work on CPU.</p>'

try:
    s3 = boto3.client(
        's3',
        aws_access_key_id=os.environ['AWS_ACCESS_KEY_ID'],
        aws_secret_access_key=os.environ['AWS_SECRET_ACCESS_KEY'],
        region_name=os.environ['S3_REGION'],
        config=Config(
            connect_timeout=5,
            read_timeout=5,
            retries={
                'mode': 'standard',
                'total_max_attempts': 3,
            },
        ),
    )
except Exception:
    logger.exception('Failed to initialize S3 client')


def clear_and_save_textbox(message: str) -> tuple[str, str]:
    return '', message


def display_input(message: str, history: list[tuple[str, str]]) -> list[tuple[str, str]]:
    history.append((message, ''))
    return history


def delete_prev_fn(history: list[tuple[str, str]]) -> tuple[list[tuple[str, str]], str]:
    try:
        message, _ = history.pop()
    except IndexError:
        message = ''
    return history, message or ''


async def generate(
    message: str,
    history_with_input: list[tuple[str, str]],
    system_prompt: str,
    max_new_tokens: int,
    temperature: float,
    top_p: float,
    top_k: int,
    do_sample: bool,
    repetition_penalty: float,
) -> AsyncGenerator[list[tuple[str, str]], None]:
    if max_new_tokens > MAX_MAX_NEW_TOKENS:
        raise ValueError

    history = history_with_input[:-1]
    stream = await run(
        message=message,
        chat_history=history,
        system_prompt=system_prompt,
        max_new_tokens=max_new_tokens,
        temperature=float(temperature),
        top_p=float(top_p),
        top_k=top_k,
        do_sample=do_sample,
        repetition_penalty=float(repetition_penalty),
        stream=True,
    )
    async for response in stream:
        yield history + [(message, response)]


def process_example(message: str) -> tuple[str, list[tuple[str, str]]]:
    response = asyncio.run(run(
        message=message,
        chat_history=[],
        system_prompt=DEFAULT_SYSTEM_PROMPT,
        max_new_tokens=DEFAULT_MAX_NEW_TOKENS,
        temperature=1,
        top_p=0.95,
        top_k=50,
        do_sample=False,
        repetition_penalty=1.0,
        stream=False
    ))

    return '', [(message, response)]

def check_input_token_length(message: str, chat_history: list[tuple[str, str]], system_prompt: str) -> None:
    input_token_length = get_input_token_length(message, chat_history, system_prompt)
    if input_token_length > MAX_INPUT_TOKEN_LENGTH:
        raise gr.Error(
            f'合計対話長が長すぎます ({input_token_length} > {MAX_INPUT_TOKEN_LENGTH})。入力文章を短くするか、「🗑️  これまでの出力を消す」ボタンを押してから再実行してください。'
        )

    if len(message) <= 0:
        raise gr.Error('入力が空です。1文字以上の文字列を入力してください。')


def convert_history_to_str(history: list[tuple[str, str]]) -> str:
    res = []
    for user_utt, sys_utt in history:
        res.append(f'😃: {user_utt}')
        res.append(f'🤖: {sys_utt}')
    return '<br>'.join(res)


def output_log(history: list[tuple[str, str]], uuid_list: list[tuple[str, str]]) -> None:
    tree_uuid = uuid_list[0][0]
    last_messages = history[-1]
    last_uuids = uuid_list[-1]
    parent_uuid = None
    record_message = None
    record_uuid = None
    role = None
    if last_uuids[1] == '':
        role = 'user'
        record_message = last_messages[0]
        record_uuid = last_uuids[0]
        if len(history) >= 2:
            parent_uuid = uuid_list[-2][1]
        else:
            parent_uuid = last_uuids[0]
    else:
        role = 'assistant'
        record_message = last_messages[1]
        record_uuid = last_uuids[1]
        parent_uuid = last_uuids[0]

    now = datetime.fromtimestamp(time.time(), JST)
    yyyymmdd = now.strftime('%Y%m%d')
    created_at = now.strftime('%Y-%m-%d %H:%M:%S.%f')

    d = {
        'created_at': created_at,
        'tree_uuid': tree_uuid,
        'parent_uuid': parent_uuid,
        'uuid': record_uuid,
        'role': role,
        'message': record_message,
    }
    try:
        csv_buffer = pd.DataFrame(d, index=[0]).to_csv(index=None)
        s3.put_object(
            Bucket=os.environ['S3_BUCKET'],
            Key=f"{os.environ['S3_KEY_PREFIX']}/{yyyymmdd}/{record_uuid}.csv",
            Body=csv_buffer,
        )
    except Exception:
        logger.exception('Failed to upload log to S3')
    return


def assign_uuid(history: list[tuple[str, str]], uuid_list: list[tuple[str, str]]) -> list[tuple[str, str]]:
    len_history = len(history)
    len_uuid_list = len(uuid_list)
    new_uuid_list = [x for x in uuid_list]

    if len_history > len_uuid_list:
        for t_history in history[len_uuid_list:]:
            if t_history[1] == '':
                # 入力だけされてるタイミング
                new_uuid_list.append((str(uuid.uuid4()), ''))
            else:
                # undoなどを経て、入力だけされてるタイミングを飛び越えた場合
                new_uuid_list.append((str(uuid.uuid4()), str(uuid.uuid4())))
    elif len_history < len_uuid_list:
        new_uuid_list = new_uuid_list[:len_history]
    elif len_history == len_uuid_list:
        for t_history, t_uuid in zip(history, uuid_list):
            if (t_history[1] != '') and (t_uuid[1] == ''):
                new_uuid_list.pop()
                new_uuid_list.append((t_uuid[0], str(uuid.uuid4())))
            elif (t_history[1] == '') and (t_uuid[1] != ''):
                new_uuid_list.pop()
                new_uuid_list.append((t_uuid[0], ''))
    return new_uuid_list


with gr.Blocks(css='style.css') as demo:
    gr.Markdown(TITLE)

    with gr.Row():
        gr.HTML(
            """
        <div id="logo">
            <img src='file/key_visual.png' width=1200 min-width=300></img>
        </div>
        """
        )

    with gr.Group():
        chatbot = gr.Chatbot(
            label='Chatbot',
            height=600,
            avatar_images=['person_face.png', 'llama_face.png'],
        )
        with gr.Column():
            textbox = gr.Textbox(
                container=False,
                show_label=False,
                placeholder='指示を入力してください。例: カレーとハンバーグを組み合わせた美味しい料理を3つ教えて',
                scale=10,
                lines=10,
            )
            submit_button = gr.Button(
                '以下の説明文・免責事項・データ利用に同意して送信', variant='primary', scale=1, min_width=0
            )
            gr.Markdown(
                '※ 繰り返しが発生する場合は、以下「詳細設定」の `repetition_penalty` を1.05〜1.20など調整すると上手くいく場合があります'
            )
    with gr.Row():
        retry_button = gr.Button('🔄  同じ入力でもう一度生成', variant='secondary')
        undo_button = gr.Button('↩️ ひとつ前の状態に戻る', variant='secondary')
        clear_button = gr.Button('🗑️  これまでの出力を消す', variant='secondary')

    saved_input = gr.State()
    uuid_list = gr.State([])

    with gr.Accordion(label='上の対話履歴をスクリーンショット用に整形', open=False):
        output_textbox = gr.Markdown()

    with gr.Accordion(label='詳細設定', open=False):
        system_prompt = gr.Textbox(label='システムプロンプト', value=DEFAULT_SYSTEM_PROMPT, lines=8)
        max_new_tokens = gr.Slider(
            label='最大出力トークン数',
            minimum=1,
            maximum=MAX_MAX_NEW_TOKENS,
            step=1,
            value=DEFAULT_MAX_NEW_TOKENS,
        )
        repetition_penalty = gr.Slider(
            label='Repetition penalty',
            minimum=1.0,
            maximum=10.0,
            step=0.1,
            value=1.0,
        )
        do_sample = gr.Checkbox(label='do_sample', value=False)
        temperature = gr.Slider(
            label='Temperature',
            minimum=0.1,
            maximum=4.0,
            step=0.1,
            value=1.0,
        )
        top_p = gr.Slider(
            label='Top-p (nucleus sampling)',
            minimum=0.05,
            maximum=1.0,
            step=0.05,
            value=0.95,
        )
        top_k = gr.Slider(
            label='Top-k',
            minimum=1,
            maximum=1000,
            step=1,
            value=50,
        )

    gr.Examples(
        examples=examples,
        inputs=textbox,
        outputs=[textbox, chatbot],
        fn=process_example,
        cache_examples=True,
    )

    gr.Markdown(DESCRIPTION)

    textbox.submit(
        fn=clear_and_save_textbox,
        inputs=textbox,
        outputs=[textbox, saved_input],
        api_name=False,
        queue=False,
    ).then(
        fn=check_input_token_length,
        inputs=[saved_input, chatbot, system_prompt],
        api_name=False,
        queue=False,
    ).success(
        fn=display_input,
        inputs=[saved_input, chatbot],
        outputs=chatbot,
        api_name=False,
        queue=False,
    ).then(
        fn=assign_uuid,
        inputs=[chatbot, uuid_list],
        outputs=uuid_list,
    ).then(
        fn=output_log,
        inputs=[chatbot, uuid_list],
    ).then(
        fn=generate,
        inputs=[
            saved_input,
            chatbot,
            system_prompt,
            max_new_tokens,
            temperature,
            top_p,
            top_k,
            do_sample,
            repetition_penalty,
        ],
        outputs=chatbot,
        api_name=False,
    ).then(
        fn=assign_uuid,
        inputs=[chatbot, uuid_list],
        outputs=uuid_list,
    ).then(
        fn=output_log,
        inputs=[chatbot, uuid_list],
    ).then(
        fn=convert_history_to_str,
        inputs=chatbot,
        outputs=output_textbox,
    )

    button_event_preprocess = (
        submit_button.click(
            fn=clear_and_save_textbox,
            inputs=textbox,
            outputs=[textbox, saved_input],
            api_name=False,
            queue=False,
        )
        .then(
            fn=check_input_token_length,
            inputs=[saved_input, chatbot, system_prompt],
            api_name=False,
            queue=False,
        )
        .success(
            fn=display_input,
            inputs=[saved_input, chatbot],
            outputs=chatbot,
            api_name=False,
            queue=False,
        )
        .then(
            fn=assign_uuid,
            inputs=[chatbot, uuid_list],
            outputs=uuid_list,
        )
        .then(
            fn=output_log,
            inputs=[chatbot, uuid_list],
        )
        .success(
            fn=generate,
            inputs=[
                saved_input,
                chatbot,
                system_prompt,
                max_new_tokens,
                temperature,
                top_p,
                top_k,
                do_sample,
                repetition_penalty,
            ],
            outputs=chatbot,
            api_name=False,
        )
        .then(
            fn=assign_uuid,
            inputs=[chatbot, uuid_list],
            outputs=uuid_list,
        )
        .then(
            fn=output_log,
            inputs=[chatbot, uuid_list],
        )
        .then(
            fn=convert_history_to_str,
            inputs=chatbot,
            outputs=output_textbox,
        )
    )

    retry_button.click(
        fn=delete_prev_fn,
        inputs=chatbot,
        outputs=[chatbot, saved_input],
        api_name=False,
        queue=False,
    ).then(
        fn=check_input_token_length,
        inputs=[saved_input, chatbot, system_prompt],
        api_name=False,
        queue=False,
    ).success(
        fn=display_input,
        inputs=[saved_input, chatbot],
        outputs=chatbot,
        api_name=False,
        queue=False,
    ).then(
        fn=assign_uuid,
        inputs=[chatbot, uuid_list],
        outputs=uuid_list,
    ).then(
        fn=output_log,
        inputs=[chatbot, uuid_list],
    ).then(
        fn=generate,
        inputs=[
            saved_input,
            chatbot,
            system_prompt,
            max_new_tokens,
            temperature,
            top_p,
            top_k,
            do_sample,
            repetition_penalty,
        ],
        outputs=chatbot,
        api_name=False,
    ).then(
        fn=assign_uuid,
        inputs=[chatbot, uuid_list],
        outputs=uuid_list,
    ).then(
        fn=output_log,
        inputs=[chatbot, uuid_list],
    ).then(
        fn=convert_history_to_str,
        inputs=chatbot,
        outputs=output_textbox,
    )

    undo_button.click(
        fn=delete_prev_fn,
        inputs=chatbot,
        outputs=[chatbot, saved_input],
        api_name=False,
        queue=False,
    ).then(
        fn=assign_uuid,
        inputs=[chatbot, uuid_list],
        outputs=uuid_list,
    ).then(
        fn=lambda x: x,
        inputs=saved_input,
        outputs=textbox,
        api_name=False,
        queue=False,
    ).then(
        fn=convert_history_to_str,
        inputs=chatbot,
        outputs=output_textbox,
    )

    clear_button.click(
        fn=lambda: ([], ''),
        outputs=[chatbot, saved_input],
        queue=False,
        api_name=False,
    ).then(
        fn=assign_uuid,
        inputs=[chatbot, uuid_list],
        outputs=uuid_list,
    ).then(
        fn=convert_history_to_str,
        inputs=chatbot,
        outputs=output_textbox,
    )

demo.queue(max_size=5).launch(server_name='0.0.0.0')