Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,34 +5,31 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
| 5 |
import torch
|
| 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("🚀
|
| 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"❌ マージ
|
| 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,38 +39,28 @@ def prepare_model():
|
|
| 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 |
-
#
|
| 75 |
chatbot = gr.Chatbot(type="messages")
|
| 76 |
-
msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。"
|
| 77 |
with gr.Row():
|
| 78 |
send = gr.Button("送信", variant="primary")
|
| 79 |
clear = gr.Button("クリア")
|
|
@@ -82,28 +69,26 @@ with gr.Blocks(title="NI-v1.0") as demo:
|
|
| 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 |
-
|
| 103 |
-
# リポジトリ名をのっぽの指定に修正
|
| 104 |
repo_id = gr.Textbox(label="Repo ID", value="noppodev/NoppoIntelligence")
|
| 105 |
-
user_token = gr.Textbox(label="Write Token
|
| 106 |
-
pub_btn = gr.Button("アップロード
|
| 107 |
status = gr.Textbox(label="Status")
|
| 108 |
|
| 109 |
def upload(r, t):
|
|
@@ -111,12 +96,10 @@ with gr.Blocks(title="NI-v1.0") as demo:
|
|
| 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)
|
|
|
|
| 5 |
import torch
|
| 6 |
from huggingface_hub import HfApi
|
| 7 |
|
| 8 |
+
# 1. モデルマージ & ロード
|
|
|
|
|
|
|
| 9 |
def prepare_model():
|
| 10 |
model_path = "./ni_v1_model"
|
| 11 |
token = os.getenv("HF_TOKEN")
|
| 12 |
|
| 13 |
if not os.path.exists(model_path):
|
| 14 |
+
print("🚀 マージ開始...")
|
| 15 |
env = os.environ.copy()
|
| 16 |
if token:
|
| 17 |
env["HF_TOKEN"] = token
|
|
|
|
| 18 |
subprocess.run(["huggingface-cli", "login", "--token", token], check=True)
|
| 19 |
|
| 20 |
try:
|
| 21 |
+
# config.yamlを使用してマージ
|
| 22 |
subprocess.run(
|
| 23 |
["mergekit-yaml", "config.yaml", model_path, "--allow-crimes"],
|
| 24 |
check=True,
|
| 25 |
env=env
|
| 26 |
)
|
| 27 |
+
print("✨ マージ成功。")
|
| 28 |
except Exception as e:
|
| 29 |
+
print(f"❌ マージエラー: {e}")
|
| 30 |
raise e
|
| 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 |
)
|
| 40 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 41 |
|
| 42 |
+
# 起動
|
| 43 |
try:
|
| 44 |
pipe = prepare_model()
|
| 45 |
except Exception as e:
|
| 46 |
print(f"起動エラー: {e}")
|
| 47 |
pipe = None
|
| 48 |
|
| 49 |
+
# 2. 推論
|
|
|
|
|
|
|
| 50 |
def chat_fn(message):
|
| 51 |
+
if pipe is None: return "知能ユニット未起動だぜ。"
|
|
|
|
|
|
|
| 52 |
prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 53 |
outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 54 |
+
return outputs[0]['generated_text'].split("assistant\n")[-1].replace("<|im_end|>", "")
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# 3. UI (Gradio 5.x 仕様に完全準拠)
|
|
|
|
|
|
|
| 57 |
with gr.Blocks(title="NI-v1.0") as demo:
|
| 58 |
gr.Markdown("# 🤖 Noppo-Intelligence v1.0")
|
|
|
|
| 59 |
|
| 60 |
with gr.Tab("チャット"):
|
| 61 |
+
# ログで要求されている通り、辞書型(messages)を明示的に指定
|
| 62 |
chatbot = gr.Chatbot(type="messages")
|
| 63 |
+
msg = gr.Textbox(placeholder="最新モデルの力を見せてくれ。")
|
| 64 |
with gr.Row():
|
| 65 |
send = gr.Button("送信", variant="primary")
|
| 66 |
clear = gr.Button("クリア")
|
|
|
|
| 69 |
if not message:
|
| 70 |
return "", chat_history
|
| 71 |
|
| 72 |
+
# ユーザーの入力を「辞書形式」で追加
|
| 73 |
chat_history.append({"role": "user", "content": message})
|
| 74 |
|
| 75 |
+
# 応答生成
|
| 76 |
bot_res = chat_fn(message)
|
| 77 |
|
| 78 |
+
# AIの回答を「辞書形式」で追加
|
| 79 |
chat_history.append({"role": "assistant", "content": bot_res})
|
| 80 |
|
| 81 |
return "", chat_history
|
| 82 |
|
|
|
|
| 83 |
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 84 |
send.click(respond, [msg, chatbot], [msg, chatbot])
|
| 85 |
clear.click(lambda: [], None, chatbot, queue=False)
|
| 86 |
|
| 87 |
with gr.Tab("公開"):
|
| 88 |
+
# のっぽの指定通り noppodev/NoppoIntelligence に修正
|
|
|
|
| 89 |
repo_id = gr.Textbox(label="Repo ID", value="noppodev/NoppoIntelligence")
|
| 90 |
+
user_token = gr.Textbox(label="Write Token", type="password")
|
| 91 |
+
pub_btn = gr.Button("アップロード")
|
| 92 |
status = gr.Textbox(label="Status")
|
| 93 |
|
| 94 |
def upload(r, t):
|
|
|
|
| 96 |
api = HfApi()
|
| 97 |
api.create_repo(repo_id=r, repo_type="model", exist_ok=True)
|
| 98 |
api.upload_folder(folder_path="./ni_v1_model", repo_id=r, token=t)
|
| 99 |
+
return "✅ 完了したぜ。"
|
| 100 |
+
except Exception as e: return f"❌ エラー: {e}"
|
|
|
|
| 101 |
|
| 102 |
pub_btn.click(upload, [repo_id, user_token], status)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
|
|
|
| 105 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|