Wassupbro123 commited on
Commit
11dea74
·
verified ·
1 Parent(s): 719f28b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -31
app.py CHANGED
@@ -1,13 +1,17 @@
 
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
 
8
- processor = AutoProcessor.from_pretrained("mlabonne/gemma-3-27b-it-abliterated")
9
- model = AutoModelForImageTextToText.from_pretrained("mlabonne/gemma-3-27b-it-abliterated")
10
 
 
 
 
11
 
12
  def respond(
13
  message,
@@ -17,50 +21,41 @@ def respond(
17
  temperature,
18
  top_p,
19
  ):
 
20
  messages = [{"role": "system", "content": system_message}]
21
-
22
- for val in history:
23
- if val[0]:
24
- messages.append({"role": "user", "content": val[0]})
25
- if val[1]:
26
- messages.append({"role": "assistant", "content": val[1]})
27
-
28
  messages.append({"role": "user", "content": message})
29
 
30
  response = ""
31
-
32
- for message in client.chat_completion(
33
- messages,
 
34
  max_tokens=max_tokens,
35
- stream=True,
36
  temperature=temperature,
37
  top_p=top_p,
 
38
  ):
39
- token = message.choices[0].delta.content
40
-
41
- response += token
42
  yield response
43
 
44
-
45
- """
46
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
47
- """
48
  demo = gr.ChatInterface(
49
  respond,
50
  additional_inputs=[
51
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
52
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
53
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
54
- gr.Slider(
55
- minimum=0.1,
56
- maximum=1.0,
57
- value=0.95,
58
- step=0.05,
59
- label="Top-p (nucleus sampling)",
60
- ),
61
  ],
62
  )
63
 
64
-
65
  if __name__ == "__main__":
66
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  from huggingface_hub import InferenceClient
4
+ from transformers import AutoTokenizer, AutoModelForImageTextToText
5
 
6
+ # ── ① Hugging Face トークンを環境変数にセット(ご自身のトークンを設定してください)
7
+ os.environ["HUGGINGFACE_TOKEN"] = "YOUR_HF_TOKEN"
 
8
 
9
+ # ── ② InferenceClient を生成
10
+ client = InferenceClient(api_key=os.environ["HUGGINGFACE_TOKEN"])
11
 
12
+ # (※ローカル推論は使わない場合、以下のモデルロードは不要です)
13
+ tokenizer = AutoTokenizer.from_pretrained("mlabonne/gemma-3-27b-it-abliterated")
14
+ model = AutoModelForImageTextToText.from_pretrained("mlabonne/gemma-3-27b-it-abliterated")
15
 
16
  def respond(
17
  message,
 
21
  temperature,
22
  top_p,
23
  ):
24
+ # システムプロンプトを先頭に
25
  messages = [{"role": "system", "content": system_message}]
26
+ # 過去のやり取りを追加
27
+ for u, a in history:
28
+ if u:
29
+ messages.append({"role": "user", "content": u})
30
+ if a:
31
+ messages.append({"role": "assistant", "content": a})
32
+ # 最新ユーザー入力
33
  messages.append({"role": "user", "content": message})
34
 
35
  response = ""
36
+ # ── ③ モデル指定で chat_completion を呼び出し
37
+ for chunk in client.chat_completion(
38
+ model="mlabonne/gemma-3-27b-it-abliterated",
39
+ messages=messages,
40
  max_tokens=max_tokens,
 
41
  temperature=temperature,
42
  top_p=top_p,
43
+ stream=True,
44
  ):
45
+ delta = chunk.choices[0].delta.content
46
+ response += delta
 
47
  yield response
48
 
49
+ # ── ④ Gradio の ChatInterface 定義
 
 
 
50
  demo = gr.ChatInterface(
51
  respond,
52
  additional_inputs=[
53
  gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
54
  gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
55
  gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
56
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
 
 
 
 
 
 
57
  ],
58
  )
59
 
 
60
  if __name__ == "__main__":
61
  demo.launch()