-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_with_speech.py
161 lines (120 loc) · 5.16 KB
/
basic_with_speech.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
# Basic Product + Speech Test
# Camera + LAVIS + ChatGPT + TTS + Speech Recog
# TODO
# - Render Captions larger to the screen.
# - Talk every minute along with keyword detection
# - Animations while waiting
import torch
from lavis.models import load_model_and_preprocess
from PIL import Image
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using {device}...")
from openai import OpenAI
from dotenv import load_dotenv
import pyttsx3
import os
from vosk import Model, KaldiRecognizer
import pyaudio
import Prompts as pmp
import random
import Prompts as pmp
from MirrorTTS import MirrorTTS as mTTS
load_dotenv()
import numpy as np
import cv2 as cv
import time
#========[ INPUTS ]==========================================
PERSONA_INDEX = 5 # <--- Change this value
PERSONAS = [
"A",
"B",
"Broski",
"Bogan",
"Butler",
"Gordon R",
"David At",
"Paris Hilton",
"Uncle Roger"
]
#============================================================
tts_engine = mTTS()
tts_engine.setVoiceProfile(PERSONAS[PERSONA_INDEX])
client = OpenAI()
speech_R_model = Model("models/vosk-model-small-en-us-0.15/vosk-model-small-en-us-0.15")
recognizer = KaldiRecognizer(speech_R_model, 16000, '[ "mirror mirror" ]')
magic_words = "mirror mirror"
mic = pyaudio.PyAudio()
stream = mic.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=8192)
stream.start_stream()
# time.sleep(5)
start_time = time.perf_counter()
cap = cv.VideoCapture(1)
if not cap.isOpened():
print("Cannot open camera")
exit()
# So that the image is not too dark
ramp_frames = 30
for i in range(ramp_frames):
temp = cap.read()
while True:
speech_data = stream.read(4096, exception_on_overflow = False)
if recognizer.AcceptWaveform(speech_data):
text = recognizer.Result()
output = text[14:-3]
if output == magic_words:
print(output, "ok, give me a moment...")
tts_engine.engine.say("ok, give me a moment...")
tts_engine.engine.runAndWait()
ret, frame = cap.read()
if not ret:
print("Can't receive frame (stream end?). Exiting ...")
tts_engine.engine.say("Ahhh, something went wrong. Imma head out...")
tts_engine.engine.runAndWait()
break
else:
# cv.imshow('frame', frame)
rgb_image = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
raw_image = Image.fromarray(rgb_image)
raw_image.show()
# while True:
# if cv.waitKey(1) == ord('q'):
# break
model, vis_processors, txt_processors = load_model_and_preprocess(name="blip_vqa", model_type="vqav2", is_eval=True, device=device)
# ask a random question.
# start_time = time.perf_counter()
image = vis_processors["eval"](raw_image).unsqueeze(0).to(device)
answers = []
for q in pmp.questions:
pq = txt_processors["eval"](q)
answers.append(model.predict_answers(samples={"image": image, "text_input": pq}, inference_method="generate")[0])
# print(f"Execution time: {time.perf_counter() - start_time}")
print("Answers")
for a in answers:
print(a)
person_description = f"They are about {answers[0]} years old, wearing {answers[1]}, a {answers[3]} top, a {answers[4]} bottom. They are in {answers[5]} clothes and also seem to be wearing {answers[11]}. They have {answers[7]} looking facial features and look {answers[6]}. They are {answers[8]} in a {answers[9]} and their hair is {answers[12]}."
print("="*15, "DEBUG INFO", "="*15)
print("="*8, "PROMPT", "="*8)
print(pmp.goals["Describe person"] + person_description + pmp.extras["shorten"])
print("="*40)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": pmp.personalities[PERSONAS[PERSONA_INDEX]]},
{"role": "user", "content": pmp.goals["Describe person"] + person_description + pmp.extras["shorten"]}
]
)
print(f"Execution time: {time.perf_counter() - start_time}")
response = completion.choices[0].message.content
print("="*40)
print(f"PERSONA: {PERSONAS[PERSONA_INDEX]}")
print(response)
print("="*40)
tts_engine.engine.say(response)
tts_engine.engine.runAndWait()
else:
wrong_answer = pmp.wrong_keyword_responses[random.randint(0, len(pmp.wrong_keyword_responses)-1)]
print(wrong_answer)
tts_engine.engine.say(pmp.wrong_keyword_responses[random.randint(0, len(pmp.wrong_keyword_responses)-1)])
tts_engine.engine.runAndWait()
cap.release()
cv.destroyAllWindows()