noppodev commited on
Commit
d77ac7b
·
verified ·
1 Parent(s): 7c6e803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -22
app.py CHANGED
@@ -6,30 +6,33 @@ import torch
6
  from huggingface_hub import HfApi
7
 
8
  # ---------------------------------------------------------
9
- # モデル準備セクション
10
  # ---------------------------------------------------------
11
  def prepare_model():
12
  model_path = "./ni_v1_model"
13
  token = os.getenv("HF_TOKEN")
14
 
15
  if not os.path.exists(model_path):
16
- print("🚀 NI-v1 マージ開始")
17
  env = os.environ.copy()
18
  if token:
19
  env["HF_TOKEN"] = token
 
20
  subprocess.run(["huggingface-cli", "login", "--token", token], check=True)
21
 
22
  try:
 
23
  subprocess.run(
24
  ["mergekit-yaml", "config.yaml", model_path, "--allow-crimes"],
25
  check=True,
26
  env=env
27
  )
28
- print("✨ マージ成功")
29
- except:
30
- raise RuntimeError("マージ失敗")
 
31
 
32
- print("🧠 NI-v1 ロード中...")
33
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
34
  model = AutoModelForCausalLM.from_pretrained(
35
  model_path,
@@ -39,32 +42,38 @@ def prepare_model():
39
  )
40
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
41
 
 
42
  try:
43
  pipe = prepare_model()
44
  except Exception as e:
45
- print(f"起動失敗: {e}")
46
  pipe = None
47
 
48
  # ---------------------------------------------------------
49
- # ロジックセクション
50
  # ---------------------------------------------------------
51
  def chat_fn(message):
52
- if pipe is None: return "知能ユニットが死んでるぜ。"
53
- # NoppoIntelligence用のプロンプトフォーマット
 
54
  prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
55
  outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
56
- return outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
 
 
 
57
 
58
  # ---------------------------------------------------------
59
- # UIセクション (Gradio 5.x 辞書型完全準拠)
60
  # ---------------------------------------------------------
61
  with gr.Blocks(title="NI-v1.0") as demo:
62
  gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
 
63
 
64
  with gr.Tab("チャット"):
65
- # 強制的に辞書型(messages)を指定。これで incompatible エラーは物理的に出なくなる。
66
  chatbot = gr.Chatbot(type="messages")
67
- msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。")
68
  with gr.Row():
69
  send = gr.Button("送信", variant="primary")
70
  clear = gr.Button("クリア")
@@ -73,25 +82,28 @@ with gr.Blocks(title="NI-v1.0") as demo:
73
  if not message:
74
  return "", chat_history
75
 
76
- # 1. ユーザーの入力を追加
77
  chat_history.append({"role": "user", "content": message})
78
 
79
- # 2. AIの回答を生成
80
  bot_res = chat_fn(message)
81
 
82
- # 3. AIの回答を追加
83
  chat_history.append({"role": "assistant", "content": bot_res})
84
 
85
  return "", chat_history
86
 
 
87
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
88
  send.click(respond, [msg, chatbot], [msg, chatbot])
89
- clear.click(lambda: None, None, chatbot, queue=False)
90
 
91
  with gr.Tab("公開"):
 
 
92
  repo_id = gr.Textbox(label="Repo ID", value="noppodev/NoppoIntelligence")
93
- user_token = gr.Textbox(label="Write Token", type="password")
94
- pub_btn = gr.Button("アップロード")
95
  status = gr.Textbox(label="Status")
96
 
97
  def upload(r, t):
@@ -99,10 +111,12 @@ with gr.Blocks(title="NI-v1.0") as demo:
99
  api = HfApi()
100
  api.create_repo(repo_id=r, repo_type="model", exist_ok=True)
101
  api.upload_folder(folder_path="./ni_v1_model", repo_id=r, token=t)
102
- return "✅ 成功したぜ。"
103
- except Exception as e: return f"❌ エラー: {e}"
 
104
 
105
  pub_btn.click(upload, [repo_id, user_token], status)
106
 
107
  if __name__ == "__main__":
 
108
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
6
  from huggingface_hub import HfApi
7
 
8
  # ---------------------------------------------------------
9
+ # 1. モデル準備 (Merge & Load)
10
  # ---------------------------------------------------------
11
  def prepare_model():
12
  model_path = "./ni_v1_model"
13
  token = os.getenv("HF_TOKEN")
14
 
15
  if not os.path.exists(model_path):
16
+ print("🚀 NI-v1 マージプロセス開始...")
17
  env = os.environ.copy()
18
  if token:
19
  env["HF_TOKEN"] = token
20
+ # 認証を確実に通す
21
  subprocess.run(["huggingface-cli", "login", "--token", token], check=True)
22
 
23
  try:
24
+ # config.yaml を使ってマージを実行
25
  subprocess.run(
26
  ["mergekit-yaml", "config.yaml", model_path, "--allow-crimes"],
27
  check=True,
28
  env=env
29
  )
30
+ print("✨ マージ成功だぜ!")
31
+ except Exception as e:
32
+ print(f"❌ マージ失敗: {e}")
33
+ raise e
34
 
35
+ print("🧠 NI-v1 モデルをロード中...")
36
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
37
  model = AutoModelForCausalLM.from_pretrained(
38
  model_path,
 
42
  )
43
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
44
 
45
+ # グローバル初期化
46
  try:
47
  pipe = prepare_model()
48
  except Exception as e:
49
+ print(f"起動エラー: {e}")
50
  pipe = None
51
 
52
  # ---------------------------------------------------------
53
+ # 2. 推論ロジック
54
  # ---------------------------------------------------------
55
  def chat_fn(message):
56
+ if pipe is None: return "知能ユニットが応答不能だぜ。"
57
+
58
+ # ユーザー指定のモデルに合わせたプロンプト
59
  prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
60
  outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
61
+
62
+ # 応答部分だけ抽出
63
+ response = outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
64
+ return response
65
 
66
  # ---------------------------------------------------------
67
+ # 3. UI (Gradio 5.x 辞書型/messages形式に完全準拠)
68
  # ---------------------------------------------------------
69
  with gr.Blocks(title="NI-v1.0") as demo:
70
  gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
71
+ gr.Markdown("Maverick-17B と Gemma-4-E4B を融合。")
72
 
73
  with gr.Tab("チャット"):
74
+ # エラー回避の核心:type="messages" を明示し、辞書型を強制
75
  chatbot = gr.Chatbot(type="messages")
76
+ msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。", container=False, scale=7)
77
  with gr.Row():
78
  send = gr.Button("送信", variant="primary")
79
  clear = gr.Button("クリア")
 
82
  if not message:
83
  return "", chat_history
84
 
85
+ # ユーザー側の発言を「辞書形式」で追加
86
  chat_history.append({"role": "user", "content": message})
87
 
88
+ # 回答生成
89
  bot_res = chat_fn(message)
90
 
91
+ # AI側の発言を「辞書形式」で追加
92
  chat_history.append({"role": "assistant", "content": bot_res})
93
 
94
  return "", chat_history
95
 
96
+ # イベント紐付け
97
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
98
  send.click(respond, [msg, chatbot], [msg, chatbot])
99
+ clear.click(lambda: [], None, chatbot, queue=False)
100
 
101
  with gr.Tab("公開"):
102
+ gr.Markdown("### 完成した NI-v1 を Hugging Face にアップロード")
103
+ # リポジトリ名をのっぽの指定に修正
104
  repo_id = gr.Textbox(label="Repo ID", value="noppodev/NoppoIntelligence")
105
+ user_token = gr.Textbox(label="Write Token (HF_TOKEN)", type="password")
106
+ pub_btn = gr.Button("アップロード開始")
107
  status = gr.Textbox(label="Status")
108
 
109
  def upload(r, t):
 
111
  api = HfApi()
112
  api.create_repo(repo_id=r, repo_type="model", exist_ok=True)
113
  api.upload_folder(folder_path="./ni_v1_model", repo_id=r, token=t)
114
+ return "✅ アップロード完了!のっぽ、やったぜ。"
115
+ except Exception as e:
116
+ return f"❌ エラー発生: {e}"
117
 
118
  pub_btn.click(upload, [repo_id, user_token], status)
119
 
120
  if __name__ == "__main__":
121
+ # Docker環境のポート設定
122
  demo.launch(server_name="0.0.0.0", server_port=7860)