Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# 配置:使用3B以下量化模型(CPU友好)
|
| 7 |
+
MODEL_REPO = "Qwen/Qwen2.5-1.5B-Instruct-GGUF"
|
| 8 |
+
MODEL_FILENAME = "qwen2.5-1_5b-instruct-q4_k_m.gguf"
|
| 9 |
+
|
| 10 |
+
@st.cache_resource
|
| 11 |
+
def load_model():
|
| 12 |
+
"""下载并加载GGUF模型(首次启动自动下载)"""
|
| 13 |
+
model_path = hf_hub_download(
|
| 14 |
+
repo_id=MODEL_REPO,
|
| 15 |
+
filename=MODEL_FILENAME,
|
| 16 |
+
local_dir="models"
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# CPU优化配置:限制线程数避免OOM
|
| 20 |
+
llm = Llama(
|
| 21 |
+
model_path=model_path,
|
| 22 |
+
n_ctx=2048, # 上下文长度
|
| 23 |
+
n_threads=4, # CPU线程数(Spaces通常4核)
|
| 24 |
+
n_gpu_layers=0, # 0=纯CPU推理
|
| 25 |
+
verbose=False
|
| 26 |
+
)
|
| 27 |
+
return llm
|
| 28 |
+
|
| 29 |
+
# Streamlit UI
|
| 30 |
+
st.title("🦙 本地LLM (CPU-only)")
|
| 31 |
+
st.caption("基于Qwen2.5-1.5B量化模型 · 无外部API调用")
|
| 32 |
+
|
| 33 |
+
if "llm" not in st.session_state:
|
| 34 |
+
with st.spinner("⏳ 首次加载模型(约2-3分钟)..."):
|
| 35 |
+
st.session_state.llm = load_model()
|
| 36 |
+
st.success("✅ 模型加载完成!")
|
| 37 |
+
|
| 38 |
+
# 聊天界面
|
| 39 |
+
if "messages" not in st.session_state:
|
| 40 |
+
st.session_state.messages = []
|
| 41 |
+
|
| 42 |
+
for message in st.session_state.messages:
|
| 43 |
+
with st.chat_message(message["role"]):
|
| 44 |
+
st.markdown(message["content"])
|
| 45 |
+
|
| 46 |
+
if prompt := st.chat_input("输入您的问题..."):
|
| 47 |
+
# 显示用户消息
|
| 48 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 49 |
+
with st.chat_message("user"):
|
| 50 |
+
st.markdown(prompt)
|
| 51 |
+
|
| 52 |
+
# 生成回复(CPU推理较慢,需显示加载状态)
|
| 53 |
+
with st.chat_message("assistant"):
|
| 54 |
+
message_placeholder = st.empty()
|
| 55 |
+
full_response = ""
|
| 56 |
+
|
| 57 |
+
# Qwen2.5的对话模板
|
| 58 |
+
messages = [
|
| 59 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
| 60 |
+
*[
|
| 61 |
+
{"role": m["role"], "content": m["content"]}
|
| 62 |
+
for m in st.session_state.messages
|
| 63 |
+
]
|
| 64 |
+
]
|
| 65 |
+
|
| 66 |
+
# 流式生成
|
| 67 |
+
for chunk in st.session_state.llm.create_chat_completion(
|
| 68 |
+
messages=messages,
|
| 69 |
+
max_tokens=512,
|
| 70 |
+
temperature=0.7,
|
| 71 |
+
stream=True
|
| 72 |
+
):
|
| 73 |
+
if "content" in chunk["choices"][0]["delta"]:
|
| 74 |
+
full_response += chunk["choices"][0]["delta"]["content"]
|
| 75 |
+
message_placeholder.markdown(full_response + "▌")
|
| 76 |
+
|
| 77 |
+
message_placeholder.markdown(full_response)
|
| 78 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|