File size: 1,831 Bytes
03e7882
710ade4
7daf25d
0ede83c
7daf25d
3cfdd3f
 
 
79e247b
3cfdd3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0db0b4e
 
f0944cc
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
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)