Spaces:
Running
on
Zero
Running
on
Zero
alfredplpl
commited on
Commit
•
6ec23f7
1
Parent(s):
04c6086
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Ref: https://huggingface.co/spaces/ysharma/Chat_with_Meta_llama3_8b
|
2 |
+
|
3 |
+
import spaces
|
4 |
+
import gradio as gr
|
5 |
+
import os
|
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;">非公式LLM-JP-3-13B-Instruct</h1>
|
14 |
+
<p>LLM-JP-3-13B-Instructの非公式デモだよ。 <a href="https://huggingface.co/llm-jp/llm-jp-3-13b-instruct"><b>llm-jp/llm-jp-3-13b-instruct</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("llm-jp/llm-jp-3-13b-instruct")
|
49 |
+
model = AutoModelForCausalLM.from_pretrained("llm-jp/llm-jp-3-13b-instruct", device_map="auto")
|
50 |
+
|
51 |
+
@spaces.GPU()
|
52 |
+
def chat_llama3_8b(message: str,
|
53 |
+
history: list,
|
54 |
+
temperature: float,
|
55 |
+
max_new_tokens: int
|
56 |
+
) -> str:
|
57 |
+
"""
|
58 |
+
Generate a streaming response using the llama3-8b model.
|
59 |
+
Args:
|
60 |
+
message (str): The input message.
|
61 |
+
history (list): The conversation history used by ChatInterface.
|
62 |
+
temperature (float): The temperature for generating the response.
|
63 |
+
max_new_tokens (int): The maximum number of new tokens to generate.
|
64 |
+
Returns:
|
65 |
+
str: The generated response.
|
66 |
+
"""
|
67 |
+
conversation = []
|
68 |
+
conversation.append({"role": "system", "content": "以下は、タスクを説明する指示です。要求を適切に満たす応答を書きなさい。"})
|
69 |
+
for user, assistant in history:
|
70 |
+
conversation.extend([{"role": "user", "content": user}, {"role": "assistant", "content": assistant}])
|
71 |
+
conversation.append({"role": "user", "content": message})
|
72 |
+
|
73 |
+
input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(model.device)
|
74 |
+
|
75 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
|
76 |
+
|
77 |
+
generate_kwargs = dict(
|
78 |
+
input_ids= input_ids,
|
79 |
+
streamer=streamer,
|
80 |
+
max_new_tokens=max_new_tokens,
|
81 |
+
do_sample=True,
|
82 |
+
temperature=temperature,
|
83 |
+
top_p=0.95,
|
84 |
+
repetition_penalty=1.1,
|
85 |
+
eos_token_id=terminators,
|
86 |
+
)
|
87 |
+
# This will enforce greedy generation (do_sample=False) when the temperature is passed 0, avoiding the crash.
|
88 |
+
if temperature == 0:
|
89 |
+
generate_kwargs['do_sample'] = False
|
90 |
+
|
91 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
92 |
+
t.start()
|
93 |
+
|
94 |
+
outputs = []
|
95 |
+
for text in streamer:
|
96 |
+
outputs.append(text)
|
97 |
+
print(outputs)
|
98 |
+
yield "".join(outputs)
|
99 |
+
|
100 |
+
|
101 |
+
# Gradio block
|
102 |
+
chatbot=gr.Chatbot(height=450, placeholder=PLACEHOLDER, label='Gradio ChatInterface')
|
103 |
+
|
104 |
+
with gr.Blocks(fill_height=True, css=css) as demo:
|
105 |
+
|
106 |
+
gr.Markdown(DESCRIPTION)
|
107 |
+
gr.DuplicateButton(value="Duplicate Space for private use", elem_id="duplicate-button")
|
108 |
+
gr.ChatInterface(
|
109 |
+
fn=chat_llama3_8b,
|
110 |
+
chatbot=chatbot,
|
111 |
+
fill_height=True,
|
112 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
113 |
+
additional_inputs=[
|
114 |
+
gr.Slider(minimum=0,
|
115 |
+
maximum=1,
|
116 |
+
step=0.1,
|
117 |
+
value=0.7,
|
118 |
+
label="Temperature",
|
119 |
+
render=False),
|
120 |
+
gr.Slider(minimum=128,
|
121 |
+
maximum=4096,
|
122 |
+
step=1,
|
123 |
+
value=1024,
|
124 |
+
label="Max new tokens",
|
125 |
+
render=False ),
|
126 |
+
],
|
127 |
+
examples=[
|
128 |
+
['小学生にもわかるように相対性理論を教えてください。'],
|
129 |
+
['宇宙の起源を知るための方法をステップ・バイ・ステップで教えてください。'],
|
130 |
+
['1から100までの素数を求めるスクリプトをPythonで書いてください。'],
|
131 |
+
['友達の陽葵にあげる誕生日プレゼントを考えてください。ただし、陽葵は中学生で、私は同じクラスの男性であることを考慮してください。'],
|
132 |
+
['ペンギンがジャングルの王様であることを正当化するように説明してください。']
|
133 |
+
],
|
134 |
+
cache_examples=False,
|
135 |
+
)
|
136 |
+
|
137 |
+
gr.Markdown(LICENSE)
|
138 |
+
|
139 |
+
if __name__ == "__main__":
|
140 |
+
demo.launch()
|
141 |
+
|