Spaces:
				
			
			
	
			
			
		Runtime error
		
	
	
	
			
			
	
	
	
	
		
		
		Runtime error
		
	File size: 2,589 Bytes
			
			| 7a3aac9 cdddf91 550b1f1 f2ae05b ed0a7ea 08bcec8 f2ae05b 7a3aac9 08bcec8 7a3aac9 f2ae05b 7a3aac9 d7810f3 7a3aac9 e75e07f 7a3aac9 7cf2fb1 7a3aac9 a90c92b d7810f3 7a3aac9 44ac364 f2ae05b cdddf91 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import gradio as gr
from huggingface_hub import hf_hub_download
import os
import subprocess
# 模型下载链接
model_url = "https://huggingface.co/CMLL/ZhongJing-2-1_8b-GGUF/resolve/main/ZhongJing1_5-1_8b-q4_0.gguf"
# 下载模型函数
def download_model(url, model_dir="models"):
    os.makedirs(model_dir, exist_ok=True)
    model_path = hf_hub_download(repo_id="CMLL/ZhongJing-2-1_8b-GGUF", filename="ZhongJing1_5-1_8b-q4_0.gguf", local_dir=model_dir)
    return model_path
# 安装环境函数
def setup_environment():
    if not os.path.exists("llama.cpp"):
        os.system("git clone https://github.com/ggerganov/llama.cpp.git")
        os.system("cd llama.cpp && mkdir build && cd build && cmake .. && make")
    model_path = download_model(model_url)
    
    prompts_dir = "llama.cpp/prompts"
    os.makedirs(prompts_dir, exist_ok=True)
    with open(os.path.join(prompts_dir, "TcmChat.txt"), "w") as f:
        f.write("You are a helpful TCM medical assistant named 仲景中医大语言模型.\n")
    
    return "Environment setup complete. Model downloaded and prompts file created."
# 聊天函数
def chat_with_model(user_input, history):
    prompt = f"User: {user_input}\nAssistant:"
    with open(os.path.join("llama.cpp/prompts", "TcmChat.txt"), "a") as f:
        f.write(prompt + "\n")
    # 执行命令并捕获输出
    command = f"./llama.cpp/build/bin/main -m models/ZhongJing1_5-1_8b-q4_0.gguf -n 256 --repeat_penalty 1.0 --color -i -r \"User:\" -f llama.cpp/prompts/chat-with-bob.txt"
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    
    # 解析输出
    output_lines = result.stdout.split("\n")
    response = ""
    capture = False
    for line in output_lines:
        if "User:" in line:
            capture = True
        if capture:
            response += line + "\n"
    
    response = response.strip()
    history.append((user_input, response))
    return history, history
# 创建 Gradio 界面
with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    state = gr.State([])
    with gr.Row():
        with gr.Column():
            setup_btn = gr.Button("Setup Environment")
            setup_output = gr.Textbox(label="Setup Output")
        
        with gr.Column():
            user_input = gr.Textbox(show_label=False, placeholder="Enter your message...")
            submit_btn = gr.Button("Submit")
    
    setup_btn.click(setup_environment, outputs=setup_output)
    submit_btn.click(chat_with_model, [user_input, state], [chatbot, state])
if __name__ == "__main__":
    demo.launch()
 | 
