Add gradio frontend
Browse files- Dockerfile +7 -4
- frontend.py +53 -0
- healthcheck.sh +5 -0
- nginx.conf +19 -0
- requirements.txt +3 -1
- server.py +12 -6
Dockerfile
CHANGED
@@ -6,7 +6,7 @@ WORKDIR /app
|
|
6 |
|
7 |
# Install git and git-lfs
|
8 |
RUN apt-get update \
|
9 |
-
&& apt-get install -y git git-lfs libsndfile1 \
|
10 |
&& apt-get clean \
|
11 |
&& rm -rf /var/lib/apt/lists/* \
|
12 |
&& git lfs install
|
@@ -19,14 +19,17 @@ RUN pip install --no-cache-dir -r requirements.txt \
|
|
19 |
# Install gruut[sw] separately
|
20 |
&& pip install -f 'https://synesthesiam.github.io/prebuilt-apps/' 'gruut[sw]'
|
21 |
|
|
|
|
|
|
|
22 |
# Copy the rest of the application code into the container
|
23 |
COPY . .
|
24 |
|
25 |
-
# Expose the port the app runs on
|
26 |
EXPOSE 7860
|
27 |
|
28 |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
29 |
-
CMD
|
30 |
|
31 |
# Run the application
|
32 |
-
CMD ["gunicorn", "--bind", "0.0.0.0:
|
|
|
6 |
|
7 |
# Install git and git-lfs
|
8 |
RUN apt-get update \
|
9 |
+
&& apt-get install -y git git-lfs libsndfile1 nginx \
|
10 |
&& apt-get clean \
|
11 |
&& rm -rf /var/lib/apt/lists/* \
|
12 |
&& git lfs install
|
|
|
19 |
# Install gruut[sw] separately
|
20 |
&& pip install -f 'https://synesthesiam.github.io/prebuilt-apps/' 'gruut[sw]'
|
21 |
|
22 |
+
# Copy Nginx configuration
|
23 |
+
COPY nginx.conf /etc/nginx/nginx.conf
|
24 |
+
|
25 |
# Copy the rest of the application code into the container
|
26 |
COPY . .
|
27 |
|
28 |
+
# Expose the port the app runs on (from nginx)
|
29 |
EXPOSE 7860
|
30 |
|
31 |
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
32 |
+
CMD ["sh", "/app/healthcheck.sh"]
|
33 |
|
34 |
# Run the application
|
35 |
+
CMD ["gunicorn", "--bind", "0.0.0.0:8080", "server:app"]
|
frontend.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
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__)))
|
9 |
+
|
10 |
+
# Configure logging
|
11 |
+
logging.basicConfig(
|
12 |
+
level=logging.INFO,
|
13 |
+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
14 |
+
)
|
15 |
+
logger = logging.getLogger(__name__)
|
16 |
+
|
17 |
+
# Function to fetch audio from a URL
|
18 |
+
def fetch_audio(input_text):
|
19 |
+
# Replace with the actual backend URL for generating audio
|
20 |
+
backend_url = "https://stem-content-ai-project-swahili-tts-model.hf.space/text_to_speech"
|
21 |
+
response = requests.get(backend_url, params={"text": input_text})
|
22 |
+
if response.status_code == 200:
|
23 |
+
return response.url # Return the URL of the generated audio
|
24 |
+
else:
|
25 |
+
return None
|
26 |
+
|
27 |
+
# Gradio interface
|
28 |
+
def create_gradio_interface():
|
29 |
+
# Create the Gradio interface
|
30 |
+
with gr.Blocks(title="Swahili TTS Model") as demo:
|
31 |
+
gr.Markdown("# Swahili TTS Model")
|
32 |
+
gr.Markdown("Generate Swahili speech from text.")
|
33 |
+
|
34 |
+
with gr.Row():
|
35 |
+
with gr.Column():
|
36 |
+
input_text = gr.Textbox(label="Input Text")
|
37 |
+
generate_btn = gr.Button("Generate Speech")
|
38 |
+
|
39 |
+
with gr.Column():
|
40 |
+
# show generated audio from a url on output, href = "https://sample.com/audio.wav"
|
41 |
+
output = gr.Audio(label="Generated Audio", type="filepath")
|
42 |
+
|
43 |
+
generate_btn.click(
|
44 |
+
fn=fetch_audio,
|
45 |
+
inputs=input_text,
|
46 |
+
outputs=output
|
47 |
+
)
|
48 |
+
return demo
|
49 |
+
|
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)
|
healthcheck.sh
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
curl -f http://localhost:7860 || exit 1 # Nginx
|
4 |
+
curl -f http://localhost:8080 || exit 1 # Flask API
|
5 |
+
curl -f http://localhost:9090 || exit 1 # Gradio frontend
|
nginx.conf
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
server {
|
2 |
+
listen 7861;
|
3 |
+
|
4 |
+
# Route /api/* to Flask (port 8080)
|
5 |
+
location /api/ {
|
6 |
+
proxy_pass http://127.0.0.1:8080/;
|
7 |
+
proxy_set_header Host $host;
|
8 |
+
proxy_set_header X-Real-IP $remote_addr;
|
9 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
10 |
+
}
|
11 |
+
|
12 |
+
# Route everything else to Gradio (port 9090)
|
13 |
+
location / {
|
14 |
+
proxy_pass http://127.0.0.1:9090/;
|
15 |
+
proxy_set_header Host $host;
|
16 |
+
proxy_set_header X-Real-IP $remote_addr;
|
17 |
+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
18 |
+
}
|
19 |
+
}
|
requirements.txt
CHANGED
@@ -13,4 +13,6 @@ gruut[sw] -f https://synesthesiam.github.io/prebuilt-apps
|
|
13 |
|
14 |
librosa
|
15 |
phonemizer
|
16 |
-
g2p_id
|
|
|
|
|
|
13 |
|
14 |
librosa
|
15 |
phonemizer
|
16 |
+
g2p_id
|
17 |
+
gradio
|
18 |
+
requests
|
server.py
CHANGED
@@ -6,6 +6,7 @@ app = Flask(__name__)
|
|
6 |
CORS(app) # Enable CORS for all routes
|
7 |
|
8 |
# Import the TTS class
|
|
|
9 |
from tts import TTS
|
10 |
import numpy as np
|
11 |
import onnxruntime as ort
|
@@ -13,13 +14,14 @@ from pathlib import Path
|
|
13 |
import datetime
|
14 |
from config import Config
|
15 |
import soundfile as sf
|
|
|
16 |
|
17 |
@app.route('/')
|
18 |
def index():
|
19 |
return 'Server is up!'
|
20 |
|
21 |
|
22 |
-
@app.route('/text_to_speech', methods=['POST'])
|
23 |
def text_to_speech():
|
24 |
try:
|
25 |
data = request.json
|
@@ -36,7 +38,7 @@ def text_to_speech():
|
|
36 |
# Save the audio to a file
|
37 |
TTS.save_audio(audio_array, file_path)
|
38 |
|
39 |
-
audio_url = f"{Config.BASE_URL}/audio/{file_name}";
|
40 |
|
41 |
return dict(success=True, audio_url=audio_url)
|
42 |
except Exception as e:
|
@@ -44,7 +46,7 @@ def text_to_speech():
|
|
44 |
except Exception as e:
|
45 |
return dict(success=False, error=str(e))
|
46 |
|
47 |
-
@app.route('/text_to_speech', methods=['GET'])
|
48 |
def text_to_speech2():
|
49 |
try:
|
50 |
# Parse GET parameters
|
@@ -70,16 +72,20 @@ def text_to_speech2():
|
|
70 |
"Content-Disposition": "inline; filename=output.wav"
|
71 |
}
|
72 |
)
|
73 |
-
|
74 |
-
return dict(success=True, audio_url=audio_url)
|
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('/audio/<path:path>')
|
81 |
def send_audio(path):
|
82 |
return send_from_directory('outputs', path)
|
83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
if __name__ == '__main__':
|
85 |
app.run(host=Config.HOST, port=Config.PORT, debug=Config.DEBUG)
|
|
|
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
|
|
|
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
|
|
|
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:
|
|
|
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
|
|
|
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)
|