0

I am using pyttsx3 library to convert text to speech. I am using Flask rest API to implement the application

Problem

I am able to serve only one request for text to speech operation at a time But I wanted to understand how can I do it for multiple requests at same time for text to speech operation?

I wanted to achieve, multiple users can access this application from different locations and they can convert their text to voice, it might be possible that multiple users can use this application at same time. I know we need to use multithreading here but not sure how can I do it with pyttsx3 library.

Code

app.py

from flask import Flask, render_template, request, send_file
from flask_cors import cross_origin
from text2speech import text_to_speech

app = Flask(__name__)

APP_VERSION = '/v1'

@app.route('/')
@cross_origin()
def renderView():
    return render_template('home.html')

@app.route(APP_VERSION + '/api/convert', methods=['POST'])
@cross_origin()
def play():
    data = request.get_json()
    text = data['speech']
    gender = data['voices']
    text_to_speech(text, gender, 'play')
    return renderView()
     

if __name__ == "__main__":
    app.run(port=8000, debug=True)

text2speech.py

import pyttsx3

def text_to_speech(text, gender, actionType):
    voice_dict = {'Male': 0, 'Female': 1}
    code = voice_dict[gender]

    engine = pyttsx3.init()

    # Setting up voice rate
    engine.setProperty('rate', 125)

    # Setting up volume level  between 0 and 1
    engine.setProperty('volume', 0.8)

    # Change voices: 0 for male and 1 for female
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[code].id)

    engine.say(text)
    engine.runAndWait()
Sangram Badi
  • 4,054
  • 9
  • 45
  • 78

0 Answers0