Spaces:
Sleeping
Sleeping
hussein2000
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,55 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import requests
|
3 |
-
import json
|
4 |
|
5 |
app = Flask(__name__)
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
"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"
|
11 |
-
}
|
12 |
-
|
13 |
-
@app.route('/get_response', methods=['POST'])
|
14 |
-
def get_response():
|
15 |
-
user_message = request.json.get('message')
|
16 |
-
chat_history = request.json.get('chat_history')
|
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 |
if __name__ == '__main__':
|
45 |
app.run(host="0.0.0.0", port=7860)
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import requests
|
|
|
3 |
|
4 |
app = Flask(__name__)
|
5 |
|
6 |
+
def get_mp3_voice(text, pubsub_topic):
|
7 |
+
# Set the request URL
|
8 |
+
url = "https://api.braininc.net/be/vectordb/tts"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Define the headers
|
11 |
+
headers = {
|
12 |
+
"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",
|
13 |
+
"Content-Type": "application/json",
|
14 |
+
"Authorization": "token f082106b7f91d304eb2049fae9048160817425f1"
|
15 |
+
}
|
16 |
+
|
17 |
+
# Define the payload with required parameters
|
18 |
+
payload = {
|
19 |
+
"pubsub_topic": pubsub_topic,
|
20 |
+
"text": text
|
21 |
+
}
|
22 |
+
|
23 |
+
try:
|
24 |
+
# Send the POST request to the server
|
25 |
+
response = requests.post(url, json=payload, headers=headers)
|
26 |
+
|
27 |
+
# Check if the request was successful
|
28 |
+
if response.status_code == 200:
|
29 |
+
# Save the mp3 file
|
30 |
+
with open("output.mp3", "wb") as file:
|
31 |
+
file.write(response.content)
|
32 |
+
return "MP3 file saved as 'output.mp3'"
|
33 |
+
else:
|
34 |
+
return f"Request failed with status code {response.status_code}: {response.text}"
|
35 |
+
|
36 |
+
except requests.exceptions.RequestException as e:
|
37 |
+
return f"An error occurred: {e}"
|
38 |
+
|
39 |
+
@app.route('/generate-voice', methods=['POST'])
|
40 |
+
def generate_voice():
|
41 |
+
# Get text from request JSON
|
42 |
+
data = request.get_json()
|
43 |
+
text = data.get("text")
|
44 |
+
|
45 |
+
if not text:
|
46 |
+
return jsonify({"error": "No text provided"}), 400
|
47 |
+
|
48 |
+
# Call get_mp3_voice with the provided text and a static pubsub_topic
|
49 |
+
pubsub_topic = "/studios/739240/wsuid_new-edge-22_nodeid_editor-21/customApi/1731358894033"
|
50 |
+
result = get_mp3_voice(text, pubsub_topic)
|
51 |
+
|
52 |
+
return jsonify({"message": result})
|
53 |
|
54 |
if __name__ == '__main__':
|
55 |
app.run(host="0.0.0.0", port=7860)
|