noppodev commited on
Commit
34098c3
·
verified ·
1 Parent(s): 43fd933

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -13
app.py CHANGED
@@ -7,7 +7,6 @@ from huggingface_hub import HfApi
7
 
8
  def prepare_model():
9
  model_path = "./ni_v1_model"
10
- # SpaceのSettingsで設定したHF_TOKENを確実に取得
11
  token = os.getenv("HF_TOKEN")
12
 
13
  if not os.path.exists(model_path):
@@ -15,20 +14,17 @@ def prepare_model():
15
  env = os.environ.copy()
16
  if token:
17
  env["HF_TOKEN"] = token
18
- # mergekitが認証を通せるようにログイン状態を作る
19
  subprocess.run(["huggingface-cli", "login", "--token", token], check=True)
20
 
21
  try:
22
- # allow-crimesをつけつつ、正確なモデル名でマージ
23
  subprocess.run(
24
  ["mergekit-yaml", "config.yaml", model_path, "--allow-crimes"],
25
  check=True,
26
  env=env
27
  )
28
  print("✨ マージ成功。")
29
- except subprocess.CalledProcessError as e:
30
- print(f"❌ マージエラー。モデルのアクセス権(Gate)を確認してくれ。")
31
- raise e
32
 
33
  print("🧠 NI-v1 ロード中...")
34
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
@@ -46,18 +42,18 @@ except Exception as e:
46
  print(f"起動エラー: {e}")
47
  pipe = None
48
 
49
- def chat_fn(message, history):
50
  if pipe is None: return "知能ユニット未起動。"
51
  prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
52
  outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
53
  return outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
54
 
55
- # --- GUI (古いGradioでも動くリスト形式) ---
56
  with gr.Blocks(title="NI-v1.0") as demo:
57
  gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
58
 
59
  with gr.Tab("チャット"):
60
- # エラーの原因だった type="messages" を削除
61
  chatbot = gr.Chatbot()
62
  msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。")
63
  with gr.Row():
@@ -65,9 +61,17 @@ with gr.Blocks(title="NI-v1.0") as demo:
65
  clear = gr.Button("クリア")
66
 
67
  def respond(message, chat_history):
68
- # リスト形式 (old style) で履歴を管理
69
- bot_message = chat_fn(message, chat_history)
70
- chat_history.append((message, bot_message))
 
 
 
 
 
 
 
 
71
  return "", chat_history
72
 
73
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
@@ -75,7 +79,7 @@ with gr.Blocks(title="NI-v1.0") as demo:
75
  clear.click(lambda: None, None, chatbot, queue=False)
76
 
77
  with gr.Tab("公開"):
78
- repo_id = gr.Textbox(label="Repo ID", placeholder="noppodev/NoppoIntelligence")
79
  user_token = gr.Textbox(label="Write Token", type="password")
80
  pub_btn = gr.Button("アップロード")
81
  status = gr.Textbox(label="Status")
 
7
 
8
  def prepare_model():
9
  model_path = "./ni_v1_model"
 
10
  token = os.getenv("HF_TOKEN")
11
 
12
  if not os.path.exists(model_path):
 
14
  env = os.environ.copy()
15
  if token:
16
  env["HF_TOKEN"] = token
 
17
  subprocess.run(["huggingface-cli", "login", "--token", token], check=True)
18
 
19
  try:
 
20
  subprocess.run(
21
  ["mergekit-yaml", "config.yaml", model_path, "--allow-crimes"],
22
  check=True,
23
  env=env
24
  )
25
  print("✨ マージ成功。")
26
+ except subprocess.CalledProcessError:
27
+ raise RuntimeError("マージエラー。設定を確認してくれ。")
 
28
 
29
  print("🧠 NI-v1 ロード中...")
30
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
 
42
  print(f"起動エラー: {e}")
43
  pipe = None
44
 
45
+ def chat_fn(message):
46
  if pipe is None: return "知能ユニット未起動。"
47
  prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
48
  outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
49
  return outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
50
 
51
+ # --- どんなGradioのバージョンでも動かすための魔法 ---
52
  with gr.Blocks(title="NI-v1.0") as demo:
53
  gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
54
 
55
  with gr.Tab("チャット"):
56
+ # バージョン問わず動くように型指定なしで初期化
57
  chatbot = gr.Chatbot()
58
  msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。")
59
  with gr.Row():
 
61
  clear = gr.Button("クリア")
62
 
63
  def respond(message, chat_history):
64
+ bot_res = chat_fn(message)
65
+
66
+ # --- ここが重要:環境に合わせて履歴の形式を自動変換する ---
67
+ try:
68
+ # 最新版 (辞書型) を試みる
69
+ chat_history.append({"role": "user", "content": message})
70
+ chat_history.append({"role": "assistant", "content": bot_res})
71
+ except:
72
+ # 失敗したら旧型 (リスト型) を使う
73
+ chat_history.append((message, bot_res))
74
+
75
  return "", chat_history
76
 
77
  msg.submit(respond, [msg, chatbot], [msg, chatbot])
 
79
  clear.click(lambda: None, None, chatbot, queue=False)
80
 
81
  with gr.Tab("公開"):
82
+ repo_id = gr.Textbox(label="Repo ID", placeholder="noppo/NI-v1")
83
  user_token = gr.Textbox(label="Write Token", type="password")
84
  pub_btn = gr.Button("アップロード")
85
  status = gr.Textbox(label="Status")