Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
+
from llama_cpp import Llama
|
| 4 |
+
|
| 5 |
+
print("正在从云端下载微调模型,请稍候...")
|
| 6 |
+
# 自动从 Hugging Face 仓库下载微调好的模型(此处以 Arduino 0.5B 模型为例)
|
| 7 |
+
try:
|
| 8 |
+
# 注意:如果运行时报错,请确认原作者或您克隆的仓库中 GGUF 的确切文件名
|
| 9 |
+
model_path = hf_hub_download(
|
| 10 |
+
repo_id="eoinedge/arduino-qwen0.5-lora",
|
| 11 |
+
filename="arduino-qwen0.5-lora.Q4_K_M.gguf"
|
| 12 |
+
)
|
| 13 |
+
llm = Llama(model_path=model_path, n_ctx=2048)
|
| 14 |
+
print("模型加载成功!")
|
| 15 |
+
except Exception as e:
|
| 16 |
+
llm = None
|
| 17 |
+
print(f"模型加载失败,错误信息: {e}")
|
| 18 |
+
|
| 19 |
+
def predict(message, history):
|
| 20 |
+
if llm is None:
|
| 21 |
+
return "抱歉,服务器模型加载失败,请检查 Hugging Face 上的模型路径与文件名是否正确。"
|
| 22 |
+
|
| 23 |
+
# 按照 Qwen 模型的标准 Chat 格式拼接 Prompt
|
| 24 |
+
prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 25 |
+
response = llm(prompt, max_tokens=512, stop=["<|im_end|>"])
|
| 26 |
+
return response["choices"][0]["text"]
|
| 27 |
+
|
| 28 |
+
# 构筑精美的 Gradio 聊天交互界面
|
| 29 |
+
gr.ChatInterface(
|
| 30 |
+
fn=predict,
|
| 31 |
+
title="🤖 Arduino & EdgeAI 网页编程助手",
|
| 32 |
+
description="基于 Qwen2.5-Coder-0.5B 微调模型的在线轻量化版本。数据完全在 Hugging Face 独立沙盒内运行。"
|
| 33 |
+
).launch()
|