Spaces:
Build error
Build error
| """ | |
| ☕ Coffee Barista v42 — Gradio Space | |
| Chat-only coffee expert, no tools needed. | |
| """ | |
| import gradio as gr | |
| from llama_cpp import Llama | |
| MODEL_URL = "https://huggingface.co/ynanxiu/qwen25-3b-coffee-master-gguf/resolve/main/qwen25_3b_coffee_master_v43_q4km.gguf" | |
| MODEL_PATH = "qwen25_3b_coffee_master_v43_q4km.gguf" | |
| SYSTEM_PROMPT = "你是一个专业咖啡师,拥有丰富的咖啡知识和吧台经验。用自然、亲切的中文回答,像在跟熟客聊天一样。回复简洁、有人情味,1-3句话。" | |
| # Download model on startup | |
| import os | |
| if not os.path.exists(MODEL_PATH): | |
| from huggingface_hub import hf_hub_download | |
| hf_hub_download(repo_id="ynanxiu/qwen25-3b-coffee-master-gguf", filename="qwen25_3b_coffee_master_v42_q4km.gguf", local_dir=".") | |
| llm = Llama(model_path=MODEL_PATH, n_ctx=2048, n_threads=4, verbose=False) | |
| def chat(message, history): | |
| messages = [{"role": "system", "content": SYSTEM_PROMPT}] | |
| for h in history: | |
| messages.append({"role": "user", "content": h[0]}) | |
| if h[1]: | |
| messages.append({"role": "assistant", "content": h[1]}) | |
| messages.append({"role": "user", "content": message}) | |
| response = llm.create_chat_completion( | |
| messages=messages, | |
| temperature=0.7, | |
| max_tokens=100, # ← 硬截断,≈ 150 汉字 | |
| repeat_penalty=1.15, # ← 防复读机 | |
| stop=["<|im_end|>", "<|im_start|>", "\n\n"], # ← 自然段落处停 | |
| ) | |
| return response["choices"][0]["message"]["content"] | |
| demo = gr.ChatInterface( | |
| fn=chat, | |
| title="☕ 咖啡大师 Chat", | |
| description="一个懂咖啡、会聊天的 AI 咖啡师。问我任何咖啡问题!", | |
| examples=[ | |
| "手冲咖啡水温怎么选?", | |
| "拿铁和卡布奇诺有什么区别?", | |
| "新手学咖啡从哪开始?", | |
| "深烘豆手冲有什么技巧?", | |
| ], | |
| theme="soft", | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |