Spaces:
Sleeping
Sleeping
解决回复格式问题
Browse files
app.py
CHANGED
|
@@ -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,13 @@ MODEL_CONFIGS = {
|
|
| 27 |
def load_model(model_choice):
|
| 28 |
global loaded_models, current_model_name
|
| 29 |
|
|
|
|
| 30 |
if model_choice in loaded_models:
|
| 31 |
print(f"Reusing already loaded model: {model_choice}")
|
| 32 |
current_model_name = model_choice
|
| 33 |
return loaded_models[model_choice]
|
| 34 |
|
| 35 |
print(f"Downloading model: {model_choice}")
|
| 36 |
-
|
| 37 |
cfg = MODEL_CONFIGS[model_choice]
|
| 38 |
|
| 39 |
model_path = hf_hub_download(
|
|
@@ -65,36 +65,23 @@ def load_model(model_choice):
|
|
| 65 |
|
| 66 |
|
| 67 |
# ----------------------------------------
|
| 68 |
-
# Chat function
|
| 69 |
# ----------------------------------------
|
| 70 |
def chat(message, history, model_choice):
|
| 71 |
llm = load_model(model_choice)
|
| 72 |
|
| 73 |
-
#
|
| 74 |
conversation = "System: You are a helpful assistant.\n"
|
| 75 |
|
| 76 |
-
#
|
| 77 |
-
for
|
| 78 |
-
|
| 79 |
-
if
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
if role == "user":
|
| 83 |
-
conversation += f"User: {content}\n"
|
| 84 |
-
elif role == "assistant":
|
| 85 |
-
conversation += f"Assistant: {content}\n"
|
| 86 |
-
|
| 87 |
-
# Safety: old tuple format
|
| 88 |
-
elif isinstance(msg, list) or isinstance(msg, tuple):
|
| 89 |
-
human, assistant = msg
|
| 90 |
-
conversation += f"User: {human}\n"
|
| 91 |
-
if assistant:
|
| 92 |
-
conversation += f"Assistant: {assistant}\n"
|
| 93 |
-
|
| 94 |
-
# Add current message
|
| 95 |
conversation += f"User: {message}\nAssistant:"
|
| 96 |
|
| 97 |
-
#
|
| 98 |
response = llm(
|
| 99 |
conversation,
|
| 100 |
max_tokens=128,
|
|
@@ -103,33 +90,51 @@ def chat(message, history, model_choice):
|
|
| 103 |
top_k=40,
|
| 104 |
repeat_penalty=1.1,
|
| 105 |
stop=["User:", "Assistant:"],
|
| 106 |
-
echo=False
|
| 107 |
)
|
| 108 |
|
| 109 |
return response["choices"][0]["text"].strip()
|
| 110 |
|
|
|
|
| 111 |
# ----------------------------------------
|
| 112 |
-
# Gradio UI
|
| 113 |
# ----------------------------------------
|
| 114 |
with gr.Blocks() as demo:
|
| 115 |
|
| 116 |
gr.Markdown("# 🦙 Datangtang GGUF Model Demo")
|
| 117 |
gr.Markdown("Switch between **1B** and **3B** GGUF models in real-time.")
|
| 118 |
|
|
|
|
| 119 |
model_choice = gr.Dropdown(
|
| 120 |
label="Select Model",
|
| 121 |
choices=list(MODEL_CONFIGS.keys()),
|
| 122 |
value="1B Model (Datangtang/GGUF1B)",
|
| 123 |
)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
model_choice.change(
|
|
@@ -140,4 +145,4 @@ with gr.Blocks() as demo:
|
|
| 140 |
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
-
demo.launch()
|
|
|
|
| 15 |
"filename": "llama-3.2-1b-instruct.Q4_K_M.gguf"
|
| 16 |
},
|
| 17 |
"3B Model (Datangtang/GGUF3B)": {
|
| 18 |
+
"repo_id": "Datangtang/GGUF3B", # FIXED typo here
|
| 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 |
+
# 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(
|
|
|
|
| 65 |
|
| 66 |
|
| 67 |
# ----------------------------------------
|
| 68 |
+
# Chat function (works with Chatbot)
|
| 69 |
# ----------------------------------------
|
| 70 |
def chat(message, history, model_choice):
|
| 71 |
llm = load_model(model_choice)
|
| 72 |
|
| 73 |
+
# Build conversation string
|
| 74 |
conversation = "System: You are a helpful assistant.\n"
|
| 75 |
|
| 76 |
+
# Handle Chatbot list-of-lists format
|
| 77 |
+
for human, assistant in history[-3:]:
|
| 78 |
+
conversation += f"User: {human}\n"
|
| 79 |
+
if assistant:
|
| 80 |
+
conversation += f"Assistant: {assistant}\n"
|
| 81 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
conversation += f"User: {message}\nAssistant:"
|
| 83 |
|
| 84 |
+
# Call model
|
| 85 |
response = llm(
|
| 86 |
conversation,
|
| 87 |
max_tokens=128,
|
|
|
|
| 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 (Blocks + Chatbot)
|
| 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="Type your message:")
|
| 116 |
+
|
| 117 |
+
# Add user message to chatbot
|
| 118 |
+
def user_send(message, history):
|
| 119 |
+
history = history + [[message, None]]
|
| 120 |
+
return history, ""
|
| 121 |
+
|
| 122 |
+
# Generate bot reply
|
| 123 |
+
def bot_reply(history, model_name):
|
| 124 |
+
user_msg = history[-1][0]
|
| 125 |
+
bot_msg = chat(user_msg, history[:-1], model_name)
|
| 126 |
+
history[-1][1] = bot_msg
|
| 127 |
+
return history
|
| 128 |
+
|
| 129 |
+
# Wire functions
|
| 130 |
+
msg_box.submit(
|
| 131 |
+
user_send,
|
| 132 |
+
[msg_box, chatbot],
|
| 133 |
+
[chatbot, msg_box]
|
| 134 |
+
).then(
|
| 135 |
+
bot_reply,
|
| 136 |
+
[chatbot, model_choice],
|
| 137 |
+
chatbot
|
| 138 |
)
|
| 139 |
|
| 140 |
model_choice.change(
|
|
|
|
| 145 |
|
| 146 |
|
| 147 |
if __name__ == "__main__":
|
| 148 |
+
demo.launch()
|