Reorganize apps
Browse files- api_app.py +85 -0
- server.py +0 -91
- frontend.py → spaces_app.py +13 -1
- startup.sh +1 -1
api_app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from io import BytesIO
|
2 |
+
from flask import Flask, Response, send_from_directory, request, jsonify
|
3 |
+
from flask_cors import CORS
|
4 |
+
import datetime
|
5 |
+
import soundfile as sf
|
6 |
+
|
7 |
+
from config import Config
|
8 |
+
from tts import TTS
|
9 |
+
|
10 |
+
def create_api_app():
|
11 |
+
app = Flask(__name__)
|
12 |
+
CORS(app) # Enable CORS for all routes
|
13 |
+
|
14 |
+
@app.route('/')
|
15 |
+
def index():
|
16 |
+
return 'Server is up!'
|
17 |
+
|
18 |
+
@app.route('/api/text_to_speech', methods=['POST'])
|
19 |
+
def text_to_speech():
|
20 |
+
try:
|
21 |
+
data = request.json
|
22 |
+
input_text = data.get('text')
|
23 |
+
|
24 |
+
try:
|
25 |
+
audio_array = TTS.generate(input_text)
|
26 |
+
|
27 |
+
now = datetime.datetime.now()
|
28 |
+
now_str = now.strftime("%Y%m%d_%H%M%S")
|
29 |
+
file_name = f"output_{now_str}.wav"
|
30 |
+
file_path = f"./outputs/{file_name}"
|
31 |
+
|
32 |
+
# Save the audio to a file
|
33 |
+
TTS.save_audio(audio_array, file_path)
|
34 |
+
|
35 |
+
audio_url = f"{Config.BASE_URL}/api/audio/{file_name}";
|
36 |
+
|
37 |
+
return dict(success=True, audio_url=audio_url)
|
38 |
+
except Exception as e:
|
39 |
+
return dict(success=False, error=str(e))
|
40 |
+
except Exception as e:
|
41 |
+
return dict(success=False, error=str(e))
|
42 |
+
|
43 |
+
@app.route('/api/text_to_speech', methods=['GET'])
|
44 |
+
def text_to_speech2():
|
45 |
+
try:
|
46 |
+
# Parse GET parameters
|
47 |
+
input_text = request.args.get('text')
|
48 |
+
|
49 |
+
if not input_text:
|
50 |
+
return jsonify(success=False, error="Missing 'text' parameter"), 400
|
51 |
+
|
52 |
+
|
53 |
+
try:
|
54 |
+
audio_array = TTS.generate(input_text)
|
55 |
+
|
56 |
+
# Use an in-memory buffer to store the audio
|
57 |
+
buffer = BytesIO()
|
58 |
+
sf.write(buffer, audio_array, 44100, format='WAV') # Write audio to buffer in WAV format
|
59 |
+
buffer.seek(0) # Reset buffer pointer to the beginning
|
60 |
+
|
61 |
+
# Return the binary data with appropriate headers
|
62 |
+
return Response(
|
63 |
+
buffer.read(),
|
64 |
+
mimetype="audio/wav",
|
65 |
+
headers={
|
66 |
+
"Content-Disposition": "inline; filename=output.wav"
|
67 |
+
}
|
68 |
+
)
|
69 |
+
except Exception as e:
|
70 |
+
return dict(success=False, error=str(e))
|
71 |
+
except Exception as e:
|
72 |
+
return dict(success=False, error=str(e))
|
73 |
+
|
74 |
+
@app.route('/api/audio/<path:path>')
|
75 |
+
def send_audio(path):
|
76 |
+
return send_from_directory('outputs', path)
|
77 |
+
|
78 |
+
return app
|
79 |
+
|
80 |
+
def start_api_app(port=Config.PORT, debug=Config.DEBUG):
|
81 |
+
app = create_api_app()
|
82 |
+
app.run(host=Config.HOST, port=port, debug=debug)
|
83 |
+
|
84 |
+
if __name__ == '__main__':
|
85 |
+
start_api_app()
|
server.py
DELETED
@@ -1,91 +0,0 @@
|
|
1 |
-
from io import BytesIO
|
2 |
-
from flask import Flask, Response, send_from_directory, request, jsonify
|
3 |
-
from flask_cors import CORS
|
4 |
-
|
5 |
-
app = Flask(__name__)
|
6 |
-
CORS(app) # Enable CORS for all routes
|
7 |
-
|
8 |
-
# Import the TTS class
|
9 |
-
from frontend import start_gradio
|
10 |
-
from tts import TTS
|
11 |
-
import numpy as np
|
12 |
-
import onnxruntime as ort
|
13 |
-
from pathlib import Path
|
14 |
-
import datetime
|
15 |
-
from config import Config
|
16 |
-
import soundfile as sf
|
17 |
-
import threading
|
18 |
-
|
19 |
-
@app.route('/')
|
20 |
-
def index():
|
21 |
-
return 'Server is up!'
|
22 |
-
|
23 |
-
|
24 |
-
@app.route('/api/text_to_speech', methods=['POST'])
|
25 |
-
def text_to_speech():
|
26 |
-
try:
|
27 |
-
data = request.json
|
28 |
-
input_text = data.get('text')
|
29 |
-
|
30 |
-
try:
|
31 |
-
audio_array = TTS.generate(input_text)
|
32 |
-
|
33 |
-
now = datetime.datetime.now()
|
34 |
-
now_str = now.strftime("%Y%m%d_%H%M%S")
|
35 |
-
file_name = f"output_{now_str}.wav"
|
36 |
-
file_path = f"./outputs/{file_name}"
|
37 |
-
|
38 |
-
# Save the audio to a file
|
39 |
-
TTS.save_audio(audio_array, file_path)
|
40 |
-
|
41 |
-
audio_url = f"{Config.BASE_URL}/api/audio/{file_name}";
|
42 |
-
|
43 |
-
return dict(success=True, audio_url=audio_url)
|
44 |
-
except Exception as e:
|
45 |
-
return dict(success=False, error=str(e))
|
46 |
-
except Exception as e:
|
47 |
-
return dict(success=False, error=str(e))
|
48 |
-
|
49 |
-
@app.route('/api/text_to_speech', methods=['GET'])
|
50 |
-
def text_to_speech2():
|
51 |
-
try:
|
52 |
-
# Parse GET parameters
|
53 |
-
input_text = request.args.get('text')
|
54 |
-
|
55 |
-
if not input_text:
|
56 |
-
return jsonify(success=False, error="Missing 'text' parameter"), 400
|
57 |
-
|
58 |
-
|
59 |
-
try:
|
60 |
-
audio_array = TTS.generate(input_text)
|
61 |
-
|
62 |
-
# Use an in-memory buffer to store the audio
|
63 |
-
buffer = BytesIO()
|
64 |
-
sf.write(buffer, audio_array, 44100, format='WAV') # Write audio to buffer in WAV format
|
65 |
-
buffer.seek(0) # Reset buffer pointer to the beginning
|
66 |
-
|
67 |
-
# Return the binary data with appropriate headers
|
68 |
-
return Response(
|
69 |
-
buffer.read(),
|
70 |
-
mimetype="audio/wav",
|
71 |
-
headers={
|
72 |
-
"Content-Disposition": "inline; filename=output.wav"
|
73 |
-
}
|
74 |
-
)
|
75 |
-
except Exception as e:
|
76 |
-
return dict(success=False, error=str(e))
|
77 |
-
except Exception as e:
|
78 |
-
return dict(success=False, error=str(e))
|
79 |
-
|
80 |
-
@app.route('/api/audio/<path:path>')
|
81 |
-
def send_audio(path):
|
82 |
-
return send_from_directory('outputs', path)
|
83 |
-
|
84 |
-
|
85 |
-
# Start Gradio in a separate thread
|
86 |
-
thread = threading.Thread(target=start_gradio)
|
87 |
-
thread.daemon = True
|
88 |
-
thread.start()
|
89 |
-
|
90 |
-
if __name__ == '__main__':
|
91 |
-
app.run(host=Config.HOST, port=Config.PORT, debug=Config.DEBUG)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
frontend.py → spaces_app.py
RENAMED
@@ -3,6 +3,9 @@ import os
|
|
3 |
import sys
|
4 |
import logging
|
5 |
import requests
|
|
|
|
|
|
|
6 |
|
7 |
# Add the current directory to the path so we can import from app
|
8 |
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
@@ -50,4 +53,13 @@ def create_gradio_interface():
|
|
50 |
# Start Gradio in a separate thread
|
51 |
def start_gradio(port=9090):
|
52 |
demo = create_gradio_interface()
|
53 |
-
demo.launch(server_name="0.0.0.0", server_port=port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import sys
|
4 |
import logging
|
5 |
import requests
|
6 |
+
import threading
|
7 |
+
|
8 |
+
from api_app import start_api_app
|
9 |
|
10 |
# Add the current directory to the path so we can import from app
|
11 |
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|
|
53 |
# Start Gradio in a separate thread
|
54 |
def start_gradio(port=9090):
|
55 |
demo = create_gradio_interface()
|
56 |
+
demo.launch(server_name="0.0.0.0", server_port=port)
|
57 |
+
|
58 |
+
# Start Gradio in a separate thread
|
59 |
+
thread = threading.Thread(target=start_api_app)
|
60 |
+
thread.daemon = True
|
61 |
+
thread.start()
|
62 |
+
|
63 |
+
# Launch the app
|
64 |
+
if __name__ == "__main__":
|
65 |
+
start_gradio()
|
startup.sh
CHANGED
@@ -11,4 +11,4 @@ if [ $? -ne 0 ]; then
|
|
11 |
fi
|
12 |
|
13 |
# Start Gunicorn for the Flask app
|
14 |
-
gunicorn --bind 0.0.0.0:
|
|
|
11 |
fi
|
12 |
|
13 |
# Start Gunicorn for the Flask app
|
14 |
+
gunicorn --bind 0.0.0.0:9090 spaces_app:app
|