Click here to Skip to main content
15,887,175 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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:

HTML
<!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:
Python
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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'  # Replace with your actual API key
   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?
Posted
Updated 19-Sep-23 10:40am
v8
Comments
Richard Deeming 18-Sep-23 4:53am    
"I got an error ..." without the details of the error is absolutely no use.

And no, nobody is going to some random external website to download some unknown file just to see the details of the error that you couldn't be bothered to tell us about.

You've been here almost 11½ years - plenty of time to learn how to ask a question.
Member 8840306 18-Sep-23 5:05am    
you can see the image url that is now bold
Member 8840306 18-Sep-23 5:20am    
the bold link is show detail of error
OriginalGriff 18-Sep-23 8:14am    
Very few people here will go anywhere near link shorteners: we have no idea what is on the other end. Making a link to it bold doesn't change that.

You want to show us an error message? Copy'n'paste the text into your question.
Member 8840306 18-Sep-23 11:11am    
See i have added the main error .How i can solve it? Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900