noppodev commited on
Commit
755fbc6
·
verified ·
1 Parent(s): 6b2ead8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -16
app.py CHANGED
@@ -1,38 +1,49 @@
1
  import os
2
  import subprocess
 
3
  import gradio as gr
4
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
5
  import torch
6
  from huggingface_hub import HfApi
7
 
8
- # ---------------------------------------------------------
9
- # 1. モデル準備 (2026年最新 hf コマンド対応)
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
- # 2026年最新の hf コマンドを使用
21
  try:
22
  subprocess.run(["hf", "auth", "login", "--token", token], check=True)
23
  except:
24
- # 万が一 hf が入ってない場合でも、環境変数だけマージを試みる
25
- print("⚠️ hf auth login 失敗。環境変数のみで続行します。")
26
 
27
  try:
 
 
28
  subprocess.run(
29
- ["mergekit-yaml", "config.yaml", model_path, "--allow-crimes"],
 
 
 
30
  check=True,
31
  env=env
32
  )
33
- print("✨ マージ成功。")
34
  except:
35
- raise RuntimeError("マージ失敗。設定を確認してくれ。")
36
 
37
  print("🧠 NI-v1 ロード中...")
38
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
@@ -44,7 +55,6 @@ def prepare_model():
44
  )
45
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
46
 
47
- # 起動
48
  try:
49
  pipe = prepare_model()
50
  except Exception as e:
@@ -57,21 +67,16 @@ def predict(message, history):
57
  outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
58
  return outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
59
 
60
- # ---------------------------------------------------------
61
- # 2. UI (Gradio バージョン差異を吸収)
62
- # ---------------------------------------------------------
63
  with gr.Blocks(title="NI-v1.0") as demo:
64
  gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
65
 
66
  with gr.Tab("チャット"):
67
- # TypeError 回避: type 引数を使わず、デフォルト設定で起動
68
  gr.ChatInterface(fn=predict)
69
 
70
  with gr.Tab("公開"):
71
- gr.Markdown("### 完成した NI-v1 を Hugging Face にアップロード")
72
  repo_id = gr.Textbox(label="Repo ID", value="noppodev/NoppoIntelligence")
73
  user_token = gr.Textbox(label="Write Token", type="password")
74
- pub_btn = gr.Button("アップロード開始")
75
  status = gr.Textbox(label="Status")
76
 
77
  def upload(r, t):
 
1
  import os
2
  import subprocess
3
+ import shutil
4
  import gradio as gr
5
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
6
  import torch
7
  from huggingface_hub import HfApi
8
 
 
 
 
9
  def prepare_model():
10
  model_path = "./ni_v1_model"
11
+ # Hugging Faceのキャッシュディレクトリ(ここがパンクの元)
12
+ cache_path = os.path.expanduser("~/.cache/huggingface/hub")
13
  token = os.getenv("HF_TOKEN")
14
 
15
  if not os.path.exists(model_path):
16
+ print("🧹 ストレージ確保のため、古い残骸を掃除するぜ...")
17
+ # 以前のマージ失敗作があれば削除
18
+ if os.path.exists(model_path):
19
+ shutil.rmtree(model_path)
20
+
21
+ # もし容量がギリギリならキャッシュも消す(再ダウンロードになるけど背に腹は代えられない)
22
+ # shutil.rmtree(cache_path, ignore_errors=True)
23
+
24
  print("🚀 NI-v1 マージ開始...")
25
  env = os.environ.copy()
26
  if token:
27
  env["HF_TOKEN"] = token
 
28
  try:
29
  subprocess.run(["hf", "auth", "login", "--token", token], check=True)
30
  except:
31
+ print("⚠️ ログインスキップ(環境変数で続行)")
 
32
 
33
  try:
34
+ # --lazy-unpickle: メモリとディスク消費を抑える魔法の引数
35
+ # --low-cpu-mem: さらに負荷を減らす
36
  subprocess.run(
37
+ ["mergekit-yaml", "config.yaml", model_path,
38
+ "--allow-crimes",
39
+ "--lazy-unpickle",
40
+ "--low-cpu-mem"],
41
  check=True,
42
  env=env
43
  )
44
+ print("✨ マージ成功。のっぽ、耐えたぜ!")
45
  except:
46
+ raise RuntimeError("マージ失敗。容量か設定を見直してくれ。")
47
 
48
  print("🧠 NI-v1 ロード中...")
49
  tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
 
55
  )
56
  return pipeline("text-generation", model=model, tokenizer=tokenizer)
57
 
 
58
  try:
59
  pipe = prepare_model()
60
  except Exception as e:
 
67
  outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
68
  return outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
69
 
 
 
 
70
  with gr.Blocks(title="NI-v1.0") as demo:
71
  gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
72
 
73
  with gr.Tab("チャット"):
 
74
  gr.ChatInterface(fn=predict)
75
 
76
  with gr.Tab("公開"):
 
77
  repo_id = gr.Textbox(label="Repo ID", value="noppodev/NoppoIntelligence")
78
  user_token = gr.Textbox(label="Write Token", type="password")
79
+ pub_btn = gr.Button("アップロード")
80
  status = gr.Textbox(label="Status")
81
 
82
  def upload(r, t):