Update app.py
Browse files
app.py
CHANGED
@@ -1,143 +1,134 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
from transformers import (
|
4 |
-
AutoModelForCausalLM,
|
5 |
-
AutoTokenizer,
|
6 |
-
TextIteratorStreamer,
|
7 |
-
)
|
8 |
-
import os
|
9 |
-
from threading import Thread
|
10 |
-
import spaces
|
11 |
-
import time
|
12 |
-
import subprocess
|
13 |
-
|
14 |
-
subprocess.run(
|
15 |
-
"pip install flash-attn --no-build-isolation",
|
16 |
-
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
|
17 |
-
shell=True,
|
18 |
-
)
|
19 |
|
20 |
-
|
|
|
|
|
|
|
21 |
|
22 |
-
model = AutoModelForCausalLM.from_pretrained(
|
23 |
-
"microsoft/Phi-3-mini-128k-instruct",
|
24 |
-
token=token,
|
25 |
-
trust_remote_code=True,
|
26 |
-
)
|
27 |
-
tok = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-128k-instruct", token=token)
|
28 |
-
terminators = [
|
29 |
-
tok.eos_token_id,
|
30 |
-
]
|
31 |
-
|
32 |
-
if torch.cuda.is_available():
|
33 |
-
device = torch.device("cuda")
|
34 |
-
print(f"Using GPU: {torch.cuda.get_device_name(device)}")
|
35 |
-
else:
|
36 |
-
device = torch.device("cpu")
|
37 |
-
print("Using CPU")
|
38 |
-
|
39 |
-
model = model.to(device)
|
40 |
-
|
41 |
-
# Dispatch Errors
|
42 |
-
|
43 |
-
@spaces.GPU(duration=60)
|
44 |
-
def chat(message, history, temperature, do_sample, max_tokens):
|
45 |
-
# 定義 PTZ 控制助手的 prompt
|
46 |
-
prompt = (
|
47 |
-
"You are an assistant for controlling PTZ cameras.\n"
|
48 |
-
"When the user gives you a clear command, please JUST respond in the following format:\n"
|
49 |
-
"Camera:<camera_id>. Tracking_Target:<target_name> placement:<position>.\n"
|
50 |
-
"If multiple cameras are specified, provide separate lines for each camera.\n"
|
51 |
-
"The available placements are: top_left, top_middle, top_right, "
|
52 |
-
"center_left, center_middle, center_right, bottom_left, bottom_middle, bottom_right.\n"
|
53 |
-
"Default Values:\n"
|
54 |
-
"- camera_id: default\n"
|
55 |
-
"- tracking_target: default\n"
|
56 |
-
"- placement: center_middle\n"
|
57 |
-
"Rules for Defaults:\n"
|
58 |
-
"1. If the camera_id is not specified, use the default value `default`.\n"
|
59 |
-
"2. If the tracking_target is not specified, use the default value `default`.\n"
|
60 |
-
"3. If the position information is incomplete or not specified, default the placement to the middle position.\n"
|
61 |
-
"For example, if the user specifies 'top', interpret it as 'top_middle'.\n\n"
|
62 |
-
"Examples:\n"
|
63 |
-
"User: Please set camera 1 to track target A at bottom_right.\n"
|
64 |
-
"Assistant: Camera:1. Tracking_Target:A placement:bottom_right.\n\n"
|
65 |
-
"User: Please set camera 2 to track target B at top.\n"
|
66 |
-
"Assistant: Camera:2. Tracking_Target:B placement:top_middle.\n\n"
|
67 |
-
"User: Please set camera 3 to track target C.\n"
|
68 |
-
"Assistant: Camera:3. Tracking_Target:C placement:center_middle.\n\n"
|
69 |
-
"User: Please track target D at left.\n"
|
70 |
-
"Assistant: Camera:default. Tracking_Target:D placement:center_left.\n\n"
|
71 |
-
"User: Please control camera 4.\n"
|
72 |
-
"Assistant: Camera:4. Tracking_Target:default placement:center_middle.\n\n"
|
73 |
-
"User: Please start recording.\n"
|
74 |
-
"Assistant: Camera:default. Tracking_Target:default placement:center_middle.\n\n"
|
75 |
-
"User: Please set camera 2 and camera 3 to track target Kyle at bottom_right.\n"
|
76 |
-
"Assistant:\n"
|
77 |
-
"Camera:2. Tracking_Target:Kyle placement:bottom_right.\n"
|
78 |
-
"Camera:3. Tracking_Target:Kyle placement:bottom_right.\n\n"
|
79 |
-
"Now, respond to the following command:\n"
|
80 |
-
)
|
81 |
-
|
82 |
-
chat = []
|
83 |
-
for item in history:
|
84 |
-
chat.append({"role": "user", "content": item[0]})
|
85 |
-
if item[1] is not None:
|
86 |
-
chat.append({"role": "assistant", "content": item[1]})
|
87 |
-
chat.append({"role": "user", "content": message})
|
88 |
-
|
89 |
-
# 將 prompt 添加到消息的開頭
|
90 |
-
full_message = prompt + "\n" + tok.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
|
91 |
-
|
92 |
-
model_inputs = tok([full_message], return_tensors="pt").to(device)
|
93 |
-
streamer = TextIteratorStreamer(
|
94 |
-
tok, timeout=20.0, skip_prompt=True, skip_special_tokens=True
|
95 |
-
)
|
96 |
-
generate_kwargs = dict(
|
97 |
-
model_inputs,
|
98 |
-
streamer=streamer,
|
99 |
-
max_new_tokens=max_tokens,
|
100 |
-
do_sample=True,
|
101 |
-
temperature=temperature,
|
102 |
-
eos_token_id=terminators,
|
103 |
-
)
|
104 |
|
105 |
-
if temperature == 0:
|
106 |
-
generate_kwargs["do_sample"] = False
|
107 |
|
108 |
-
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
-
|
112 |
-
for
|
113 |
-
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
|
116 |
-
yield partial_text
|
117 |
|
118 |
|
|
|
|
|
|
|
119 |
demo = gr.ChatInterface(
|
120 |
-
|
121 |
-
examples=[["Please set camera 2 to track target A at top."]],
|
122 |
-
additional_inputs_accordion=gr.Accordion(
|
123 |
-
label="⚙️ Parameters", open=False, render=False
|
124 |
-
),
|
125 |
additional_inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
gr.Slider(
|
127 |
-
minimum=0
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
maximum=4096,
|
133 |
-
step=1,
|
134 |
-
value=512,
|
135 |
-
label="Max new tokens",
|
136 |
-
render=False,
|
137 |
),
|
138 |
],
|
139 |
-
stop_btn="Stop Generation",
|
140 |
-
title="PTZ Camera Control Chat",
|
141 |
-
description="Now Running [microsoft/Phi-3-mini-128k-instruct](https://huggingface.co/microsoft/Phi-3-mini-128k-instruct) for PTZ camera control.",
|
142 |
)
|
143 |
-
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
"""
|
5 |
+
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
6 |
+
"""
|
7 |
+
client = InferenceClient("meta-llama/Llama-3.2-1B")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
|
|
|
|
10 |
|
11 |
+
def respond(
|
12 |
+
message,
|
13 |
+
history: list[tuple[str, str]],
|
14 |
+
system_message,
|
15 |
+
max_tokens,
|
16 |
+
temperature,
|
17 |
+
top_p,
|
18 |
+
):
|
19 |
+
MAX_HISTORY_LENGTH = 5 # 保留最近 5 條歷史
|
20 |
+
|
21 |
+
# 限制歷史對話的長度
|
22 |
+
history = history[-MAX_HISTORY_LENGTH:]
|
23 |
+
|
24 |
+
# 構建消息列表,從系統消息開始
|
25 |
+
messages = [{"role": "system", "content": system_message}]
|
26 |
+
|
27 |
+
# 添加歷史對話
|
28 |
+
for val in history:
|
29 |
+
if val[0]:
|
30 |
+
messages.append({"role": "user", "content": val[0]})
|
31 |
+
if val[1]:
|
32 |
+
messages.append({"role": "assistant", "content": val[1]})
|
33 |
+
|
34 |
+
# 添加最新的用戶輸入
|
35 |
+
messages.append({"role": "user", "content": message})
|
36 |
+
|
37 |
+
# 初始化空回應
|
38 |
+
response = ""
|
39 |
|
40 |
+
# 呼叫模型並生成回應,使用 stream 模式逐步更新
|
41 |
+
for message in client.chat_completion(
|
42 |
+
messages,
|
43 |
+
max_tokens=max_tokens,
|
44 |
+
stream=True,
|
45 |
+
temperature=temperature,
|
46 |
+
top_p=top_p,
|
47 |
+
):
|
48 |
+
token = message.choices[0].delta.content
|
49 |
+
response += token
|
50 |
+
yield response
|
51 |
|
|
|
52 |
|
53 |
|
54 |
+
"""
|
55 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
56 |
+
"""
|
57 |
demo = gr.ChatInterface(
|
58 |
+
respond,
|
|
|
|
|
|
|
|
|
59 |
additional_inputs=[
|
60 |
+
gr.Textbox(
|
61 |
+
value=(
|
62 |
+
"You are an assistant for controlling PTZ cameras.\n"
|
63 |
+
"When the user gives you a clear command, please JUST respond in the following format:\n"
|
64 |
+
"Camera:<camera_id>. Tracking_Target:<target_name> placement:<position> speed:<speed> only_ptz_action:<only_ptz_action> tracking_action:<tracking_action>.\n"
|
65 |
+
"If multiple cameras are specified, provide separate lines for each camera.\n"
|
66 |
+
"Only provide commands for the cameras specified by the user.\n"
|
67 |
+
"Do not include additional cameras that the user did not mention.\n"
|
68 |
+
"Ensure all field names are spelled correctly.\n\n"
|
69 |
+
"The available placements are ONLY: top_left, top_middle, top_right, center_left, center_middle, center_right, bottom_left, bottom_middle, bottom_right.\n"
|
70 |
+
"The available speed options are ONLY: slow, medium, fast.\n"
|
71 |
+
"The available only_ptz_actions are ONLY: turn_right, turn_left, tilt_up, tilt_down, zoom_in, zoom_out, stop.\n"
|
72 |
+
"The available tracking_actions are ONLY: tracking.\n\n"
|
73 |
+
"Default Values:\n"
|
74 |
+
"- camera_id: default\n"
|
75 |
+
"- tracking_target: default\n"
|
76 |
+
"- placement: center_middle\n"
|
77 |
+
"- speed: medium\n"
|
78 |
+
"- only_ptz_action: default\n"
|
79 |
+
"- tracking_action: default\n\n"
|
80 |
+
"Rules for Defaults:\n"
|
81 |
+
"1. If the camera_id is not specified, use the default value `default`.\n"
|
82 |
+
"2. If the tracking_target is not specified, use the default value `default`.\n"
|
83 |
+
"3. If the position information is incomplete or not specified, default the placement to `center_middle`.\n"
|
84 |
+
"4. If only a general direction is specified, interpret it as the middle of that direction.\n"
|
85 |
+
" For example, 'top' is interpreted as 'top_middle' and 'left' as 'center_left'.\n"
|
86 |
+
"5. If the speed is not specified, default to `medium`.\n"
|
87 |
+
"6. If the only_ptz_action is not specified, default to `default`.\n"
|
88 |
+
"7. If the tracking_action is not specified, default to `default`.\n"
|
89 |
+
"8. Camera IDs are restricted to 1, 2, 3, and 4. If an invalid camera_id is provided, use `default`.\n"
|
90 |
+
"9. If the user specifies 'all camera' or 'all cameras', apply the command to all cameras (1-4).\n\n"
|
91 |
+
"**Special Action Handling**:\n"
|
92 |
+
"- If only `camera_id` and `only_ptz_action` are specified (all other fields are `default`), execute only the specified `only_ptz_action`.\n"
|
93 |
+
"- If only `camera_id` and `tracking_action` are specified (all other fields are `default`), execute only the specified `tracking_action`.\n"
|
94 |
+
"- When tracking is involved, set `tracking_action` to `tracking`.\n\n"
|
95 |
+
"Examples:\n"
|
96 |
+
"User: Please set camera 1 to track target Alice at bottom_right with speed fast and action turn_right.\n"
|
97 |
+
"Assistant: Camera:1. Tracking_Target:Alice placement:bottom_right speed:fast only_ptz_action:turn_right tracking_action:default.\n\n"
|
98 |
+
"User: Please set camera 3 to track target Bob at top with speed slow.\n"
|
99 |
+
"Assistant: Camera:3. Tracking_Target:Bob placement:top_middle speed:slow only_ptz_action:default tracking_action:tracking.\n\n"
|
100 |
+
"User: Please set camera 2 to track target Carol.\n"
|
101 |
+
"Assistant: Camera:2. Tracking_Target:Carol placement:center_middle speed:medium only_ptz_action:default tracking_action:tracking.\n\n"
|
102 |
+
"User: Please track target Dave at left.\n"
|
103 |
+
"Assistant: Camera:default. Tracking_Target:Dave placement:center_left speed:medium only_ptz_action:default tracking_action:tracking.\n\n"
|
104 |
+
"User: Please control camera 4.\n"
|
105 |
+
"Assistant: Camera:4. Tracking_Target:default placement:center_middle speed:medium only_ptz_action:default tracking_action:default.\n\n"
|
106 |
+
"User: Please start recording.\n"
|
107 |
+
"Assistant: Camera:default. Tracking_Target:default placement:center_middle speed:medium only_ptz_action:default tracking_action:default.\n\n"
|
108 |
+
"User: Camera 5 action turn_left.\n"
|
109 |
+
"Assistant: Camera:default. Tracking_Target:default placement:center_middle speed:medium only_ptz_action:turn_left tracking_action:default.\n\n"
|
110 |
+
"User: camera 1 and 2 turn right.\n"
|
111 |
+
"Assistant:\n"
|
112 |
+
"Camera:1. Tracking_Target:default placement:center_middle speed:medium only_ptz_action:turn_right tracking_action:default.\n"
|
113 |
+
"Camera:2. Tracking_Target:default placement:center_middle speed:medium only_ptz_action:turn_right tracking_action:default.\n\n"
|
114 |
+
"Respond with ONLY the Assistant's output. Do NOT add any extra text."
|
115 |
+
|
116 |
+
),
|
117 |
+
label="System message"
|
118 |
+
)
|
119 |
+
,
|
120 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
121 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
122 |
gr.Slider(
|
123 |
+
minimum=0.1,
|
124 |
+
maximum=1.0,
|
125 |
+
value=0.95,
|
126 |
+
step=0.05,
|
127 |
+
label="Top-p (nucleus sampling)",
|
|
|
|
|
|
|
|
|
|
|
128 |
),
|
129 |
],
|
|
|
|
|
|
|
130 |
)
|
131 |
+
|
132 |
+
|
133 |
+
if __name__ == "__main__":
|
134 |
+
demo.launch()
|