Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,143 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
|
20 |
-
|
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 |
-
|
27 |
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
temperature=temperature,
|
35 |
-
|
36 |
-
)
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
|
43 |
-
"""
|
44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
45 |
-
"""
|
46 |
demo = gr.ChatInterface(
|
47 |
-
|
|
|
|
|
|
|
|
|
48 |
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
|
|
|
|
|
|
|
|
58 |
),
|
59 |
],
|
|
|
|
|
|
|
60 |
)
|
61 |
-
|
62 |
-
|
63 |
-
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
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 |
+
token = os.environ["HF_TOKEN"]
|
|
|
|
|
|
|
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 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
109 |
+
t.start()
|
110 |
|
111 |
+
partial_text = ""
|
112 |
+
for new_text in streamer:
|
113 |
+
partial_text += new_text
|
114 |
+
yield partial_text
|
115 |
+
|
116 |
+
yield partial_text
|
117 |
|
118 |
|
|
|
|
|
|
|
119 |
demo = gr.ChatInterface(
|
120 |
+
fn=chat,
|
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, maximum=1, step=0.1, value=0.9, label="Temperature", render=False
|
128 |
+
),
|
129 |
+
gr.Checkbox(label="Sampling", value=True),
|
130 |
+
gr.Slider(
|
131 |
+
minimum=128,
|
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 |
+
demo.launch()
|
|
|
|
|
|