-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.py
180 lines (144 loc) · 5.12 KB
/
run.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
from gradio_client import Client
from playsound import playsound
from gtts import gTTS
import pyaudio
import wave
import RPi.GPIO as GPIO
import os
import time
from multiprocessing import Process, Pipe
from blink_LED import LED_class
parent_conn, child_conn = Pipe()
a = LED_class()
p = Process(target=a.parseData, args=(child_conn,))
p.start()
buttonpin = 21
GPIO.setmode(GPIO.BCM)
GPIO.setup(buttonpin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Set the GPIO mode and PWM frequency
GPIO.setmode(GPIO.BCM)
PWM_FREQUENCY = 1000
# Pin numbers for Red, Green, and Blue channels
RED_PIN = 17
GREEN_PIN = 18
BLUE_PIN = 27
# Setup PWM channels for Red, Green, and Blue LEDs
GPIO.setup(RED_PIN, GPIO.OUT)
GPIO.setup(GREEN_PIN, GPIO.OUT)
GPIO.setup(BLUE_PIN, GPIO.OUT)
red_pwm = GPIO.PWM(RED_PIN, PWM_FREQUENCY)
green_pwm = GPIO.PWM(GREEN_PIN, PWM_FREQUENCY)
blue_pwm = GPIO.PWM(BLUE_PIN, PWM_FREQUENCY)
GPIO.output(RED_PIN, GPIO.LOW)
GPIO.output(GREEN_PIN, GPIO.LOW)
GPIO.output(BLUE_PIN, GPIO.LOW)
def getFromAPI(query : str):
os.system("rm output1.wav | true")
os.system("""curl --location 'https://a83f-103-159-214-187.ngrok-free.app/query' \
--header 'Content-Type:application/json' \
--data '{"base_prompt":"{"""+query +"""}", "history": []}' -o answer.mp3""")
def error_call():
GPIO.output(RED_PIN, GPIO.LOW)
GPIO.output(GREEN_PIN, GPIO.LOW)
GPIO.output(BLUE_PIN, GPIO.HIGH)
def recording():
parent_conn.send(1)
def speaking():
print("Sending")
parent_conn.send(0)
print("Sent")
GPIO.output(RED_PIN, GPIO.HIGH)
GPIO.output(GREEN_PIN, GPIO.HIGH)
GPIO.output(BLUE_PIN, GPIO.HIGH)
def thinking():
while True:
for duty_cycle in range(0, 101, 1):
if duty_cycle % 2 == 0:
red_pwm.ChangeDutyCycle(95)
# Smoothly increase green value
green_pwm.ChangeDutyCycle(0)
time.sleep(0.15)
if duty_cycle % 2 == 1:
red_pwm.ChangeDutyCycle(0)
# Smoothly increase green value
green_pwm.ChangeDutyCycle(95)
time.sleep(0.15)
init_prompt = "## Instruction: You are an AI language model and must return truthful responses as per the information. Do not answer with any information which isn't completely verified and correct. Do not lie. Do not present information where you don't know the answer. Do not include incorrect extra information. Your name is IITIGPT. You are a helpful and truthful chatbot. You can help answer any questions about the IIT Indore campus."
info = "Information: \n"
q_prompt = "\n ##Instruction: Please provide an appropriate response to the following in 100 words or less and if possible, under 50 words. Respond in less than 3 lines.: \n"
client = Client("https://sanchit-gandhi-whisper-large-v2.hf.space/")
retrieval = Client("https://warlord-k-iiti-similarity.hf.space/")
chat_client = Client("https://mosaicml-mpt-30b-chat.hf.space/", serialize=False)
chatbot = [["", None]]
def answer_question(question):
global chatbot
information = retrieval.predict(question, api_name="/predict")
answer = chat_client.predict(
# str in 'Type an input and press Enter' Textbox component
init_prompt + info + information + question,
chatbot,
fn_index=1,
)
chatbot = answer[1]
return answer[1][0][1]
def record_audio(file_name, sample_rate=96000, channels=1, chunk_size=2048):
a = file_name
audio = pyaudio.PyAudio()
stream = audio.open(
format=pyaudio.paInt16,
channels=channels,
rate=sample_rate,
input=True,
frames_per_buffer=chunk_size,
)
# print("Recording...")
frames = []
readval = GPIO.input(buttonpin)
while not readval:
data = stream.read(chunk_size)
frames.append(data)
readval = GPIO.input(buttonpin)
# print("Finished recording.")
stream.stop_stream()
stream.close()
audio.terminate()
wave_file = wave.open(file_name, "wb")
wave_file.setnchannels(channels)
wave_file.setsampwidth(audio.get_sample_size(pyaudio.paInt16))
wave_file.setframerate(sample_rate)
wave_file.writeframes(b"".join(frames))
wave_file.close()
return a
def file_to_text(audio_fpath):
result = client.predict(
audio_fpath,
"transcribe", # str in 'Audio input' Radio component
api_name="/predict",
)
return result
def text_to_file(text):
tts = gTTS(text, lang="en")
tts.save("answer.mp3")
return "answer.mp3"
def main(filename):
text = file_to_text(filename)
print(text)
getFromAPI(text)
return "answer.mp3"
file_name = "output.wav"
if __name__ == "__main__":
while True:
readval = GPIO.input(buttonpin)
if not readval:
try:
recording()
input_audio = record_audio(file_name)
final_audio = main(input_audio)
speaking()
playsound("answer.mp3")
except Exception as e:
print(e)
error_call()
parent_conn.send(0)
playsound("error.mp3")
print("Error")