Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer
|
3 |
+
import onnxruntime as ort
|
4 |
+
import numpy as np
|
5 |
+
import string
|
6 |
+
from huggingface_hub import InferenceClient
|
7 |
+
|
8 |
+
# Initialize Qwen client
|
9 |
+
qwen_client = InferenceClient("EVA-UNIT-01/EVA-Qwen2.5-1.5B-v0.0")
|
10 |
+
|
11 |
+
# Model and ONNX setup
|
12 |
+
HG_MODEL = "livekit/turn-detector"
|
13 |
+
ONNX_FILENAME = "model_quantized.onnx"
|
14 |
+
PUNCS = string.punctuation.replace("'", "")
|
15 |
+
MAX_HISTORY = 4 # Adjusted to use the last 4 messages
|
16 |
+
MAX_HISTORY_TOKENS = 512
|
17 |
+
EOU_THRESHOLD = 0.5 # Updated threshold to match original
|
18 |
+
|
19 |
+
# Initialize ONNX model
|
20 |
+
tokenizer = AutoTokenizer.from_pretrained(HG_MODEL)
|
21 |
+
onnx_session = ort.InferenceSession(ONNX_FILENAME, providers=["CPUExecutionProvider"])
|
22 |
+
|
23 |
+
# Softmax function
|
24 |
+
def softmax(logits):
|
25 |
+
exp_logits = np.exp(logits - np.max(logits))
|
26 |
+
return exp_logits / np.sum(exp_logits)
|
27 |
+
|
28 |
+
# Normalize text
|
29 |
+
def normalize_text(text):
|
30 |
+
def strip_puncs(text):
|
31 |
+
return text.translate(str.maketrans("", "", PUNCS))
|
32 |
+
return " ".join(strip_puncs(text).lower().split())
|
33 |
+
|
34 |
+
# Format chat context
|
35 |
+
def format_chat_ctx(chat_ctx):
|
36 |
+
new_chat_ctx = []
|
37 |
+
for msg in chat_ctx:
|
38 |
+
if msg["role"] in ("user", "assistant"):
|
39 |
+
content = normalize_text(msg["content"])
|
40 |
+
if content:
|
41 |
+
msg["content"] = content
|
42 |
+
new_chat_ctx.append(msg)
|
43 |
+
|
44 |
+
# Tokenize with chat template
|
45 |
+
convo_text = tokenizer.apply_chat_template(
|
46 |
+
new_chat_ctx, add_generation_prompt=False, add_special_tokens=False, tokenize=False
|
47 |
+
)
|
48 |
+
|
49 |
+
# Remove EOU token from the current utterance
|
50 |
+
ix = convo_text.rfind("<|im_end|>")
|
51 |
+
return convo_text[:ix]
|
52 |
+
|
53 |
+
# Calculate EOU probability
|
54 |
+
def calculate_eou(chat_ctx, session):
|
55 |
+
formatted_text = format_chat_ctx(chat_ctx[-MAX_HISTORY:]) # Use the last 4 messages
|
56 |
+
inputs = tokenizer(
|
57 |
+
formatted_text,
|
58 |
+
return_tensors="np",
|
59 |
+
truncation=True,
|
60 |
+
max_length=MAX_HISTORY_TOKENS,
|
61 |
+
)
|
62 |
+
input_ids = np.array(inputs["input_ids"], dtype=np.int64)
|
63 |
+
outputs = session.run(["logits"], {"input_ids": input_ids})
|
64 |
+
logits = outputs[0][0, -1, :]
|
65 |
+
probs = softmax(logits)
|
66 |
+
eou_token_id = tokenizer.encode("<|im_end|>")[-1]
|
67 |
+
return probs[eou_token_id]
|
68 |
+
|
69 |
+
# Read system message from file
|
70 |
+
with open("character/herta.txt", "r") as f:
|
71 |
+
system_message = f.read()
|
72 |
+
|
73 |
+
# Respond function
|
74 |
+
def respond(
|
75 |
+
message,
|
76 |
+
history: list[tuple[str, str]],
|
77 |
+
max_tokens,
|
78 |
+
temperature,
|
79 |
+
top_p,
|
80 |
+
):
|
81 |
+
# Keep the last 4 conversation pairs (user-assistant)
|
82 |
+
messages = [{"role": "system", "content": system_message}]
|
83 |
+
|
84 |
+
for val in history[-10:]: # Only use the last 4 pairs
|
85 |
+
if val[0]:
|
86 |
+
messages.append({"role": "user", "content": val[0]})
|
87 |
+
if val[1]:
|
88 |
+
messages.append({"role": "assistant", "content": val[1]})
|
89 |
+
|
90 |
+
# Add the new user message to the context
|
91 |
+
messages.append({"role": "user", "content": message})
|
92 |
+
|
93 |
+
# Calculate EOU probability
|
94 |
+
eou_prob = calculate_eou(messages, onnx_session)
|
95 |
+
print(f"EOU Probability: {eou_prob}") # Debug output
|
96 |
+
|
97 |
+
# If EOU is below the threshold, ask for more input
|
98 |
+
if eou_prob < EOU_THRESHOLD:
|
99 |
+
yield "[Waiting for user to continue input...]"
|
100 |
+
return
|
101 |
+
|
102 |
+
# Generate response with Qwen
|
103 |
+
response = ""
|
104 |
+
for message in qwen_client.chat_completion(
|
105 |
+
messages,
|
106 |
+
max_tokens=max_tokens,
|
107 |
+
stream=True,
|
108 |
+
temperature=temperature,
|
109 |
+
top_p=top_p,
|
110 |
+
):
|
111 |
+
token = message.choices[0].delta.content
|
112 |
+
response += token
|
113 |
+
yield response
|
114 |
+
|
115 |
+
print(f"Generated response: {response}")
|
116 |
+
|
117 |
+
|
118 |
+
# Gradio interface
|
119 |
+
demo = gr.ChatInterface(
|
120 |
+
respond,
|
121 |
+
# additional_inputs=[
|
122 |
+
# # Commented out to disable user modification of the system message
|
123 |
+
# # gr.Textbox(value="You are an assistant.", label="System message"),
|
124 |
+
# gr.Slider(minimum=1, maximum=4096, value=256, step=1, label="Max new tokens"),
|
125 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
126 |
+
# gr.Slider(
|
127 |
+
# minimum=0.1,
|
128 |
+
# maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
129 |
+
# ),
|
130 |
+
# ],
|
131 |
+
theme = gr.themes.Default().set(
|
132 |
+
button_primary_background_fill="#FF0000",
|
133 |
+
button_primary_background_fill_dark="#AAAAAA",
|
134 |
+
button_primary_border="*button_primary_background_fill",
|
135 |
+
button_primary_border_dark="*button_primary_background_fill_dark",
|
136 |
+
)
|
137 |
+
|
138 |
+
)
|
139 |
+
|
140 |
+
if __name__ == "__main__":
|
141 |
+
demo.launch()
|