makeproject commited on
Commit
a430c46
·
verified ·
1 Parent(s): 57cf567

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -22
app.py CHANGED
@@ -1,34 +1,43 @@
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
- # ✏️ 把那两行替换成下面这样:
10
- model_path = hf_hub_download(
11
- repo_id="eoinedge/edgeai-docs-qwen2.5-coder-0.5b-lora",
12
- filename="edgeai-docs-qwen2.5-coder-0.5b-lora.Q4_K_M.gguf"
13
  )
14
- llm = Llama(model_path=model_path, n_ctx=2048)
15
- print("模型加载成功!")
16
  except Exception as e:
17
- llm = None
18
- print(f"模型加载失败,错误信息: {e}")
19
 
20
  def predict(message, history):
21
- if llm is None:
22
- return "抱歉,服务器模型加载失败,请检查 Hugging Face 上的模型路径与文件名是否正确。"
 
 
 
 
 
 
 
 
 
 
 
23
 
24
- # 按照 Qwen 模型标准 Chat 格式拼接 Prompt
25
- prompt = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
26
- response = llm(prompt, max_tokens=512, stop=["<|im_end|>"])
27
- return response["choices"][0]["text"]
28
 
29
- # 构筑精美的 Gradio 聊天交互界面
30
  gr.ChatInterface(
31
  fn=predict,
32
- title="🤖 Arduino & EdgeAI 网页编程助手",
33
- description="基于 Qwen2.5-Coder-0.5B 微调模型的在线轻量化版本。数据完全在 Hugging Face 独立沙盒内运行。"
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()