voice / app.py
hussein2000's picture
Update app.py
3cfdd3f verified
from flask import Flask, request, jsonify
import requests
app = Flask(__name__)
def get_mp3_voice(text, pubsub_topic):
# Set the request URL
url = "https://api.braininc.net/be/vectordb/tts"
# Define the headers
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36",
"Content-Type": "application/json",
"Authorization": "token f082106b7f91d304eb2049fae9048160817425f1"
}
# Define the payload with required parameters
payload = {
"pubsub_topic": pubsub_topic,
"text": text
}
try:
# Send the POST request to the server
response = requests.post(url, json=payload, headers=headers)
# Check if the request was successful
if response.status_code == 200:
# Save the mp3 file
with open("output.mp3", "wb") as file:
file.write(response.content)
return "MP3 file saved as 'output.mp3'"
else:
return f"Request failed with status code {response.status_code}: {response.text}"
except requests.exceptions.RequestException as e:
return f"An error occurred: {e}"
@app.route('/generate-voice', methods=['POST'])
def generate_voice():
# Get text from request JSON
data = request.get_json()
text = data.get("text")
if not text:
return jsonify({"error": "No text provided"}), 400
# Call get_mp3_voice with the provided text and a static pubsub_topic
pubsub_topic = "/studios/739240/wsuid_new-edge-22_nodeid_editor-21/customApi/1731358894033"
result = get_mp3_voice(text, pubsub_topic)
return jsonify({"message": result})
if __name__ == '__main__':
app.run(host="0.0.0.0", port=7860)