Answer the question
In order to leave comments, you need to log in
Hello, how can I write a function that takes responses from JSON and passes them to python?
Help writing a function or give a short example of the
OP: ubuntu 20.04
===================================== ============
I need to write a voice assistant that takes responses from JSON (of course, in accordance with the question), (I have already written speech recognition and displaying speech on the screen, and it can also be read by ear through terminal using RHVoice).
Help!. This is a hobby, in the future I plan to act as a programmer, so I develop in advance
Answer the question
In order to leave comments, you need to log in
import json
import random
# json - так json
ANSWERS = json.loads('{"привет": ["Привет", "Хай", "Здоровеньки булы"], "как дела": ["Норм", "А у тебя?"]}')
answer = lambda x:random.choice(ANSWERS.get(x.lower(), ['Не понял вопроса']))
answer('Привет')
# Привет
answer('Как дела')
# А у тебя?
answer('ваыва')
# Не понял вопроса
import time
import datetime
from vosk import Model, KaldiRecognizer # оффлайн-распознавание от Vosk
import speech_recognition # распознавание пользовательской речи (Speech-To-Text)
import wave # создание и чтение аудиофайлов формата wav
import json # работа с json-файлами и json-строками
import os # работа с файлами
import subprocess
import requests
def assistent(*args: tuple):
with microphone:
value = ""
one = 'echo Алиса слушает | RHVoice-test -p anna'
subprocess.call(one, shell=True)
recognizer.adjust_for_ambient_noise(microphone, duration=2)
try:
audio = recognizer.listen(microphone, 6, 5)
with open("voices-my.wav", "wb") as file:
file.write(audio.get_wav_data())
except speech_recognition.WaitTimeoutError:
print("Повтори пожалуйста:\n")
one = 'echo Повтори пожалуйста | RHVoice-test -p anna'
subprocess.call(one, shell=True)
return
try:
if not os.path.exists("images/vosk-model-small-ru-0.15"):
print("Нет пакета Vosk")
exit(1)
audio_file = wave.open("voices-my.wav", "rb")
model = Model("images/vosk-model-small-ru-0.15")
offline = KaldiRecognizer(model, audio_file.getframerate())
data = audio_file.readframes(audio_file.getnframes())
if len(data) > 0:
if offline.AcceptWaveform(data):
value = offline.Result()
value = json.loads(value)
value = value["text"]
except:
error = 'echo не могу распознать речь| RHVoice-test -p anna'
subprocess.call(error, shell=True)
return value
if __name__ == "__main__":
recognizer = speech_recognition.Recognizer()
microphone = speech_recognition.Microphone()
while True:
voice_input = assistent()
os.remove("voices-my.wav")
print(voice_input)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question