Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,8 @@ from transformers import AutoTokenizer, AutoModelForTokenClassification
|
|
| 4 |
import whisper
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
| 7 |
app = Flask(__name__)
|
| 8 |
|
| 9 |
# Initialize Whisper model
|
|
@@ -17,9 +19,15 @@ ner_tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
|
|
| 17 |
ner_model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER") # Renamed variable
|
| 18 |
ner_pipeline = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer) # Renamed variable
|
| 19 |
|
| 20 |
-
def allowed_file(filename):
|
| 21 |
-
return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'wav', 'mp3', 'ogg', 'flac', 'm4a'}
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
@app.route('/transcribe', methods=['POST'])
|
| 24 |
def transcribe_audio():
|
| 25 |
if 'file' not in request.files:
|
|
@@ -37,18 +45,26 @@ def transcribe_audio():
|
|
| 37 |
temp_path = "temp_audio"
|
| 38 |
file.save(temp_path)
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
transcription = result["text"]
|
| 43 |
|
|
|
|
| 44 |
if os.path.exists(temp_path):
|
| 45 |
os.remove(temp_path)
|
|
|
|
|
|
|
| 46 |
|
| 47 |
return jsonify({'transcription': transcription})
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
return jsonify({'error': str(e)}), 500
|
| 51 |
-
|
| 52 |
@app.route('/classify', methods=['POST'])
|
| 53 |
def classify():
|
| 54 |
try:
|
|
|
|
| 4 |
import whisper
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
import ffmpeg
|
| 8 |
+
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
# Initialize Whisper model
|
|
|
|
| 19 |
ner_model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER") # Renamed variable
|
| 20 |
ner_pipeline = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer) # Renamed variable
|
| 21 |
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def convert_audio(input_path, output_path):
|
| 24 |
+
try:
|
| 25 |
+
ffmpeg.input(input_path).output(output_path, acodec='pcm_s16le').run()
|
| 26 |
+
return True
|
| 27 |
+
except ffmpeg.Error as e:
|
| 28 |
+
print(f"FFmpeg error: {e.stderr.decode()}")
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
@app.route('/transcribe', methods=['POST'])
|
| 32 |
def transcribe_audio():
|
| 33 |
if 'file' not in request.files:
|
|
|
|
| 45 |
temp_path = "temp_audio"
|
| 46 |
file.save(temp_path)
|
| 47 |
|
| 48 |
+
# Convert audio to a format Whisper can process
|
| 49 |
+
converted_path = "converted_audio.wav"
|
| 50 |
+
if not convert_audio(temp_path, converted_path):
|
| 51 |
+
return jsonify({'error': 'Audio conversion failed'}), 500
|
| 52 |
+
|
| 53 |
+
# Transcribe the converted audio
|
| 54 |
+
result = whisper_model.transcribe(converted_path)
|
| 55 |
transcription = result["text"]
|
| 56 |
|
| 57 |
+
# Clean up temporary files
|
| 58 |
if os.path.exists(temp_path):
|
| 59 |
os.remove(temp_path)
|
| 60 |
+
if os.path.exists(converted_path):
|
| 61 |
+
os.remove(converted_path)
|
| 62 |
|
| 63 |
return jsonify({'transcription': transcription})
|
| 64 |
|
| 65 |
except Exception as e:
|
| 66 |
return jsonify({'error': str(e)}), 500
|
| 67 |
+
|
| 68 |
@app.route('/classify', methods=['POST'])
|
| 69 |
def classify():
|
| 70 |
try:
|