CMLL commited on
Commit
ed0a7ea
·
verified ·
1 Parent(s): 44ac364

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -60
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
- # ウェブUIの起動
16
- import gradio as gr
17
- import copy
18
- import time
19
- from llama_cpp import Llama
20
 
21
- llm = Llama(
22
- model_path=model_path,
23
- n_ctx=2048,
24
- # n_gpu_layers=100, # CPUで実行する場合は削除
25
- )
26
 
27
- history = []
 
 
 
28
 
29
- system_message = """
30
- You are a helpful TCM medical assistant named 仲景中医大语言模型.
31
- """
 
 
32
 
33
- def generate_text(message, history):
34
- temp = ""
35
- input_prompt = f"{system_message}"
36
- for interaction in history:
37
- input_prompt = input_prompt + "\nUSER: " + str(interaction[0]) + "\nASSISTANT: " + str(interaction[1])
38
- input_prompt = input_prompt + "\nUSER: " + str(message) + "\nASSISTANT: "
39
 
40
- output = llm.create_completion(
41
- input_prompt,
42
- temperature=0.7,
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
- history.append((message, temp))
 
 
 
 
60
 
 
61
 
62
- demo = gr.ChatInterface(
63
- generate_text,
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