Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,65 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import subprocess
|
3 |
|
4 |
# 模型下载链接
|
5 |
model_url = "https://huggingface.co/CMLL/ZhongJing-2-1_8b-GGUF/resolve/main/ZhongJing1_5-1_8b-fp16.gguf"
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
subprocess.run(['pip', 'install', '-U', 'gradio==3.33.1'], check=True)
|
13 |
-
subprocess.run(['pip', 'uninstall', '-y', 'llama-cpp-python'], check=True)
|
14 |
-
os.environ['CMAKE_ARGS'] = "-DLLAMA_CUBLAS=on"
|
15 |
-
os.environ['FORCE_CMAKE'] = "1"
|
16 |
-
subprocess.run(['pip', 'install', 'llama-cpp-python', '--no-cache-dir'], check=True)
|
17 |
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
if __name__ == "__main__":
|
26 |
-
|
27 |
-
model_name = 'ZhongJing1_5-1_8b-fp16.gguf'
|
28 |
-
download_model(model_url, model_name)
|
29 |
-
run_server(model_name)
|
30 |
|
31 |
|
32 |
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
import os
|
4 |
import subprocess
|
5 |
|
6 |
# 模型下载链接
|
7 |
model_url = "https://huggingface.co/CMLL/ZhongJing-2-1_8b-GGUF/resolve/main/ZhongJing1_5-1_8b-fp16.gguf"
|
8 |
|
9 |
+
# 下载模型函数
|
10 |
+
def download_model(url, model_dir="models"):
|
11 |
+
os.makedirs(model_dir, exist_ok=True)
|
12 |
+
model_path = hf_hub_download(repo_id="CMLL/ZhongJing-2-1_8b-GGUF", filename="ZhongJing1_5-1_8b-fp16.gguf", local_dir=model_dir)
|
13 |
+
return model_path
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# 安装环境函数
|
16 |
+
def setup_environment():
|
17 |
+
if not os.path.exists("llama.cpp"):
|
18 |
+
os.system("git clone https://github.com/ggerganov/llama.cpp.git")
|
19 |
+
os.system("cd llama.cpp && mkdir build && cd build && cmake .. && make")
|
20 |
|
21 |
+
model_path = download_model(model_url)
|
22 |
+
|
23 |
+
prompts_dir = "llama.cpp/prompts"
|
24 |
+
os.makedirs(prompts_dir, exist_ok=True)
|
25 |
+
with open(os.path.join(prompts_dir, "TcmChat.txt"), "w") as f:
|
26 |
+
f.write("You are a helpful TCM medical assistant named 仲景中医大语言模型.\n")
|
27 |
+
|
28 |
+
return "Environment setup complete. Model downloaded and prompts file created."
|
29 |
+
|
30 |
+
# 聊天函数
|
31 |
+
def chat_with_model(user_input, history):
|
32 |
+
prompt = f"User: {user_input}\nAssistant:"
|
33 |
+
with open(os.path.join("llama.cpp/prompts", "TcmChat.txt"), "a") as f:
|
34 |
+
f.write(prompt + "\n")
|
35 |
+
|
36 |
+
# 执行命令并捕获输出
|
37 |
+
command = f"./llama.cpp/build/bin/main -m models/ZhongJing1_5-1_8b-fp16.gguf -n 256 --repeat_penalty 1.0 --color -i -r \"User:\" -f llama.cpp/prompts/TcmChat.txt"
|
38 |
+
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
39 |
+
|
40 |
+
response = result.stdout.split("User:")[-1].strip()
|
41 |
+
history.append((user_input, response))
|
42 |
+
return history, history
|
43 |
+
|
44 |
+
# 创建 Gradio 界面
|
45 |
+
with gr.Blocks() as demo:
|
46 |
+
chatbot = gr.Chatbot()
|
47 |
+
state = gr.State([])
|
48 |
+
|
49 |
+
with gr.Row():
|
50 |
+
with gr.Column():
|
51 |
+
setup_btn = gr.Button("Setup Environment")
|
52 |
+
setup_output = gr.Textbox(label="Setup Output")
|
53 |
+
|
54 |
+
with gr.Column():
|
55 |
+
user_input = gr.Textbox(show_label=False, placeholder="Enter your message...")
|
56 |
+
submit_btn = gr.Button("Submit")
|
57 |
+
|
58 |
+
setup_btn.click(setup_environment, outputs=setup_output)
|
59 |
+
submit_btn.click(chat_with_model, [user_input, state], [chatbot, state])
|
60 |
|
61 |
if __name__ == "__main__":
|
62 |
+
demo.launch()
|
|
|
|
|
|
|
63 |
|
64 |
|
65 |
|