Spaces:
Sleeping
Sleeping
继续修改bug,没有回复
Browse files
app.py
CHANGED
|
@@ -6,7 +6,7 @@ import os
|
|
| 6 |
# ----------------------------------------
|
| 7 |
# Global model cache
|
| 8 |
# ----------------------------------------
|
| 9 |
-
loaded_models = {}
|
| 10 |
current_model_name = None
|
| 11 |
|
| 12 |
MODEL_CONFIGS = {
|
|
@@ -15,7 +15,7 @@ MODEL_CONFIGS = {
|
|
| 15 |
"filename": "llama-3.2-1b-instruct.Q4_K_M.gguf"
|
| 16 |
},
|
| 17 |
"3B Model (Datangtang/GGUF3B)": {
|
| 18 |
-
"repo_id": "Datangtang/GGUF3B",
|
| 19 |
"filename": "llama-3.2-3b-instruct.Q4_K_M.gguf"
|
| 20 |
}
|
| 21 |
}
|
|
@@ -27,13 +27,9 @@ MODEL_CONFIGS = {
|
|
| 27 |
def load_model(model_choice):
|
| 28 |
global loaded_models, current_model_name
|
| 29 |
|
| 30 |
-
# Use cache
|
| 31 |
if model_choice in loaded_models:
|
| 32 |
-
print(f"Reusing already loaded model: {model_choice}")
|
| 33 |
-
current_model_name = model_choice
|
| 34 |
return loaded_models[model_choice]
|
| 35 |
|
| 36 |
-
print(f"Downloading model: {model_choice}")
|
| 37 |
cfg = MODEL_CONFIGS[model_choice]
|
| 38 |
|
| 39 |
model_path = hf_hub_download(
|
|
@@ -43,9 +39,6 @@ def load_model(model_choice):
|
|
| 43 |
token=os.environ["HF_TOKEN"]
|
| 44 |
)
|
| 45 |
|
| 46 |
-
print(f"Model downloaded to: {model_path}")
|
| 47 |
-
print("Loading GGUF model into memory...")
|
| 48 |
-
|
| 49 |
llm = Llama(
|
| 50 |
model_path=model_path,
|
| 51 |
n_ctx=1024,
|
|
@@ -59,29 +52,31 @@ def load_model(model_choice):
|
|
| 59 |
|
| 60 |
loaded_models[model_choice] = llm
|
| 61 |
current_model_name = model_choice
|
| 62 |
-
|
| 63 |
-
print("Model loaded successfully!")
|
| 64 |
return llm
|
| 65 |
|
| 66 |
|
| 67 |
# ----------------------------------------
|
| 68 |
-
# Chat function (
|
| 69 |
# ----------------------------------------
|
| 70 |
-
def chat(
|
|
|
|
| 71 |
llm = load_model(model_choice)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
conversation = "System: You are a helpful assistant.\n"
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
if assistant:
|
| 80 |
-
conversation += f"Assistant: {assistant}\n"
|
| 81 |
|
| 82 |
-
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
| 85 |
response = llm(
|
| 86 |
conversation,
|
| 87 |
max_tokens=128,
|
|
@@ -89,60 +84,44 @@ def chat(message, history, model_choice):
|
|
| 89 |
top_p=0.9,
|
| 90 |
top_k=40,
|
| 91 |
repeat_penalty=1.1,
|
| 92 |
-
stop=["User:", "Assistant:"]
|
| 93 |
-
echo=False,
|
| 94 |
)
|
| 95 |
|
| 96 |
return response["choices"][0]["text"].strip()
|
| 97 |
|
| 98 |
|
| 99 |
# ----------------------------------------
|
| 100 |
-
# Gradio UI (
|
| 101 |
# ----------------------------------------
|
| 102 |
with gr.Blocks() as demo:
|
| 103 |
|
| 104 |
-
gr.Markdown("# 🦙 Datangtang GGUF Model Demo")
|
| 105 |
-
gr.Markdown("Switch between **1B** and **3B** GGUF models in real-time.")
|
| 106 |
|
| 107 |
-
# Dropdown for model selection
|
| 108 |
model_choice = gr.Dropdown(
|
| 109 |
label="Select Model",
|
| 110 |
choices=list(MODEL_CONFIGS.keys()),
|
| 111 |
value="1B Model (Datangtang/GGUF1B)",
|
| 112 |
)
|
| 113 |
|
| 114 |
-
chatbot = gr.Chatbot()
|
| 115 |
-
msg_box = gr.Textbox(label="
|
| 116 |
|
| 117 |
-
#
|
| 118 |
-
def
|
| 119 |
-
|
| 120 |
-
return
|
| 121 |
|
| 122 |
-
#
|
| 123 |
-
def
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
return history
|
| 128 |
|
| 129 |
-
# Wire functions
|
| 130 |
msg_box.submit(
|
| 131 |
-
|
| 132 |
-
[msg_box, chatbot],
|
| 133 |
-
[chatbot, msg_box]
|
| 134 |
).then(
|
| 135 |
-
|
| 136 |
-
[chatbot, model_choice],
|
| 137 |
-
chatbot
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
model_choice.change(
|
| 141 |
-
fn=lambda x: f"🔄 Switched to: {x}",
|
| 142 |
-
inputs=[model_choice],
|
| 143 |
-
outputs=[],
|
| 144 |
)
|
| 145 |
|
| 146 |
|
| 147 |
-
|
| 148 |
-
demo.launch()
|
|
|
|
| 6 |
# ----------------------------------------
|
| 7 |
# Global model cache
|
| 8 |
# ----------------------------------------
|
| 9 |
+
loaded_models = {}
|
| 10 |
current_model_name = None
|
| 11 |
|
| 12 |
MODEL_CONFIGS = {
|
|
|
|
| 15 |
"filename": "llama-3.2-1b-instruct.Q4_K_M.gguf"
|
| 16 |
},
|
| 17 |
"3B Model (Datangtang/GGUF3B)": {
|
| 18 |
+
"repo_id": "Datangtang/GGUF3B",
|
| 19 |
"filename": "llama-3.2-3b-instruct.Q4_K_M.gguf"
|
| 20 |
}
|
| 21 |
}
|
|
|
|
| 27 |
def load_model(model_choice):
|
| 28 |
global loaded_models, current_model_name
|
| 29 |
|
|
|
|
| 30 |
if model_choice in loaded_models:
|
|
|
|
|
|
|
| 31 |
return loaded_models[model_choice]
|
| 32 |
|
|
|
|
| 33 |
cfg = MODEL_CONFIGS[model_choice]
|
| 34 |
|
| 35 |
model_path = hf_hub_download(
|
|
|
|
| 39 |
token=os.environ["HF_TOKEN"]
|
| 40 |
)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
llm = Llama(
|
| 43 |
model_path=model_path,
|
| 44 |
n_ctx=1024,
|
|
|
|
| 52 |
|
| 53 |
loaded_models[model_choice] = llm
|
| 54 |
current_model_name = model_choice
|
|
|
|
|
|
|
| 55 |
return llm
|
| 56 |
|
| 57 |
|
| 58 |
# ----------------------------------------
|
| 59 |
+
# Chat function (Gradio 4.x message format)
|
| 60 |
# ----------------------------------------
|
| 61 |
+
def chat(messages, model_choice):
|
| 62 |
+
|
| 63 |
llm = load_model(model_choice)
|
| 64 |
|
| 65 |
+
# Construct conversation
|
| 66 |
conversation = "System: You are a helpful assistant.\n"
|
| 67 |
|
| 68 |
+
for msg in messages[-3:]:
|
| 69 |
+
role = msg["role"]
|
| 70 |
+
text = msg["content"]
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
if role == "user":
|
| 73 |
+
conversation += f"User: {text}\n"
|
| 74 |
+
elif role == "assistant":
|
| 75 |
+
conversation += f"Assistant: {text}\n"
|
| 76 |
|
| 77 |
+
conversation += "Assistant:"
|
| 78 |
+
|
| 79 |
+
# LLM output
|
| 80 |
response = llm(
|
| 81 |
conversation,
|
| 82 |
max_tokens=128,
|
|
|
|
| 84 |
top_p=0.9,
|
| 85 |
top_k=40,
|
| 86 |
repeat_penalty=1.1,
|
| 87 |
+
stop=["User:", "Assistant:"]
|
|
|
|
| 88 |
)
|
| 89 |
|
| 90 |
return response["choices"][0]["text"].strip()
|
| 91 |
|
| 92 |
|
| 93 |
# ----------------------------------------
|
| 94 |
+
# Gradio UI (Gradio 4.x messages format)
|
| 95 |
# ----------------------------------------
|
| 96 |
with gr.Blocks() as demo:
|
| 97 |
|
| 98 |
+
gr.Markdown("# 🦙 Datangtang GGUF Model Demo (Gradio 4.x Compatible)")
|
|
|
|
| 99 |
|
|
|
|
| 100 |
model_choice = gr.Dropdown(
|
| 101 |
label="Select Model",
|
| 102 |
choices=list(MODEL_CONFIGS.keys()),
|
| 103 |
value="1B Model (Datangtang/GGUF1B)",
|
| 104 |
)
|
| 105 |
|
| 106 |
+
chatbot = gr.Chatbot(label="Chat", type="messages")
|
| 107 |
+
msg_box = gr.Textbox(label="Message")
|
| 108 |
|
| 109 |
+
# User sends message
|
| 110 |
+
def add_user_message(user_msg, messages):
|
| 111 |
+
messages = messages + [{"role": "user", "content": user_msg}]
|
| 112 |
+
return messages, ""
|
| 113 |
|
| 114 |
+
# Bot replies
|
| 115 |
+
def add_bot_reply(messages, model_choice):
|
| 116 |
+
reply = chat(messages, model_choice)
|
| 117 |
+
messages = messages + [{"role": "assistant", "content": reply}]
|
| 118 |
+
return messages
|
|
|
|
| 119 |
|
|
|
|
| 120 |
msg_box.submit(
|
| 121 |
+
add_user_message, [msg_box, chatbot], [chatbot, msg_box]
|
|
|
|
|
|
|
| 122 |
).then(
|
| 123 |
+
add_bot_reply, [chatbot, model_choice], chatbot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
)
|
| 125 |
|
| 126 |
|
| 127 |
+
demo.launch()
|
|
|