Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,76 +1,51 @@
|
|
1 |
-
|
|
|
2 |
import os
|
3 |
-
import subprocess
|
4 |
-
|
5 |
-
# 必要なライブラリのインストール
|
6 |
-
subprocess.check_call(["pip", "install", "llama-cpp-python"])
|
7 |
-
subprocess.check_call(["pip", "install", "gradio"])
|
8 |
|
9 |
-
#
|
10 |
model_url = "https://huggingface.co/CMLL/ZhongJing-2-1_8b-GGUF/resolve/main/ZhongJing1_5-1_8b-fp16.gguf"
|
11 |
-
model_path = "ggml-model.gguf"
|
12 |
-
if not os.path.exists(model_path):
|
13 |
-
subprocess.check_call(["wget", model_url, "-O", model_path])
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
-
|
22 |
-
model_path=model_path,
|
23 |
-
n_ctx=2048,
|
24 |
-
# n_gpu_layers=100, # CPUで実行する場合は削除
|
25 |
-
)
|
26 |
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
top_p=0.3,
|
44 |
-
top_k=40,
|
45 |
-
repeat_penalty=1.1,
|
46 |
-
max_tokens=1024,
|
47 |
-
stop=[
|
48 |
-
"ASSISTANT:",
|
49 |
-
"USER:",
|
50 |
-
"SYSTEM:",
|
51 |
-
],
|
52 |
-
stream=True,
|
53 |
-
)
|
54 |
-
for out in output:
|
55 |
-
stream = copy.deepcopy(out)
|
56 |
-
temp += stream["choices"][0]["text"]
|
57 |
-
yield temp
|
58 |
|
59 |
-
|
|
|
|
|
|
|
|
|
60 |
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
title="ZhongJingGPT-V2-1_8B-GGUF chatbot using llama-cpp-python",
|
65 |
-
description="",
|
66 |
-
examples=["日本の四国にある県名を挙げてください。"],
|
67 |
-
cache_examples=True,
|
68 |
-
retry_btn=None,
|
69 |
-
undo_btn="Remove last",
|
70 |
-
clear_btn="Clear all",
|
71 |
-
)
|
72 |
|
73 |
-
demo.launch(debug=True, share=True, max_threads=10)
|
74 |
|
75 |
|
76 |
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import hf_hub_download
|
3 |
import os
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
# 模型下载链接
|
6 |
model_url = "https://huggingface.co/CMLL/ZhongJing-2-1_8b-GGUF/resolve/main/ZhongJing1_5-1_8b-fp16.gguf"
|
|
|
|
|
|
|
7 |
|
8 |
+
# 下载模型
|
9 |
+
def download_model(url, model_dir="models"):
|
10 |
+
os.makedirs(model_dir, exist_ok=True)
|
11 |
+
model_path = hf_hub_download(repo_id="CMLL/ZhongJing-2-1_8b-GGUF", filename="ZhongJing1_5-1_8b-fp16.gguf", local_dir=model_dir)
|
12 |
+
return model_path
|
13 |
|
14 |
+
model_path = download_model(model_url)
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
# llama.cpp 克隆并编译
|
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 |
+
# 创建 prompts/TcmChat.txt 文件
|
22 |
+
prompts_dir = "llama.cpp/prompts"
|
23 |
+
os.makedirs(prompts_dir, exist_ok=True)
|
24 |
+
with open(os.path.join(prompts_dir, "TcmChat.txt"), "w") as f:
|
25 |
+
f.write("You are a helpful TCM medical assistant named 仲景中医大语言模型.\n")
|
26 |
|
27 |
+
# Gradio 接口
|
28 |
+
def chat_with_model(user_input, history):
|
29 |
+
prompt = f"You are a helpful TCM medical assistant named 仲景中医大语言模型.\nUser: {user_input}\nAssistant:"
|
30 |
+
response = os.popen(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')}").read()
|
31 |
+
history.append((user_input, response))
|
32 |
+
return history, history
|
33 |
|
34 |
+
with gr.Blocks() as demo:
|
35 |
+
chatbot = gr.Chatbot()
|
36 |
+
state = gr.State([])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column():
|
40 |
+
user_input = gr.Textbox(show_label=False, placeholder="Enter your message...")
|
41 |
+
with gr.Column():
|
42 |
+
submit_btn = gr.Button("Submit")
|
43 |
|
44 |
+
submit_btn.click(chat_with_model, [user_input, state], [chatbot, state])
|
45 |
|
46 |
+
if __name__ == "__main__":
|
47 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
|
|
49 |
|
50 |
|
51 |
|