'Azure Functions HTTP trigger python text to speech program
I have developed a Text to Speech python program and the program will saved as MP3 file
from gtts import gTTS
# import Os module to start the audio file
import os
fh = open("test.txt", "r",encoding='UTF-8')
myText = fh.read().replace("\n", " ")
# Language we want to use
language = 'en'
output = gTTS(text=myText, lang=language, slow=False)
output.save("output.mp3")
fh.close()
# Play the converted file
os.system("start output.mp3")
Now I want to trigger this program by using Azure Functions HTTP trigger python program But I don't know how to edit the init.py in VScode
import logging
# Import the Gtts module for text
# to speech conversion
from gtts import gTTS
import azure.functions as func
# import Os module to start the audio file
import os
import requests as req
from bs4 import BeautifulSoup
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
name = req.params.get('name')
if not name:
try:
req_body = req.get_json()
except ValueError:
pass
else:
name = req_body.get('name')
if name:
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
return func.HttpResponse(
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
status_code=200
)
My question is how to do a local test by postman to merge my python code in int.py and successfully trigger the program to output a mp3 file?
Many thanks!!
Solution 1:[1]
Just need to put your code under the
logging.info('Python HTTP trigger function processed a request.')
will be ok.
I think the code can be like this:
import logging
# Import the Gtts module for text
# to speech conversion
from gtts import gTTS
import azure.functions as func
# import Os module to start the audio file
import os
import requests as req
from bs4 import BeautifulSoup
def main(req: func.HttpRequest) -> func.HttpResponse:
#Put your logic here.
return func.HttpResponse(f"test.")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Bowman Zhu-MSFT |

