Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,80 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
stream=True,
|
34 |
temperature=temperature,
|
|
|
35 |
top_p=top_p,
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(
|
50 |
-
gr.Slider(
|
51 |
-
gr.Slider(
|
52 |
-
gr.Slider(
|
53 |
-
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
],
|
60 |
-
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import time
|
3 |
+
import subprocess
|
4 |
+
import os
|
5 |
+
from llama_cpp import Llama
|
6 |
+
from huggingface_hub import snapshot_download
|
7 |
|
8 |
+
# 下载并转换模型
|
9 |
+
def setup_model(model_id):
|
10 |
+
local_dir = model_id.split('/')[-1]
|
11 |
+
if not os.path.exists(local_dir):
|
12 |
+
snapshot_download(repo_id=model_id, local_dir=local_dir)
|
13 |
+
|
14 |
+
# 转换为 GGUF 格式
|
15 |
+
gguf_path = f"{local_dir}.gguf"
|
16 |
+
if not os.path.exists(gguf_path):
|
17 |
+
subprocess.run(f'python llama.cpp/convert_hf_to_gguf.py ./{local_dir} --outfile {gguf_path}', shell=True, check=True)
|
18 |
+
|
19 |
+
# 量化模型
|
20 |
+
quantized_path = f"{local_dir}-Q2_K.gguf"
|
21 |
+
if not os.path.exists(quantized_path):
|
22 |
+
subprocess.run(f'./llama.cpp/build/bin/llama-quantize ./{gguf_path} {quantized_path} Q2_K', shell=True, check=True)
|
23 |
+
|
24 |
+
return quantized_path
|
25 |
|
26 |
+
# 设定模型路径
|
27 |
+
MODEL_ID = "ibm-granite/granite-3.1-2b-instruct"
|
28 |
+
MODEL_PATH = setup_model(MODEL_ID)
|
29 |
|
30 |
+
# 加载 Llama 模型
|
31 |
+
llm = Llama(
|
32 |
+
model_path=MODEL_PATH,
|
33 |
+
verbose=False,
|
34 |
+
n_threads=4, # 调整线程数
|
35 |
+
n_ctx=32768 # 上下文窗口大小
|
36 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
def chat_with_model(message, history, system_prompt, temperature, max_tokens, top_k, top_p):
|
39 |
+
"""调用 Llama 模型生成回复"""
|
40 |
+
start_time = time.time()
|
41 |
+
|
42 |
+
messages = [{"role": "system", "content": system_prompt}]
|
43 |
+
for user_msg, assistant_msg in history:
|
44 |
+
messages.append({"role": "user", "content": user_msg})
|
45 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
46 |
messages.append({"role": "user", "content": message})
|
47 |
+
|
48 |
+
stream = llm.create_chat_completion(
|
49 |
+
messages=messages,
|
|
|
|
|
|
|
50 |
stream=True,
|
51 |
temperature=temperature,
|
52 |
+
top_k=top_k,
|
53 |
top_p=top_p,
|
54 |
+
max_tokens=max_tokens,
|
55 |
+
stop=["<|im_end|>"]
|
56 |
+
)
|
57 |
+
|
58 |
+
response = ""
|
59 |
+
for chunk in stream:
|
60 |
+
if "choices" in chunk and chunk["choices"]:
|
61 |
+
text = chunk["choices"][0].get("delta", {}).get("content", "")
|
62 |
+
response += text
|
63 |
+
yield response # 流式返回文本
|
64 |
|
65 |
+
print(f"生成耗时: {time.time() - start_time:.2f} 秒")
|
66 |
|
67 |
+
# 启动 Gradio ChatInterface
|
68 |
+
gr.ChatInterface(
|
69 |
+
fn=chat_with_model,
|
70 |
+
title="Llama GGUF Chatbot",
|
71 |
+
description="使用 Llama GGUF 量化模型进行推理",
|
72 |
+
additional_inputs_accordion=gr.Accordion(label="⚙️ 参数设置", open=False),
|
73 |
additional_inputs=[
|
74 |
+
gr.Textbox("You are a helpful assistant.", label="System Prompt"),
|
75 |
+
gr.Slider(0, 1, 0.6, label="Temperature"),
|
76 |
+
gr.Slider(100, 4096, 1000, label="Max Tokens"),
|
77 |
+
gr.Slider(1, 100, 40, label="Top K"),
|
78 |
+
gr.Slider(0, 1, 0.85, label="Top P"),
|
|
|
|
|
|
|
|
|
|
|
79 |
],
|
80 |
+
).queue().launch()
|
|
|
|
|
|
|
|