Update main.py
Browse files
main.py
CHANGED
@@ -2,67 +2,43 @@ from flask import Flask, request, jsonify, abort
|
|
2 |
import whisper
|
3 |
import os
|
4 |
|
5 |
-
from flask_cors import CORS
|
6 |
-
from tempfile import NamedTemporaryFile
|
7 |
-
|
8 |
# Load Whisper model
|
9 |
print("\nLoading Whisper\n", flush=True)
|
10 |
model = whisper.load_model("small")
|
11 |
|
12 |
# Initialize Flask app
|
13 |
app = Flask(__name__)
|
14 |
-
CORS(app)
|
15 |
-
print("\nHello, welcome to SemaBox\n", flush=True)
|
16 |
|
|
|
17 |
|
18 |
-
def transcribe(
|
19 |
-
|
20 |
-
|
21 |
-
# load audio and pad/trim it to fit 30 seconds
|
22 |
-
audio = whisper.load_audio(audio)
|
23 |
audio = whisper.pad_or_trim(audio)
|
24 |
|
25 |
-
#
|
26 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
27 |
|
28 |
-
#
|
29 |
_, probs = model.detect_language(mel)
|
30 |
-
|
|
|
31 |
|
32 |
-
#
|
33 |
-
options = whisper.DecodingOptions(fp16
|
34 |
result = whisper.decode(model, mel, options)
|
35 |
-
return result.text
|
36 |
-
|
37 |
@app.route("/")
|
38 |
def hello():
|
39 |
return "Semabox, listens to you!"
|
40 |
|
41 |
-
@app.route('/whisper', methods=['POST'])
|
42 |
-
def sema():
|
43 |
-
if 'audio' not in request.files:
|
44 |
-
# If no audio file is submitted, return a 400 (Bad Request) error.
|
45 |
-
abort(400, description="No audio file provided")
|
46 |
-
|
47 |
-
audio_file = request.files['audio']
|
48 |
-
|
49 |
-
# Create a temporary file to save the uploaded audio.
|
50 |
-
with NamedTemporaryFile(suffix=".wav", delete=True) as temp:
|
51 |
-
audio_file.save(temp.name)
|
52 |
-
|
53 |
-
# Perform transcription using the Whisper model.
|
54 |
-
result = model.transcribe(temp.name)
|
55 |
-
|
56 |
-
# Return the transcribed text in JSON format.
|
57 |
-
return jsonify({
|
58 |
-
'filename': audio_file.filename,
|
59 |
-
'transcript': result['text'],
|
60 |
-
})
|
61 |
-
|
62 |
-
|
63 |
-
# Define the route for transcription
|
64 |
@app.route('/transcribe', methods=['POST'])
|
65 |
def transcribe_audio():
|
|
|
|
|
|
|
|
|
66 |
# Check if an audio file is included in the request
|
67 |
if 'audio' not in request.files:
|
68 |
return jsonify({"error": "No audio file provided"}), 400
|
@@ -74,7 +50,11 @@ def transcribe_audio():
|
|
74 |
audio_file.save(audio_path)
|
75 |
|
76 |
# Transcribe the audio
|
77 |
-
|
|
|
|
|
|
|
|
|
78 |
|
79 |
# Clean up the saved file
|
80 |
os.remove(audio_path)
|
|
|
2 |
import whisper
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
5 |
# Load Whisper model
|
6 |
print("\nLoading Whisper\n", flush=True)
|
7 |
model = whisper.load_model("small")
|
8 |
|
9 |
# Initialize Flask app
|
10 |
app = Flask(__name__)
|
|
|
|
|
11 |
|
12 |
+
print("\nHello, welcome to SemaBox\n", flush=True)
|
13 |
|
14 |
+
def transcribe(audio_path):
|
15 |
+
# Load audio and pad/trim it to fit 30 seconds
|
16 |
+
audio = whisper.load_audio(audio_path)
|
|
|
|
|
17 |
audio = whisper.pad_or_trim(audio)
|
18 |
|
19 |
+
# Make log-Mel spectrogram and move to the same device as the model
|
20 |
mel = whisper.log_mel_spectrogram(audio).to(model.device)
|
21 |
|
22 |
+
# Detect the spoken language
|
23 |
_, probs = model.detect_language(mel)
|
24 |
+
detected_language = max(probs, key=probs.get)
|
25 |
+
print(f"Detected language: {detected_language}")
|
26 |
|
27 |
+
# Decode the audio
|
28 |
+
options = whisper.DecodingOptions(fp16=False)
|
29 |
result = whisper.decode(model, mel, options)
|
30 |
+
return result.text, detected_language
|
31 |
+
|
32 |
@app.route("/")
|
33 |
def hello():
|
34 |
return "Semabox, listens to you!"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
@app.route('/transcribe', methods=['POST'])
|
37 |
def transcribe_audio():
|
38 |
+
# Ensure the temp_audio directory exists
|
39 |
+
if not os.path.exists("temp_audio"):
|
40 |
+
os.makedirs("temp_audio")
|
41 |
+
|
42 |
# Check if an audio file is included in the request
|
43 |
if 'audio' not in request.files:
|
44 |
return jsonify({"error": "No audio file provided"}), 400
|
|
|
50 |
audio_file.save(audio_path)
|
51 |
|
52 |
# Transcribe the audio
|
53 |
+
try:
|
54 |
+
transcription, language = transcribe(audio_path)
|
55 |
+
except Exception as e:
|
56 |
+
# Handle any errors during transcription
|
57 |
+
return jsonify({"error": str(e)}), 500
|
58 |
|
59 |
# Clean up the saved file
|
60 |
os.remove(audio_path)
|