Skip to content

Commit 15c37a1

Browse files
committed
* optimize app_chat&app_vlm
1 parent 1c604c9 commit 15c37a1

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

projects/app_chat/app.yaml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
id: ai_chat
2-
name: AI Chat
3-
name[zh]: AI聊天
4-
version: 1.0.3
1+
id: local_chat
2+
name: Local Chat
3+
name[zh]: 本地聊天
4+
version: 1.0.0
55
icon: assets/icon.json
66
author: Sipeed Ltd
7-
desc: AI Chat
8-
desc[zh]: AI 聊天
7+
desc: Local AI Chat
8+
desc[zh]: 本地AI 聊天
99
exclude:
1010
- dist
1111
- build

projects/app_chat/main.py

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from queue import Queue, Empty
44
import re
55

6-
from maix._maix.image import Image
7-
86
class PagedText:
97
def __init__(self, page_width = -1, page_height = -1):
108
"""
@@ -126,16 +124,21 @@ def __init__(self):
126124
ai_isp_on = bool(int(app.get_sys_config_kv("npu", "ai_isp", "1")))
127125
if ai_isp_on is True:
128126
img = image.Image(320, 240, bg=image.COLOR_BLACK)
129-
err_msg = "You need edit /boot/configs to set ai_isp_on to 0"
130-
err_msg_size = image.string_size(err_msg)
131-
img.draw_string((img.width() - err_msg_size.width()) // 2, (img.height() - err_msg_size.height()) // 2, err_msg, image.COLOR_RED)
127+
err_title_msg = "Ops!!!"
128+
err_msg = "You need open the Settings app, find the AI ISP option, and select Off."
129+
err_exit_msg = "Tap anywhere on the screen to exit."
130+
img.draw_string(0, 0, err_title_msg, image.COLOR_WHITE, 0.8)
131+
img.draw_string(0, 20, err_msg, image.COLOR_WHITE, 0.8)
132+
img.draw_string(0, 200, err_exit_msg, image.COLOR_WHITE, 0.6)
132133
self.disp.show(img)
133134
while not app.need_exit():
134135
ts_data = self.ts.read()
135136
if ts_data[2]:
136137
app.set_exit_flag(True)
137138
time.sleep_ms(100)
139+
exit(0)
138140
self.whisper = nn.Whisper(model="/root/models/whisper-base/whisper-base.mud", language="en")
141+
self.whisper_thread = None
139142

140143
self.__show_load_info('loading llm..')
141144
# /root/models/Qwen2.5-0.5B-Instruct/model.mud
@@ -189,6 +192,17 @@ def tts_thread_handle(self):
189192
except Empty:
190193
continue
191194

195+
def _whisper_thread_handle(self, path):
196+
self.whisper_results = self.whisper.transcribe(path)
197+
198+
def run_whisper(self, path:str):
199+
if self.whisper_thread:
200+
if self.whisper_thread.is_alive():
201+
self.whisper_thread.join()
202+
self.whisper_thread = None
203+
self.whisper_thread = threading.Thread(target=self._whisper_thread_handle, args=[path])
204+
self.whisper_thread.start()
205+
192206
def __llm_on_reply(self, obj, resp):
193207
print(resp.msg_new, end="")
194208
ts_data = self.ts.read()
@@ -207,6 +221,7 @@ def __llm_on_reply(self, obj, resp):
207221
if ts_data[2] and 0<=ts_data[0]<=self.exit_img.width() + exit_img_x*2 and 0 <=ts_data[1]<=self.exit_img.height() + exit_img_y*2:
208222
print('exit')
209223
app.set_exit_flag(True)
224+
exit(0)
210225
self.disp.show(img)
211226

212227
self.llm_last_msg += resp.msg_new
@@ -312,7 +327,7 @@ class Status:
312327
app.set_exit_flag(True)
313328
self.disp.show(img)
314329

315-
if status == Status.IDLE:
330+
if status == Status.IDLE:
316331
if ts_data[2]:
317332
if self.vad:
318333
start_vad = not start_vad
@@ -337,9 +352,16 @@ class Status:
337352
self.__reset_recorder(False)
338353
status = Status.TRANSCRIBE
339354
elif status == Status.TRANSCRIBE:
340-
asr_result = self.whisper.transcribe(self.default_wav_path)
341-
print(asr_result)
342-
status = Status.LLM
355+
if not self.whisper_thread:
356+
# 未创建线程, 开运行whisper
357+
self.run_whisper(self.default_wav_path)
358+
else:
359+
# 线程结束, whisper出了结果
360+
if not self.whisper_thread.is_alive():
361+
self.whisper_thread = None
362+
asr_result = self.whisper_results
363+
print(asr_result)
364+
status = Status.LLM
343365
elif status == Status.LLM:
344366
if asr_result:
345367
self.page_text.reset(320, 210)

projects/app_vlm/app.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
id: ai_vlm
2-
name: AI VLM
3-
name[zh]: AI 视觉大模型
1+
id: local_vlm
2+
name: Local VLM
3+
name[zh]: 本地视觉大模型
44
version: 1.0.3
55
icon: assets/icon.png
66
author: Sipeed Ltd
7-
desc: AI Vision Language Model
8-
desc[zh]: AI 视觉大模型
7+
desc: Local AI Vision Language Model
8+
desc[zh]: 本地 AI 视觉大模型
99
exclude:
1010
- dist
1111
- build

projects/app_vlm/main.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def add_text(self, text):
2525
page_height_used = sum(line[2] for line in current_page)
2626

2727
for ch in text:
28+
if ch == '\n' or ch == '\r':
29+
continue
2830
line_text, _, line_h = current_page[-1]
2931
new_line_text = line_text + ch
3032
size = image.string_size(new_line_text)
@@ -99,7 +101,20 @@ def __init__(self):
99101
self.exit_img = image.load('./assets/exit.jpg')
100102
ai_isp_on = bool(int(app.get_sys_config_kv("npu", "ai_isp", "1")))
101103
if ai_isp_on is True:
102-
self.show_error("Please trun off AI ISP first via the Settings app(Settings->AI ISP)")
104+
img = image.Image(320, 240, bg=image.COLOR_BLACK)
105+
err_title_msg = "Ops!!!"
106+
err_msg = "You need open the Settings app, find the AI ISP option, and select Off."
107+
err_exit_msg = "Tap anywhere on the screen to exit."
108+
img.draw_string(0, 0, err_title_msg, image.COLOR_WHITE, 0.8)
109+
img.draw_string(0, 20, err_msg, image.COLOR_WHITE, 0.8)
110+
img.draw_string(0, 200, err_exit_msg, image.COLOR_WHITE, 0.6)
111+
self.disp.show(img)
112+
while not app.need_exit():
113+
ts_data = self.ts.read()
114+
if ts_data[2]:
115+
app.set_exit_flag(True)
116+
time.sleep_ms(100)
117+
exit(0)
103118

104119
self.__show_load_info('loading vlm..')
105120
self.vlm = nn.InternVL('/root/models/InternVL2.5-1B/model.mud')
@@ -138,7 +153,7 @@ def show_error(self, msg: str):
138153
def __vlm_thread(self, vlm, img:image.Image, msg: str):
139154
vlm.set_image(img, image.Fit.FIT_CONTAIN)
140155
resp = vlm.send(msg)
141-
print(resp)
156+
print(resp.msg)
142157
with self.vlm_thread_lock:
143158
self.sta = self.Status.VLM_STOP
144159

@@ -244,7 +259,10 @@ def run(self):
244259
self.sta = self.Status.IDLE
245260

246261
self.show_ui()
262+
if self.vlm:
263+
self.vlm.cancel()
247264

248265
if __name__ == '__main__':
249266
appication = App()
250-
appication.run()
267+
appication.run()
268+
print('===========')

0 commit comments

Comments
 (0)