Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from huggingface_hub import HfApi
|
|
| 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,17 +15,19 @@ def prepare_model():
|
|
| 14 |
env = os.environ.copy()
|
| 15 |
if token:
|
| 16 |
env["HF_TOKEN"] = token
|
|
|
|
|
|
|
| 17 |
|
| 18 |
try:
|
| 19 |
-
#
|
| 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 as e:
|
| 27 |
-
print(f"❌ マージエラー。")
|
| 28 |
raise e
|
| 29 |
|
| 30 |
print("🧠 NI-v1 ロード中...")
|
|
@@ -37,7 +40,6 @@ def prepare_model():
|
|
| 37 |
)
|
| 38 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 39 |
|
| 40 |
-
# グローバルでパイプラインを初期化
|
| 41 |
try:
|
| 42 |
pipe = prepare_model()
|
| 43 |
except Exception as e:
|
|
@@ -45,33 +47,27 @@ except Exception as e:
|
|
| 45 |
pipe = None
|
| 46 |
|
| 47 |
def chat_fn(message, history):
|
| 48 |
-
if pipe is None: return "知能ユニット
|
| 49 |
-
|
| 50 |
-
# プロンプト構築
|
| 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 |
-
|
| 54 |
-
# 応答を抽出
|
| 55 |
-
response = outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
|
| 56 |
-
return response
|
| 57 |
|
| 58 |
-
# ---
|
| 59 |
with gr.Blocks(title="NI-v1.0") as demo:
|
| 60 |
gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
|
| 61 |
|
| 62 |
with gr.Tab("チャット"):
|
| 63 |
-
#
|
| 64 |
-
chatbot = gr.Chatbot(
|
| 65 |
msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。")
|
| 66 |
with gr.Row():
|
| 67 |
send = gr.Button("送信", variant="primary")
|
| 68 |
clear = gr.Button("クリア")
|
| 69 |
|
| 70 |
def respond(message, chat_history):
|
| 71 |
-
#
|
| 72 |
-
chat_history.append({"role": "user", "content": message})
|
| 73 |
bot_message = chat_fn(message, chat_history)
|
| 74 |
-
chat_history.append(
|
| 75 |
return "", chat_history
|
| 76 |
|
| 77 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
|
@@ -79,7 +75,7 @@ with gr.Blocks(title="NI-v1.0") as demo:
|
|
| 79 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 80 |
|
| 81 |
with gr.Tab("公開"):
|
| 82 |
-
repo_id = gr.Textbox(label="Repo ID")
|
| 83 |
user_token = gr.Textbox(label="Write Token", type="password")
|
| 84 |
pub_btn = gr.Button("アップロード")
|
| 85 |
status = gr.Textbox(label="Status")
|
|
@@ -89,11 +85,10 @@ with gr.Blocks(title="NI-v1.0") as demo:
|
|
| 89 |
api = HfApi()
|
| 90 |
api.create_repo(repo_id=r, repo_type="model", exist_ok=True)
|
| 91 |
api.upload_folder(folder_path="./ni_v1_model", repo_id=r, token=t)
|
| 92 |
-
return "✅ 完了
|
| 93 |
except Exception as e: return f"❌ エラー: {e}"
|
| 94 |
|
| 95 |
pub_btn.click(upload, [repo_id, user_token], status)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
-
# Dockerコンテナ内で外部からアクセスできるように設定
|
| 99 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 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 |
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 ロード中...")
|
|
|
|
| 40 |
)
|
| 41 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 42 |
|
|
|
|
| 43 |
try:
|
| 44 |
pipe = prepare_model()
|
| 45 |
except Exception as 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():
|
| 64 |
send = gr.Button("送信", variant="primary")
|
| 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 |
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")
|
|
|
|
| 85 |
api = HfApi()
|
| 86 |
api.create_repo(repo_id=r, repo_type="model", exist_ok=True)
|
| 87 |
api.upload_folder(folder_path="./ni_v1_model", repo_id=r, token=t)
|
| 88 |
+
return "✅ 完了。"
|
| 89 |
except Exception as e: return f"❌ エラー: {e}"
|
| 90 |
|
| 91 |
pub_btn.click(upload, [repo_id, user_token], status)
|
| 92 |
|
| 93 |
if __name__ == "__main__":
|
|
|
|
| 94 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|