Created
July 7, 2024 10:59
-
-
Save ilovelili/18ff24e7b6160bed91414b8a52fef4b5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import azure.cognitiveservices.speech as speechsdk | |
import requests | |
import json | |
from dotenv import load_dotenv | |
load_dotenv() | |
# Azure OpenAI service credentials | |
speech_endpoint = os.getenv("SPEECH_ENDPOINT") | |
speech_api_key = os.getenv("SPEECH_API_KEY") | |
speech_deployment_id = os.getenv("SPEECH_DEPLOYMENT_ID") | |
region = os.getenv("REGION") | |
subscription = os.getenv("SUBSCRIPTION_ID") | |
def recognize_from_microphone(): | |
# Creates an instance of a speech config with specified subscription key and service region. | |
speech_config = speechsdk.SpeechConfig( | |
subscription=subscription, region=region, speech_recognition_language="ja-JP" | |
) | |
# Creates a recognizer with the given settings | |
speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config) | |
print("Say something...") | |
# Starts speech recognition, and returns after a single utterance is recognized. The end of a | |
# single utterance is determined by listening for silence at the end or until a maximum of 15 | |
# seconds of audio is processed. The task returns the recognition text as result. | |
result = speech_recognizer.recognize_once() | |
# Check the result | |
if result.reason == speechsdk.ResultReason.RecognizedSpeech: | |
print("Recognized: {}".format(result.text)) | |
return result.text | |
elif result.reason == speechsdk.ResultReason.NoMatch: | |
print("No speech could be recognized: {}".format(result.no_match_details)) | |
elif result.reason == speechsdk.ResultReason.Canceled: | |
cancellation_details = result.cancellation_details | |
print("Speech Recognition canceled: {}".format(cancellation_details.reason)) | |
if cancellation_details.reason == speechsdk.CancellationReason.Error: | |
print("Error details: {}".format(cancellation_details.error_details)) | |
if __name__ == "__main__": | |
recognized_text = recognize_from_microphone() | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment