Spaces:
Sleeping
Sleeping
File size: 2,975 Bytes
f457163 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
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)
|