import gradio as gr from huggingface_hub import hf_hub_download from llama_cpp import Llama print("正在从云端下载微调模型,请稍候...") # 自动从 Hugging Face 仓库下载微调好的模型(此处以 Arduino 0.5B 模型为例) try: # 注意:如果运行时报错,请确认原作者或您克隆的仓库中 GGUF 的确切文件名 # ✏️ 把那两行替换成下面这样: model_path = hf_hub_download( repo_id="eoinedge/edgeai-docs-qwen2.5-coder-0.5b-lora", filename="edgeai-docs-qwen2.5-coder-0.5b-lora.Q4_K_M.gguf" ) llm = Llama(model_path=model_path, n_ctx=2048) print("模型加载成功!") except Exception as e: llm = None print(f"模型加载失败,错误信息: {e}") def predict(message, history): if llm is None: return "抱歉,服务器模型加载失败,请检查 Hugging Face 上的模型路径与文件名是否正确。" # 按照 Qwen 模型的标准 Chat 格式拼接 Prompt prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n" response = llm(prompt, max_tokens=512, stop=["<|im_end|>"]) return response["choices"][0]["text"] # 构筑精美的 Gradio 聊天交互界面 gr.ChatInterface( fn=predict, title="🤖 Arduino & EdgeAI 网页编程助手", description="基于 Qwen2.5-Coder-0.5B 微调模型的在线轻量化版本。数据完全在 Hugging Face 独立沙盒内运行。" ).launch()