Spaces:
Build error
Build error
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
☕ Coffee Barista v42 — Gradio Space
|
| 3 |
+
Chat-only coffee expert, no tools needed.
|
| 4 |
+
"""
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from llama_cpp import Llama
|
| 7 |
+
|
| 8 |
+
MODEL_URL = "https://huggingface.co/ynanxiu/qwen25-3b-coffee-master-gguf/resolve/main/qwen25_3b_coffee_master_v43_q4km.gguf"
|
| 9 |
+
MODEL_PATH = "qwen25_3b_coffee_master_v43_q4km.gguf"
|
| 10 |
+
|
| 11 |
+
SYSTEM_PROMPT = "你是一个专业咖啡师,拥有丰富的咖啡知识和吧台经验。用自然、亲切的中文回答,像在跟熟客聊天一样。回复简洁、有人情味,1-3句话。"
|
| 12 |
+
|
| 13 |
+
# Download model on startup
|
| 14 |
+
import os
|
| 15 |
+
if not os.path.exists(MODEL_PATH):
|
| 16 |
+
from huggingface_hub import hf_hub_download
|
| 17 |
+
hf_hub_download(repo_id="ynanxiu/qwen25-3b-coffee-master-gguf", filename="qwen25_3b_coffee_master_v42_q4km.gguf", local_dir=".")
|
| 18 |
+
|
| 19 |
+
llm = Llama(model_path=MODEL_PATH, n_ctx=2048, n_threads=4, verbose=False)
|
| 20 |
+
|
| 21 |
+
def chat(message, history):
|
| 22 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 23 |
+
for h in history:
|
| 24 |
+
messages.append({"role": "user", "content": h[0]})
|
| 25 |
+
if h[1]:
|
| 26 |
+
messages.append({"role": "assistant", "content": h[1]})
|
| 27 |
+
messages.append({"role": "user", "content": message})
|
| 28 |
+
|
| 29 |
+
response = llm.create_chat_completion(
|
| 30 |
+
messages=messages,
|
| 31 |
+
temperature=0.7,
|
| 32 |
+
max_tokens=100, # ← 硬截断,≈ 150 汉字
|
| 33 |
+
repeat_penalty=1.15, # ← 防复读机
|
| 34 |
+
stop=["<|im_end|>", "<|im_start|>", "\n\n"], # ← 自然段落处停
|
| 35 |
+
)
|
| 36 |
+
return response["choices"][0]["message"]["content"]
|
| 37 |
+
|
| 38 |
+
demo = gr.ChatInterface(
|
| 39 |
+
fn=chat,
|
| 40 |
+
title="☕ 咖啡大师 Chat",
|
| 41 |
+
description="一个懂咖啡、会聊天的 AI 咖啡师。问我任何咖啡问题!",
|
| 42 |
+
examples=[
|
| 43 |
+
"手冲咖啡水温怎么选?",
|
| 44 |
+
"拿铁和卡布奇诺有什么区别?",
|
| 45 |
+
"新手学咖啡从哪开始?",
|
| 46 |
+
"深烘豆手冲有什么技巧?",
|
| 47 |
+
],
|
| 48 |
+
theme="soft",
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
if __name__ == "__main__":
|
| 52 |
+
demo.launch()
|