matsuap commited on
Commit
c196004
โ€ข
1 Parent(s): c0f8f10

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +138 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ from transformers import GemmaTokenizer, AutoModelForCausalLM
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
5
+ from threading import Thread
6
+
7
+
8
+ DESCRIPTION = '''
9
+ <div>
10
+ <h1 style="text-align: center;">้žๅ…ฌๅผLlama-3-ELYZA-JP-8B</h1>
11
+ <p>elyza/Llama-3-ELYZA-JP-8Bใฎ้žๅ…ฌๅผใƒ‡ใƒขใ ใ‚ˆใ€‚ <a href="https://huggingface.co/elyza/Llama-3-ELYZA-JP-8B"><b>elyza/Llama-3-ELYZA-JP-8B</b></a>.</p>
12
+ </div>
13
+ '''
14
+
15
+ LICENSE = """
16
+ <p/>
17
+ ---
18
+ Built with Meta Llama 3
19
+ """
20
+
21
+ PLACEHOLDER = """
22
+ <div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
23
+ <h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
24
+ <p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">ใชใ‚“ใงใ‚‚ใใ„ใฆใญ</p>
25
+ </div>
26
+ """
27
+
28
+
29
+ css = """
30
+ h1 {
31
+ text-align: center;
32
+ display: block;
33
+ }
34
+ #duplicate-button {
35
+ margin: auto;
36
+ color: white;
37
+ background: #1565c0;
38
+ border-radius: 100vh;
39
+ }
40
+ """
41
+
42
+ # Load the tokenizer and model
43
+ tokenizer = AutoTokenizer.from_pretrained("elyza/Llama-3-ELYZA-JP-8B")
44
+ model = AutoModelForCausalLM.from_pretrained("elyza/Llama-3-ELYZA-JP-8B", device_map="auto")
45
+ terminators = [
46
+ tokenizer.eos_token_id,
47
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
48
+ ]
49
+
50
+ def chat_llama3_8b(message: str,
51
+ history: list,
52
+ temperature: float,
53
+ max_new_tokens: int
54
+ ) -> str:
55
+ """
56
+ Generate a streaming response using the llama3-8b model.
57
+ Args:
58
+ message (str): The input message.
59
+ history (list): The conversation history used by ChatInterface.
60
+ temperature (float): The temperature for generating the response.
61
+ max_new_tokens (int): The maximum number of new tokens to generate.
62
+ Returns:
63
+ str: The generated response.
64
+ """
65
+ conversation = []
66
+ conversation.append({"role": "system", "content": "ใ‚ใชใŸใฏๆ—ฅๆœฌ่ชžใงๅ›ž็ญ”ใ™ใ‚‹AIใ‚ขใ‚ทใ‚นใ‚ฟใƒณใƒˆใงใ™ใ€‚"})
67
+ for user, assistant in history:
68
+ conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
69
+ conversation.append({"role": "user", "content": message})
70
+
71
+ input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
72
+
73
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
74
+
75
+ generate_kwargs = dict(
76
+ input_ids= input_ids,
77
+ streamer=streamer,
78
+ max_new_tokens=max_new_tokens,
79
+ do_sample=True,
80
+ temperature=temperature,
81
+ top_p=0.95,
82
+ repetition_penalty=1.1,
83
+ eos_token_id=terminators,
84
+ )
85
+ # This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
86
+ if temperature == 0:
87
+ generate_kwargs['do_sample'] = False
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
+ print(outputs)
96
+ yield "".join(outputs)
97
+
98
+
99
+ # Gradio block
100
+ chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
101
+
102
+ with gr.Blocks(fill_height=True, css=css) as demo:
103
+
104
+ gr.Markdown(DESCRIPTION)
105
+ gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
106
+ gr.ChatInterface(
107
+ fn=chat_llama3_8b,
108
+ chatbot=chatbot,
109
+ fill_height=True,
110
+ additional_inputs_accordion=gr.Accordion(label="โš™๏ธ Parameters", open=False, render=False),
111
+ additional_inputs=[
112
+ gr.Slider(minimum=0,
113
+ maximum=1,
114
+ step=0.1,
115
+ value=0.2,
116
+ label="Temperature",
117
+ render=False),
118
+ gr.Slider(minimum=128,
119
+ maximum=4096,
120
+ step=1,
121
+ value=1024,
122
+ label="Max new tokens",
123
+ render=False ),
124
+ ],
125
+ examples=[
126
+ ['ๅฐๅญฆ็”Ÿใซใ‚‚ใ‚ใ‹ใ‚‹ใ‚ˆใ†ใซ็›ธๅฏพๆ€ง็†่ซ–ใ‚’ๆ•™ใˆใฆใใ ใ•ใ„ใ€‚'],
127
+ ['ๅฎ‡ๅฎ™ใฎ่ตทๆบใ‚’็Ÿฅใ‚‹ใŸใ‚ใฎๆ–นๆณ•ใ‚’ใ‚นใƒ†ใƒƒใƒ—ใƒปใƒใ‚คใƒปใ‚นใƒ†ใƒƒใƒ—ใงๆ•™ใˆใฆใใ ใ•ใ„ใ€‚'],
128
+ ['1ใ‹ใ‚‰100ใพใงใฎ็ด ๆ•ฐใ‚’ๆฑ‚ใ‚ใ‚‹ใ‚นใ‚ฏใƒชใƒ—ใƒˆใ‚’Pythonใงๆ›ธใ„ใฆใใ ใ•ใ„ใ€‚'],
129
+ ['ๅ‹้”ใฎ้™ฝ่‘ตใซใ‚ใ’ใ‚‹่ช•็”Ÿๆ—ฅใƒ—ใƒฌใ‚ผใƒณใƒˆใ‚’่€ƒใˆใฆใใ ใ•ใ„ใ€‚ใŸใ ใ—ใ€้™ฝ่‘ตใฏไธญๅญฆ็”Ÿใงใ€็งใฏๅŒใ˜ใ‚ฏใƒฉใ‚นใฎ็”ทๆ€งใงใ‚ใ‚‹ใ“ใจใ‚’่€ƒๆ…ฎใ—ใฆใใ ใ•ใ„ใ€‚'],
130
+ ['ใƒšใƒณใ‚ฎใƒณใŒใ‚ธใƒฃใƒณใ‚ฐใƒซใฎ็Ž‹ๆง˜ใงใ‚ใ‚‹ใ“ใจใ‚’ๆญฃๅฝ“ๅŒ–ใ™ใ‚‹ใ‚ˆใ†ใซ่ชฌๆ˜Žใ—ใฆใใ ใ•ใ„ใ€‚']
131
+ ],
132
+ cache_examples=False,
133
+ )
134
+
135
+ gr.Markdown(LICENSE)
136
+
137
+ if __name__ == "__main__":
138
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ accelerate
2
+ transformers
3
+ SentencePiece