I am new to Python.I have hugging face application SeamlessM4T
at this link. I was trying to solve this problem using chatgpt.
To create a Flask interface for translating speech to speech in different languages using the Hugging Face Shameless M4T API, follow these steps:
Here is the HTML page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Speech Translator Adventure</title>
</head>
<body style="background-color: #cfe7ff; text-align: center;">
<h1 style="color: #ff5733;">Speech Translator Adventure</h1>
<form action="/translate" method="post" enctype="multipart/form-data">
<label for="audio_file" style="font-size: 20px;">Choose your adventure:</label><br>
<input type="file" name="audio_file" accept=".mp3" required><br><br>
<label for="source_language" style="font-size: 20px;">Choose your starting language:</label><br>
<input type="text" name="source_language" placeholder="Starting Language Code" required><br><br>
<label for="target_language" style="font-size: 20px;">Choose your destination language:</label><br>
<input type="text" name="target_language" placeholder="Destination Language Code" required><br><br>
<input type="submit" value="Begin Translation Adventure">
</form>
{% if translated_audio %}
<h2 style="color: #33cc33;">The Translated Adventure:</h2>
<audio controls>
<source src="data:audio/wav;base64,{{ translated_audio }}" type="audio/wav">
Your browser does not support the audio element.
</audio>
{% endif %}
</body>
</html>
Here is my
app.py:
from flask import Flask, render_template, request, send_file
import requests
from pydub import AudioSegment
from io import BytesIO
import base64
import os
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/translate', methods=['POST'])
def translate_audio():
api_key = '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'
url = 'https://api-inference.huggingface.co/models/shameless/m4t'
uploaded_file = request.files['audio_file']
audio_content = uploaded_file.read()
audio = AudioSegment.from_mp3(BytesIO(audio_content))
audio.export('audio.wav', format='wav')
with open('audio.wav', 'rb') as f:
audio_content = f.read()
source_language = request.form['source_language']
target_language = request.form['target_language']
headers = {'Authorization': f'Bearer {api_key}'}
data = {'inputs': audio_content, 'options': {'target_language': target_language}}
response = requests.post(url, headers=headers, json=data)
translated_audio_data = response.content
os.remove('audio.wav')
return render_template('index.html', translated_audio=base64.b64encode(translated_audio_data).decode())
if __name__ == '__main__':
app.run(debug=True)
Replace '
YOUR_API_KEY
' with your actual Shameless M4T API key.
Then I run this command in cmd:
python app.py
Open your Web Browser:
Go to http://localhost:5000.
What I have tried:
I got an error. You can see that
at this link.
The main error is
File "C:\Users\hp\Desktop\Seamless\app.py", line 12, in index
app = Flask(__name__)
@app.route('/')
def index():
return render_template('C:\\Users\\hp\Desktop\\Seamless\\index.html')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@app.route('/translate', methods=['POST'])
def translate_audio():
api_key = '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' # Replace with your actual API key
url = 'https://api-inference.huggingface.co/models/shameless/m4t'
It is not reading the html file. I have tried four ways to right path, but all give error.
How do I resolve this error and make the code work?