flask-tts / app.py
imseldrith's picture
Update app.py
ae14d85 verified
from flask import Flask, request, jsonify
import requests
from bs4 import BeautifulSoup
app = Flask(__name__)
def text_to_speech(msg, voice):
# Start a session
session = requests.Session()
# Get the initial page to retrieve CSRF token and initial cookies
initial_url = "https://texttospeech.online/home/tryme"
response = session.get(initial_url)
# Parse the response to extract the CSRF token
soup = BeautifulSoup(response.text, 'html.parser')
csrf_token = soup.find('input', {'name': 'csrf_test_name'})['value']
# Define payload with dynamic CSRF token
payload = {
"csrf_test_name": csrf_token,
"front_tryme_language": "en-IN", # Assuming the language is fixed
"front_tryme_voice": voice,
"front_tryme_text": msg
}
# Define headers
headers = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9",
"Connection": "keep-alive",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Host": "texttospeech.online",
"Origin": "https://texttospeech.online",
"Referer": "https://texttospeech.online/",
"Sec-Ch_Ua": '"Not/A)Brand";v="8", "Chromium";v="126", "Microsoft Edge";v="126"',
"Sec-Ch_Ua-Mobile": "?0",
"Sec-Ch_Ua-Platform": '"Windows"',
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0",
"X-Requested-With": "XMLHttpRequest"
}
# Make the POST request with the session
post_url = "https://texttospeech.online/home/tryme_action/"
response = session.post(post_url, headers=headers, data=payload)
# Parse the JSON response
response_data = response.json()
if response_data["result"]:
tts_uri = response_data["tts_uri"]
return tts_uri
else:
return None
@app.route('/text_to_speech', methods=['POST'])
def api_text_to_speech():
# Extract parameters from URL query
url_msg = request.args.get('msg')
url_voice = request.args.get('voice')
# Extract parameters from JSON body
json_data = request.json
body_msg = json_data.get('msg') if json_data else None
body_voice = json_data.get('voice') if json_data else None
# Prioritize URL parameters over JSON body parameters
msg = url_msg if url_msg else body_msg
voice = url_voice if url_voice else body_voice
if not msg or not voice:
return jsonify({"error": "Please provide both 'msg' and 'voice' parameters."}), 400
tts_uri = text_to_speech(msg, voice)
if tts_uri:
return jsonify({"tts_uri": tts_uri})
else:
return jsonify({"error": "Failed to generate text-to-speech audio."}), 500
if __name__ == '__main__':
app.run(debug=True, port=7860, host="0.0.0.0")