Spaces:
Running
on
Zero
Running
on
Zero
alfredplpl
commited on
Commit
•
8533f9c
1
Parent(s):
61d3939
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Ref: https://huggingface.co/spaces/ysharma/Chat_with_Meta_llama3_8b
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import spaces
|
6 |
+
from transformers import GemmaTokenizer, AutoModelForCausalLM
|
7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
8 |
+
from threading import Thread
|
9 |
+
|
10 |
+
|
11 |
+
DESCRIPTION = '''
|
12 |
+
<div>
|
13 |
+
<h1 style="text-align: center;">非公式Llama-3-Swallow-8B-Instruct-v0.1</h1>
|
14 |
+
<p>tokyotech-llm/Llama-3-Swallow-8B-Instruct-v0.1の非公式デモだよ。 <a href="https://huggingface.co/tokyotech-llm/Llama-3-Swallow-8B-Instruct-v0.1"><b>tokyotech-llm/Llama-3-Swallow-8B-Instruct-v0.1</b></a>.</p>
|
15 |
+
</div>
|
16 |
+
'''
|
17 |
+
|
18 |
+
LICENSE = """
|
19 |
+
<p/>
|
20 |
+
|
21 |
+
---
|
22 |
+
Built with Meta Llama 3
|
23 |
+
"""
|
24 |
+
|
25 |
+
PLACEHOLDER = """
|
26 |
+
<div style="padding: 30px; text-align: center; display: flex; flex-direction: column; align-items: center;">
|
27 |
+
<h1 style="font-size: 28px; margin-bottom: 2px; opacity: 0.55;">Meta llama3</h1>
|
28 |
+
<p style="font-size: 18px; margin-bottom: 2px; opacity: 0.65;">なんでもきいてね</p>
|
29 |
+
</div>
|
30 |
+
"""
|
31 |
+
|
32 |
+
|
33 |
+
css = """
|
34 |
+
h1 {
|
35 |
+
text-align: center;
|
36 |
+
display: block;
|
37 |
+
}
|
38 |
+
|
39 |
+
#duplicate-button {
|
40 |
+
margin: auto;
|
41 |
+
color: white;
|
42 |
+
background: #1565c0;
|
43 |
+
border-radius: 100vh;
|
44 |
+
}
|
45 |
+
"""
|
46 |
+
|
47 |
+
# Load the tokenizer and model
|
48 |
+
tokenizer = AutoTokenizer.from_pretrained("tokyotech-llm/Llama-3-Swallow-8B-Instruct-v0.1")
|
49 |
+
model = AutoModelForCausalLM.from_pretrained("tokyotech-llm/Llama-3-Swallow-8B-Instruct-v0.1", device_map="auto")
|
50 |
+
terminators = [
|
51 |
+
tokenizer.eos_token_id,
|
52 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
53 |
+
]
|
54 |
+
|
55 |
+
@spaces.GPU()
|
56 |
+
def chat_llama3_8b(message: str,
|
57 |
+
history: list,
|
58 |
+
temperature: float,
|
59 |
+
max_new_tokens: int
|
60 |
+
) -> str:
|
61 |
+
"""
|
62 |
+
Generate a streaming response using the llama3-8b model.
|
63 |
+
Args:
|
64 |
+
message (str): The input message.
|
65 |
+
history (list): The conversation history used by ChatInterface.
|
66 |
+
temperature (float): The temperature for generating the response.
|
67 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
68 |
+
Returns:
|
69 |
+
str: The generated response.
|
70 |
+
"""
|
71 |
+
conversation = []
|
72 |
+
conversation.append({"role": "system", "content": "あなたは日本語で回答するAIアシスタントです。"})
|
73 |
+
for user, assistant in history:
|
74 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
75 |
+
conversation.append({"role": "user", "content": message})
|
76 |
+
|
77 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
78 |
+
|
79 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
80 |
+
|
81 |
+
generate_kwargs = dict(
|
82 |
+
input_ids= input_ids,
|
83 |
+
streamer=streamer,
|
84 |
+
max_new_tokens=max_new_tokens,
|
85 |
+
do_sample=True,
|
86 |
+
temperature=temperature,
|
87 |
+
top_p=0.95,
|
88 |
+
repetition_penalty=1.1,
|
89 |
+
eos_token_id=terminators,
|
90 |
+
)
|
91 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
92 |
+
if temperature == 0:
|
93 |
+
generate_kwargs['do_sample'] = False
|
94 |
+
|
95 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
96 |
+
t.start()
|
97 |
+
|
98 |
+
outputs = []
|
99 |
+
for text in streamer:
|
100 |
+
outputs.append(text)
|
101 |
+
print(outputs)
|
102 |
+
yield "".join(outputs)
|
103 |
+
|
104 |
+
|
105 |
+
# Gradio block
|
106 |
+
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
107 |
+
|
108 |
+
with gr.Blocks(fill_height=True, css=css) as demo:
|
109 |
+
|
110 |
+
gr.Markdown(DESCRIPTION)
|
111 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
112 |
+
gr.ChatInterface(
|
113 |
+
fn=chat_llama3_8b,
|
114 |
+
chatbot=chatbot,
|
115 |
+
fill_height=True,
|
116 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
117 |
+
additional_inputs=[
|
118 |
+
gr.Slider(minimum=0,
|
119 |
+
maximum=1,
|
120 |
+
step=0.1,
|
121 |
+
value=0.2,
|
122 |
+
label="Temperature",
|
123 |
+
render=False),
|
124 |
+
gr.Slider(minimum=128,
|
125 |
+
maximum=4096,
|
126 |
+
step=1,
|
127 |
+
value=1024,
|
128 |
+
label="Max new tokens",
|
129 |
+
render=False ),
|
130 |
+
],
|
131 |
+
examples=[
|
132 |
+
['小学生にもわかるように相対性理論を教えてください。'],
|
133 |
+
['宇宙の起源を知るための方法をステップ・バイ・ステップで教えてください。'],
|
134 |
+
['1から100までの素数を求めるスクリプトをPythonで書いてください。'],
|
135 |
+
['友達の陽葵にあげる誕生日プレゼントを考えてください。ただし、陽葵は中学生で、私は同じクラスの男性であることを考慮してください。'],
|
136 |
+
['ペンギンがジャングルの王様であることを正当化するように説明してください。']
|
137 |
+
],
|
138 |
+
cache_examples=False,
|
139 |
+
)
|
140 |
+
|
141 |
+
gr.Markdown(LICENSE)
|
142 |
+
|
143 |
+
if __name__ == "__main__":
|
144 |
+
demo.launch()
|
145 |
+
|