Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +45 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
from huggingface_hub import hf_hub_download
|
| 4 |
+
|
| 5 |
+
# 1. 下载模型(针对 Qwen 官方 GGUF 仓库的命名规范)
|
| 6 |
+
# 建议使用 Q4_K_M 版本,平衡性能与 16GB 内存限制
|
| 7 |
+
model_path = hf_hub_download(
|
| 8 |
+
repo_id="Qwen/Qwen2.5-Coder-7B-Instruct-GGUF",
|
| 9 |
+
filename="qwen2.5-coder-7b-instruct-q4_k_m.gguf"
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
# 2. 初始化模型
|
| 13 |
+
llm = Llama(
|
| 14 |
+
model_path=model_path,
|
| 15 |
+
n_ctx=4096, # 运维场景通常需要处理较长日志,设置为 4k
|
| 16 |
+
n_threads=2 # 对应免费 CPU 的核数
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
def respond(message, history):
|
| 20 |
+
system_prompt = "你是一位资深大数据运维专家。请为用户提供 Hadoop/Spark/Flink 等组件的代码调优、故障排查建议或 Shell 脚本。"
|
| 21 |
+
|
| 22 |
+
# 构造对话历史
|
| 23 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 24 |
+
for user_msg, assistant_msg in history:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 27 |
+
messages.append({"role": "user", "content": message})
|
| 28 |
+
|
| 29 |
+
# 流式生成
|
| 30 |
+
response_text = ""
|
| 31 |
+
for chunk in llm.create_chat_completion(messages=messages, stream=True):
|
| 32 |
+
delta = chunk['choices'][0]['delta']
|
| 33 |
+
if 'content' in delta:
|
| 34 |
+
response_text += delta['content']
|
| 35 |
+
yield response_text
|
| 36 |
+
|
| 37 |
+
# 3. 启动 Gradio 界面
|
| 38 |
+
demo = gr.ChatInterface(
|
| 39 |
+
fn=respond,
|
| 40 |
+
title="BigData Ops Copilot (Qwen2.5-Coder-7B)",
|
| 41 |
+
examples=["如何优化 Spark 任务的内存分配?", "写一个监控 Kafka 消费延迟的 Python 脚本"]
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
llama-cpp-python
|
| 2 |
+
gradio
|
| 3 |
+
huggingface_hub
|