forked from svpino/alloy-voice-assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassistant.py
166 lines (129 loc) · 4.9 KB
/
assistant.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import base64
from threading import Lock, Thread
import time
import numpy # Fügen Sie diesen Import hinzu
import cv2
import openai
from PIL import ImageGrab
from cv2 import imencode
from dotenv import load_dotenv
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.schema.messages import SystemMessage
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from pyaudio import PyAudio, paInt16
from speech_recognition import Microphone, Recognizer, UnknownValueError
load_dotenv()
class DesktopScreenshot:
def __init__(self):
self.screenshot = None
self.running = False
self.lock = Lock()
def start(self):
if self.running:
return self
self.running = True
self.thread = Thread(target=self.update, args=())
self.thread.start()
return self
def update(self):
while self.running:
screenshot = ImageGrab.grab()
screenshot = cv2.cvtColor(numpy.array(screenshot), cv2.COLOR_RGB2BGR)
self.lock.acquire()
self.screenshot = screenshot
self.lock.release()
time.sleep(0.1) # Kurze Pause, um CPU-Auslastung zu reduzieren
def read(self, encode=False):
self.lock.acquire()
screenshot = self.screenshot.copy() if self.screenshot is not None else None
self.lock.release()
if encode and screenshot is not None:
_, buffer = imencode(".jpeg", screenshot)
return base64.b64encode(buffer)
return screenshot
def stop(self):
self.running = False
if self.thread.is_alive():
self.thread.join()
class Assistant:
def __init__(self, model):
self.chain = self._create_inference_chain(model)
def answer(self, prompt, image):
if not prompt:
return
print("Prompt:", prompt)
response = self.chain.invoke(
{"prompt": prompt, "image_base64": image.decode()},
config={"configurable": {"session_id": "unused"}},
).strip()
print("Response:", response)
if response:
self._tts(response)
def _tts(self, response):
player = PyAudio().open(format=paInt16, channels=1, rate=24000, output=True)
with openai.audio.speech.with_streaming_response.create(
model="tts-1",
voice="shimmer",
response_format="pcm",
input=response,
) as stream:
for chunk in stream.iter_bytes(chunk_size=1024):
player.write(chunk)
def _create_inference_chain(self, model):
SYSTEM_PROMPT = """
You are a witty assistant that will use the chat history and the image
provided by the user to answer its questions.
Use few words on your answers. Go straight to the point. Do not use any
emoticons or emojis. Do not ask the user any questions.
Be friendly and helpful. Show some personality. Do not be too formal.
"""
prompt_template = ChatPromptTemplate.from_messages(
[
SystemMessage(content=SYSTEM_PROMPT),
MessagesPlaceholder(variable_name="chat_history"),
(
"human",
[
{"type": "text", "text": "{prompt}"},
{
"type": "image_url",
"image_url": "data:image/jpeg;base64,{image_base64}",
},
],
),
]
)
chain = prompt_template | model | StrOutputParser()
chat_message_history = ChatMessageHistory()
return RunnableWithMessageHistory(
chain,
lambda _: chat_message_history,
input_messages_key="prompt",
history_messages_key="chat_history",
)
desktop_screenshot = DesktopScreenshot().start()
model = ChatOpenAI(model="gpt-4o")
assistant = Assistant(model)
def audio_callback(recognizer, audio):
try:
prompt = recognizer.recognize_whisper(audio, model="base", language="german")
assistant.answer(prompt, desktop_screenshot.read(encode=True))
except UnknownValueError:
print("There was an error processing the audio.")
recognizer = Recognizer()
microphone = Microphone()
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
stop_listening = recognizer.listen_in_background(microphone, audio_callback)
while True:
screenshot = desktop_screenshot.read()
if screenshot is not None:
cv2.imshow("Desktop", screenshot)
if cv2.waitKey(1) in [27, ord("q")]:
break
desktop_screenshot.stop()
cv2.destroyAllWindows()
stop_listening(wait_for_stop=False)