TDoC ’24 Day 6: Building a Web Interface for Vocalshift with Flask
December 22, 2024

TDoC ’24 Day 6: Building a Web Interface for Vocalshift with Flask

welcome to 2024 Technical Documentation! exist Part 6we explore how to use Flask framework. This interface acts as a front-end Voice Changing Artificial Intelligenceallowing users to enter text, upload message files, and download processing results. This guide explains Flask basics, analyzes the provided code, and helps you build your first Flask application.



What is a flask?

flask It is a lightweight Python web framework that allows developers to build web applications quickly and efficiently. It is an excellent choice for small to medium-sized projects.


Key features of Flask:

  • Simple: Keeps the core simple and lets you add features as needed.
  • flexible: Provides the freedom to build applications.
  • Expandable: Supports multiple expansions such as identity authentication and database.


Set up flask


Install

Install Flask using pip:

pip install flask
Enter full screen mode

Exit full screen mode


Basic flask application

Here is a simple Flask app:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Flask!"

if __name__ == '__main__':
    app.run(debug=True)
Enter full screen mode

Exit full screen mode

  • @app.route: Map the URL to a specific function.
  • app.run(debug=True): Run the application in debug mode for easy testing.


Implement Web interface using Flask

In this web interface, the Flask application handles Voice Changing Artificial Intelligence Workflow:

  1. receive user input: Accepts audio or text and optional speaker sample audio.
  2. Process input: Pass input to the Vocalshift backend.
  3. Provide output: Send the generated message back to the user.


Step 1: Configure the application

from flask import Flask, render_template, request, send_file, redirect, url_for, flash
from werkzeug.utils import secure_filename
import os
from main import process_tts
from vocalshift import vocal_shift

app = Flask(__name__)
app.secret_key = 'supersecretkey'
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'output'
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER
Enter full screen mode

Exit full screen mode

  • Upload and export: Separate directories store uploaded files and output.
  • os.makedirs(): Make sure the directory exists.


Step 2: Handle the homepage backend

@app.route('/', methods=['GET', 'POST'])
def index():
    if request.method == 'POST':
        text = request.form.get('text')
        language = request.form.get('language', 'en')
        speaker_file = request.files.get('speaker')
        audio_file = request.files.get('audio')
        output_filename = 'output.wav'
        output_path = os.path.join(app.config['OUTPUT_FOLDER'], output_filename)

        speaker_path = None
        if speaker_file:
            speaker_filename = secure_filename(speaker_file.filename)
            speaker_path = os.path.join(app.config['UPLOAD_FOLDER'], speaker_filename)
            speaker_file.save(speaker_path)

        if audio_file:
            audio_filename = secure_filename(audio_file.filename)
            audio_path = os.path.join(app.config['UPLOAD_FOLDER'], audio_filename)
            audio_file.save(audio_path)

            success = vocal_shift(
                input_audio=audio_path,
                output_audio=output_path,
                stt_model_size='base',
                speaker=speaker_path,
                effect=None,
                effect_level=1.0
            )
        else:
            if not text:
                flash('Text is required!', 'danger')
                return redirect(url_for('index'))

            # Perform TTS conversion using main.py
            success = process_tts(text, output_path, speaker_path, language)

        if success:
            return redirect(url_for('download_file', filename=output_filename))
        else:
            flash('Conversion failed', 'danger')
            return redirect(url_for('index'))

    return render_template('index.html')
Enter full screen mode

Exit full screen mode

  • get: Display the homepage in HTML form.
  • postal: Handles user input (text and file uploads).
  • render_template(): HTML file that renders the user interface.


Step 3: File download

@app.route('/download/')
def download_file(filename):
    return send_file(os.path.join(app.config['OUTPUT_FOLDER'], filename), as_attachment=True)
Enter full screen mode

Exit full screen mode

  • send_file():Send the output audio file for download.
  • as_attachment=True: Ensures the archive is downloaded rather than played in the browser.

In addition, we will also add the ability to start the server if the file is currently executed by Python:

if __name__ == '__main__':
    app.run(debug=True)
Enter full screen mode

Exit full screen mode



Create HTML interface

this is an example index.html User interface file:


 lang="en">

     charset="UTF-8">
     name="viewport" content="width=device-width, initial-scale=1.0">
    </span>VOCALSHIFT<span class="nt"/>
    <span class="nt"><link/> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"</span><span class="nt">></span>
    <span class="nt"/>
        <span class="nt">body</span> <span class="p">{</span>
            <span class="nl">background-color</span><span class="p">:</span> <span class="m">#f8f9fa</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="nc">.container</span> <span class="p">{</span>
            <span class="nl">max-width</span><span class="p">:</span> <span class="m">600px</span><span class="p">;</span>
            <span class="nl">margin-top</span><span class="p">:</span> <span class="m">50px</span><span class="p">;</span>
            <span class="nl">padding</span><span class="p">:</span> <span class="m">20px</span><span class="p">;</span>
            <span class="nl">background-color</span><span class="p">:</span> <span class="m">#ffffff</span><span class="p">;</span>
            <span class="nl">border-radius</span><span class="p">:</span> <span class="m">8px</span><span class="p">;</span>
            <span class="nl">box-shadow</span><span class="p">:</span> <span class="m">0</span> <span class="m">0</span> <span class="m">10px</span> <span class="n">rgba</span><span class="p">(</span><span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">0</span><span class="p">,</span> <span class="m">0.1</span><span class="p">);</span>
        <span class="p">}</span>
        <span class="nc">.progress</span> <span class="p">{</span>
            <span class="nl">display</span><span class="p">:</span> <span class="nb">none</span><span class="p">;</span>
            <span class="nl">margin-top</span><span class="p">:</span> <span class="m">20px</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="nt"/>
<span class="nt"/>
<span class="nt"/>
    <span class="nt"><div> <span class="na">class=</span><span class="s">"container"</span><span class="nt">></span>
        <span class="nt"/>
        <span class="nt"/>
        <span class="nt"><div> <span class="na">class=</span><span class="s">"progress"</span><span class="nt">></span>
            <span class="nt"><p> <span class="na">class=</span><span class="s">"progress-bar progress-bar-striped progress-bar-animated"</span> <span class="na">role=</span><span class="s">"progressbar"</span> <span class="na">style=</span><span class="s">"width: 100%"</span><span class="nt">></span></p></span>
        <span class="nt"/></div></span>
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                <span class="nt"><div> <span class="na">class=</span><span class="s">"mt-3"</span><span class="nt">></span>
                    {% for category, message in messages %}
                        <span class="nt"><p> <span class="na">class=</span><span class="s">"alert alert-{{ category }}"</span><span class="nt">></span>{{ message }}<span class="nt"/></p></span>
                    {% endfor %}
                <span class="nt"/></div></span>
            {% endif %}
        {% endwith %}
    <span class="nt"/></div></span>
    <span class="nt"><script><![CDATA[<span class="na">src=]]></script></span><span class="s">"https://code.jquery.com/jquery-3.5.1.min.js"</span><span class="nt">></span>
    <span class="nt"><script/></span>
        <span class="nf">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nf">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
            <span class="nf">$</span><span class="p">(</span><span class="dl">'</span><span class="s1">#tts-form</span><span class="dl">'</span><span class="p">).</span><span class="nf">on</span><span class="p">(</span><span class="dl">'</span><span class="s1">submit</span><span class="dl">'</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span>
                <span class="nf">$</span><span class="p">(</span><span class="dl">'</span><span class="s1">.progress</span><span class="dl">'</span><span class="p">).</span><span class="nf">show</span><span class="p">();</span>
            <span class="p">});</span>
        <span class="p">});</span>
    <span class="nt"/>
<span class="nt"/>
<span class="nt"/>
</span></span></span></span></code></pre>
<div class="highlight__panel js-actions-panel">
<div class="highlight__panel-action js-fullscreen-code-action">
    <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>Enter full screen mode
    

Exit full screen mode


feature:

  • bootstrapping integration: for styling and responsiveness.
  • form elements: Accepts text input and optional speaker audio.
  • flash news: Display verification and error messages.


Run the application


Start the Flask server

Run the Flask application:

python app.py
Enter full screen mode

Exit full screen mode

access http://127.0.0.1:5000 Access the interface in your browser.



what we have achieved today

to the end of the year Part 6you:

  • Learn the basics of Flask and how to set up routing.
  • Constructed a web interface for text input and file upload.
  • Integrate TTS backend with Flask to process and serve user requests.
  • Provides seamless download option for generated files.


Looking to the future

That’s it Change voice Project! You’ve learned a lot, from the basics of Python to building a fully functional web application. Going forward, please consider:

  • hosting: Deploy your application using platforms like Heroku or AWS.
  • Enhanced user interface: Use high-level frameworks such as React or Vue.js.
  • New features: Realize instant voice playback.


Today’s resources



Your feedback matters!

We’d love to hear about your experiences! Share your questions, suggestions, or feedback in the comments below. Let’s keep innovating! 🚀

2024-12-22 12:24:15

Leave a Reply

Your email address will not be published. Required fields are marked *