Guchyos commited on
Commit
bbd68e1
·
verified ·
1 Parent(s): 9d9dcce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -54
app.py CHANGED
@@ -1,64 +1,79 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
- for val in history:
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
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
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()