簡體   English   中英

嘗試在 Python 中制作 Watson IBM 助手。 在沒有音頻文件的情況下,如何修復我的代碼以在 IDE Pycharm 中說出答案。 使用 VLC

[英]Trying to make a Watson IBM assistant in Python. How can I fix my code to say the answer within the IDE Pycharm without the audio file. Using VLC

這是從另一個基於 Watson 的問題中獲取的代碼。 我正在嘗試創建一個系統,在其中輸入我的語音作為問題或命令,它在 IDE 中給出了答案,這是我上一個問題My Previous Question的鏈接,但我該如何修復下面的代碼。

這是代碼...

    import vlc
    from ibm_watson import TextToSpeechV1
    from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

authenticator = IAMAuthenticator("API Key")
text_to_speech = TextToSpeechV1(
    authenticator=authenticator
)

text_to_speech.set_service_url(
'https://api.us-south.text-to-speech.watson.cloud.ibm.com/instances/113cd664-f07b-44fe-a11d-a46cc50caf84')

# define VLC instance
instance = vlc.Instance('--input-repeat=-1', '--fullscreen')

# Define VLC player
player = instance.media_player_new()

# Define VLC media
media = instance.media_new(
    text_to_speech.synthesize(
        'Hello world',
        voice='en-US_AllisonVoice',
        accept='audio/wav').get_result().content)

# Set player media
player.set_media(media)

# Play the media
player.play()][1]

登錄后,肯定有改進。 我更改了密鑰和 URL 並且只收到 2 個小代碼錯誤...

Traceback (most recent call last):
  File "C:/Users/PycharmProjects/IBM Test/iBM tEST.py", line 24, in <module>
    accept='audio/wav').get_result().content)
  File "C:\Users\PycharmProjects\IBM Test\venv\lib\site-packages\vlc.py", line 1947, in media_new
    if ':' in mrl and mrl.index(':') > 1:
TypeError: a bytes-like object is required, not 'str'

該錯誤告訴您 python 運行時找不到 vlc 模塊。 你需要跑

pip install python-vlc

或者

sudo pip install python-vlc

次要問題的編輯

您將需要調試代碼以找出服務返回的錯誤。 為此,您將不得不分解您的代碼。


try:
  result = text_to_speech.synthesize(
        'Hello world',
        voice='en-US_AllisonVoice',
        accept='audio/wav').get_result()
  print(result)

except Exception as e:
  print(e.message)

第三個問題的編輯

http 404 is not found 暗示你有錯誤 url

第四個問題的編輯

http 403 被禁止,這意味着 url、密鑰和方法的組合無效。 很大程度上表明您正在使用 url 和密鑰以獲得完全不同的服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM