Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,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 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
os.system("git clone https://github.com/ggerganov/llama.cpp.git")
|
20 |
-
os.system("cd llama.cpp && mkdir build && cd build && cmake .. && make")
|
21 |
-
|
22 |
-
# 创建 prompts/TcmChat.txt 文件
|
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 |
-
# Gradio 接口
|
29 |
-
def chat_with_model(user_input, history):
|
30 |
-
prompt = f"User: {user_input}\nAssistant:"
|
31 |
-
with open(os.path.join(prompts_dir, "TcmChat.txt"), "a") as f:
|
32 |
-
f.write(prompt + "\n")
|
33 |
-
|
34 |
-
# 执行命令并捕获输出
|
35 |
-
command = f"./llama.cpp/build/bin/main -m {model_path} -n 256 --repeat_penalty 1.0 --color -i -r \"User:\" -f {os.path.join(prompts_dir, 'TcmChat.txt')}"
|
36 |
-
result = subprocess.run(command, shell=True, capture_output=True, text=True)
|
37 |
-
|
38 |
-
response = result.stdout.split("User:")[-1].strip()
|
39 |
-
|
40 |
-
history.append((user_input, response))
|
41 |
-
return history, history
|
42 |
-
|
43 |
-
with gr.Blocks() as demo:
|
44 |
-
chatbot = gr.Chatbot()
|
45 |
-
state = gr.State([])
|
46 |
-
|
47 |
-
with gr.Row():
|
48 |
-
with gr.Column():
|
49 |
-
user_input = gr.Textbox(show_label=False, placeholder="Enter your message...")
|
50 |
-
with gr.Column():
|
51 |
-
submit_btn = gr.Button("Submit")
|
52 |
-
|
53 |
-
submit_btn.click(chat_with_model, [user_input, state], [chatbot, state])
|
54 |
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
57 |
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
|
60 |
|
|
|
|
|
|
|
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 |
+
def install_packages():
|
8 |
+
subprocess.run(['apt-get', '-y', 'install', '-qq', 'aria2'], check=True)
|
9 |
+
subprocess.run(['git', 'clone', '-b', 'V20230828', 'https://github.com/Troyanovsky/text-generation-webui'], check=True)
|
10 |
+
os.chdir('/text-generation-webui')
|
11 |
+
subprocess.run(['pip', 'install', '-r', 'requirements.txt'], check=True)
|
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 |
+
def download_model(model_url, model_name):
|
19 |
+
subprocess.run(['aria2c', '--console-log-level=error', '-c', '-x', '16', '-s', '16', '-k', '1M', model_url, '-d', '/text-generation-webui/models/', '-o', model_name], check=True)
|
20 |
+
|
21 |
+
def run_server(model_name):
|
22 |
+
os.chdir('/text-generation-webui')
|
23 |
+
subprocess.run(['python', 'server.py', '--share', '--n-gpu-layers', '1000000000', '--model', model_name], check=True)
|
24 |
|
25 |
+
if __name__ == "__main__":
|
26 |
+
install_packages()
|
27 |
+
model_name = 'ZhongJing1_5-1_8b-fp16.gguf'
|
28 |
+
download_model(model_url, model_name)
|
29 |
+
run_server(model_name)
|
30 |
|
31 |
|
32 |
|