Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,79 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
def
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.choices[0].delta.content
|
38 |
-
|
39 |
-
response += token
|
40 |
-
yield response
|
41 |
-
|
42 |
-
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
],
|
|
|
|
|
|
|
|
|
60 |
)
|
61 |
|
62 |
-
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
+
import torch
|
4 |
|
5 |
+
def load_model():
|
6 |
+
model_name = "Guchyos/gemma-2b-elyza-task"
|
7 |
+
|
8 |
+
print("Loading tokenizer...")
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
10 |
+
|
11 |
+
print("Loading model...")
|
12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
13 |
+
model_name,
|
14 |
+
device_map="auto",
|
15 |
+
torch_dtype=torch.float16
|
16 |
+
)
|
17 |
+
return model, tokenizer
|
18 |
|
19 |
+
# モデルをグローバルに1回だけロード
|
20 |
+
try:
|
21 |
+
model, tokenizer = load_model()
|
22 |
+
print("Model loaded successfully!")
|
23 |
+
except Exception as e:
|
24 |
+
print(f"Error loading model: {str(e)}")
|
25 |
|
26 |
+
def predict(message, history):
|
27 |
+
try:
|
28 |
+
# 入力の準備
|
29 |
+
prompt = f"質問: {message}\n\n回答:"
|
30 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
31 |
+
|
32 |
+
# 生成
|
33 |
+
outputs = model.generate(
|
34 |
+
**inputs,
|
35 |
+
max_new_tokens=512,
|
36 |
+
temperature=0.7,
|
37 |
+
top_p=0.9,
|
38 |
+
do_sample=True,
|
39 |
+
repetition_penalty=1.1
|
40 |
+
)
|
41 |
+
|
42 |
+
# 応答の生成
|
43 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
44 |
+
return response.replace(prompt, "").strip()
|
45 |
+
|
46 |
+
except Exception as e:
|
47 |
+
return f"エラーが発生しました: {str(e)}"
|
48 |
|
49 |
+
# チャットボットインターフェースの作成
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
demo = gr.ChatInterface(
|
51 |
+
fn=predict,
|
52 |
+
title="💬 Gemma 2 Quantized for ELYZA-tasks",
|
53 |
+
description="""
|
54 |
+
# ELYZA-tasks-100-TV用に最適化された日本語LLMです
|
55 |
+
|
56 |
+
## 使い方
|
57 |
+
- 質問を入力してEnterキーを押してください
|
58 |
+
- 生成には数秒かかります
|
59 |
+
- 結果が気に入らない場合は「再生成」ボタンを押してください
|
60 |
+
|
61 |
+
## 特徴
|
62 |
+
- 4bit量子化により最適化
|
63 |
+
- 日本語に特化
|
64 |
+
- ELYZA-tasks形式に対応
|
65 |
+
""",
|
66 |
+
examples=[
|
67 |
+
"日本の四季について、それぞれの特徴を説明してください。",
|
68 |
+
"人工知能の発展における倫理的な課題について説明してください。",
|
69 |
+
"東京の主要な観光スポットを3つ挙げて、それぞれ説明してください。"
|
70 |
],
|
71 |
+
retry_btn="🔄 再生成",
|
72 |
+
undo_btn="↩️ 取り消し",
|
73 |
+
clear_btn="🗑️ クリア",
|
74 |
+
theme=gr.themes.Soft()
|
75 |
)
|
76 |
|
77 |
+
# アプリの起動
|
78 |
if __name__ == "__main__":
|
79 |
+
demo.launch()
|