Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
|
|
|
|
| 4 |
|
| 5 |
-
print("正在从云端下载微调模型,请稍候...")
|
| 6 |
-
# 自动从 Hugging Face 仓库下载微调好的模型(此处以 Arduino 0.5B 模型为例)
|
| 7 |
try:
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
)
|
| 14 |
-
|
| 15 |
-
print("模型加载成功!")
|
| 16 |
except Exception as e:
|
| 17 |
-
|
| 18 |
-
print(f"模型加载失败
|
| 19 |
|
| 20 |
def predict(message, history):
|
| 21 |
-
if
|
| 22 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
response =
|
| 27 |
-
return response
|
| 28 |
|
| 29 |
-
# 构筑
|
| 30 |
gr.ChatInterface(
|
| 31 |
fn=predict,
|
| 32 |
-
title="🤖
|
| 33 |
-
description="
|
| 34 |
).launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
print("正在初始化纯 Python 推理引擎(免编译方案)...")
|
| 5 |
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
+
# 使用 Hugging Face 官方的 pipeline 直接加载 EdgeAI 模型的 GGUF 文件
|
| 8 |
+
pipe = pipeline(
|
| 9 |
+
"text-generation",
|
| 10 |
+
model="eoinedge/edgeai-docs-qwen2.5-coder-0.5b-lora",
|
| 11 |
+
model_kwargs={"gguf_file": "edgeai-docs-qwen2.5-coder-0.5b-lora.Q4_K_M.gguf"}
|
| 12 |
)
|
| 13 |
+
print("EdgeAI 模型加载成功!")
|
|
|
|
| 14 |
except Exception as e:
|
| 15 |
+
pipe = None
|
| 16 |
+
print(f"模型加载失败: {e}")
|
| 17 |
|
| 18 |
def predict(message, history):
|
| 19 |
+
if pipe is None:
|
| 20 |
+
return "服务器模型加载失败,请检查模型路径。"
|
| 21 |
+
|
| 22 |
+
# 🧠 自动构建带有聊天记忆的上下文 Prompt
|
| 23 |
+
prompt = ""
|
| 24 |
+
for user_msg, ai_msg in history:
|
| 25 |
+
prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n<|im_start|>assistant\n{ai_msg}<|im_end|>\n"
|
| 26 |
+
|
| 27 |
+
# 加上当前的最新提问
|
| 28 |
+
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 29 |
+
|
| 30 |
+
# 开始推理
|
| 31 |
+
outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 32 |
|
| 33 |
+
# 提取新生成的文本
|
| 34 |
+
generated_text = outputs[0]["generated_text"]
|
| 35 |
+
response = generated_text[len(prompt):].replace("<|im_end|>", "").strip()
|
| 36 |
+
return response
|
| 37 |
|
| 38 |
+
# 构筑高颜值聊天网页
|
| 39 |
gr.ChatInterface(
|
| 40 |
fn=predict,
|
| 41 |
+
title="🤖 EdgeAI 网页编程助手 (纯净免编译版)",
|
| 42 |
+
description="已完美集成 Edge Impulse 官方文档微调模型,支持上下文对话记忆,由 Hugging Face 独立沙盒托管。"
|
| 43 |
).launch()
|