Spaces:
Runtime error
Runtime error
username
commited on
Commit
·
09d8f0d
1
Parent(s):
b782015
fix
Browse files- app.py +0 -12
- requirements.txt +0 -1
app.py
CHANGED
|
@@ -3,16 +3,11 @@ import gradio as gr
|
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
| 6 |
-
# トークナイザーのロード(GPUは不要なのでグローバルにロード)
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained("llm-jp/llm-jp-3-8x1.8b-instruct3")
|
| 8 |
-
|
| 9 |
-
# グローバル変数としてモデルを定義(最初はNone)
|
| 10 |
model = None
|
| 11 |
|
| 12 |
-
# ZeroGPUで実行される関数(モデルロードに時間がかかる可能性があるため120秒に設定)
|
| 13 |
@spaces.GPU(duration=120)
|
| 14 |
def generate_text(system_prompt, user_input, max_length=512, temperature=0.7, top_p=0.95):
|
| 15 |
-
# モデルのロード(初回実行時のみ)
|
| 16 |
global model
|
| 17 |
if model is None:
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -21,13 +16,11 @@ def generate_text(system_prompt, user_input, max_length=512, temperature=0.7, to
|
|
| 21 |
torch_dtype=torch.bfloat16
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# チャット形式の入力を作成
|
| 25 |
chat = [
|
| 26 |
{"role": "system", "content": system_prompt},
|
| 27 |
{"role": "user", "content": user_input},
|
| 28 |
]
|
| 29 |
|
| 30 |
-
# トークン化
|
| 31 |
tokenized_input = tokenizer.apply_chat_template(
|
| 32 |
chat,
|
| 33 |
add_generation_prompt=True,
|
|
@@ -35,7 +28,6 @@ def generate_text(system_prompt, user_input, max_length=512, temperature=0.7, to
|
|
| 35 |
return_tensors="pt"
|
| 36 |
).to(model.device)
|
| 37 |
|
| 38 |
-
# 生成
|
| 39 |
with torch.no_grad():
|
| 40 |
output = model.generate(
|
| 41 |
tokenized_input,
|
|
@@ -46,12 +38,9 @@ def generate_text(system_prompt, user_input, max_length=512, temperature=0.7, to
|
|
| 46 |
repetition_penalty=1.05,
|
| 47 |
)[0]
|
| 48 |
|
| 49 |
-
# デコード
|
| 50 |
generated_text = tokenizer.decode(output, skip_special_tokens=True)
|
| 51 |
-
|
| 52 |
return generated_text
|
| 53 |
|
| 54 |
-
# Gradioインターフェースの作成
|
| 55 |
with gr.Blocks() as demo:
|
| 56 |
gr.Markdown("# LLM-JP-3-8x1.8b-instruct3 デモ")
|
| 57 |
gr.Markdown("国立情報学研究所が開発した日本語大規模言語モデル「LLM-JP-3」のデモです。ZeroGPUを使用しているため、初回実行時はGPUの割り当てに少し時間がかかることがあります。")
|
|
@@ -90,5 +79,4 @@ with gr.Blocks() as demo:
|
|
| 90 |
inputs=[system_prompt, user_input]
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# アプリの起動
|
| 94 |
demo.launch()
|
|
|
|
| 3 |
import spaces
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
|
|
|
| 6 |
tokenizer = AutoTokenizer.from_pretrained("llm-jp/llm-jp-3-8x1.8b-instruct3")
|
|
|
|
|
|
|
| 7 |
model = None
|
| 8 |
|
|
|
|
| 9 |
@spaces.GPU(duration=120)
|
| 10 |
def generate_text(system_prompt, user_input, max_length=512, temperature=0.7, top_p=0.95):
|
|
|
|
| 11 |
global model
|
| 12 |
if model is None:
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 16 |
torch_dtype=torch.bfloat16
|
| 17 |
)
|
| 18 |
|
|
|
|
| 19 |
chat = [
|
| 20 |
{"role": "system", "content": system_prompt},
|
| 21 |
{"role": "user", "content": user_input},
|
| 22 |
]
|
| 23 |
|
|
|
|
| 24 |
tokenized_input = tokenizer.apply_chat_template(
|
| 25 |
chat,
|
| 26 |
add_generation_prompt=True,
|
|
|
|
| 28 |
return_tensors="pt"
|
| 29 |
).to(model.device)
|
| 30 |
|
|
|
|
| 31 |
with torch.no_grad():
|
| 32 |
output = model.generate(
|
| 33 |
tokenized_input,
|
|
|
|
| 38 |
repetition_penalty=1.05,
|
| 39 |
)[0]
|
| 40 |
|
|
|
|
| 41 |
generated_text = tokenizer.decode(output, skip_special_tokens=True)
|
|
|
|
| 42 |
return generated_text
|
| 43 |
|
|
|
|
| 44 |
with gr.Blocks() as demo:
|
| 45 |
gr.Markdown("# LLM-JP-3-8x1.8b-instruct3 デモ")
|
| 46 |
gr.Markdown("国立情報学研究所が開発した日本語大規模言語モデル「LLM-JP-3」のデモです。ZeroGPUを使用しているため、初回実行時はGPUの割り当てに少し時間がかかることがあります。")
|
|
|
|
| 79 |
inputs=[system_prompt, user_input]
|
| 80 |
)
|
| 81 |
|
|
|
|
| 82 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -3,5 +3,4 @@ torch
|
|
| 3 |
transformers
|
| 4 |
tokenizers
|
| 5 |
accelerate
|
| 6 |
-
flash-attn
|
| 7 |
gradio
|
|
|
|
| 3 |
transformers
|
| 4 |
tokenizers
|
| 5 |
accelerate
|
|
|
|
| 6 |
gradio
|