Nechba commited on
Commit
fa58d25
·
verified ·
1 Parent(s): 62e6d01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -31
app.py CHANGED
@@ -20,50 +20,37 @@ ner_model = AutoModelForTokenClassification.from_pretrained("dslim/bert-base-NER
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:
34
  return jsonify({'error': 'No file uploaded'}), 400
35
-
36
  file = request.files['file']
37
-
 
38
  if file.filename == '':
39
  return jsonify({'error': 'No selected file'}), 400
40
-
41
- if not allowed_file(file.filename):
42
- return jsonify({'error': 'Unsupported file type'}), 400
43
 
44
  try:
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():
 
20
  ner_pipeline = pipeline("ner", model=ner_model, tokenizer=ner_tokenizer) # Renamed variable
21
 
22
 
 
 
 
 
 
 
 
 
23
  @app.route('/transcribe', methods=['POST'])
24
  def transcribe_audio():
25
+ # Check if a file was uploaded
26
  if 'file' not in request.files:
27
  return jsonify({'error': 'No file uploaded'}), 400
28
+
29
  file = request.files['file']
30
+
31
+ # Check if the file is empty
32
  if file.filename == '':
33
  return jsonify({'error': 'No selected file'}), 400
 
 
 
34
 
35
  try:
36
+ # Save the uploaded file to a temporary file
37
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
38
+ file.save(temp_audio)
39
+ temp_path = temp_audio.name
40
+
41
+ # Transcribe the audio using Whisper
42
+ result = whisper_model.transcribe(temp_path)
 
 
 
43
  transcription = result["text"]
44
+
45
+ # Clean up the temporary file
46
+ os.remove(temp_path)
47
+
 
 
 
48
  return jsonify({'transcription': transcription})
49
+
50
  except Exception as e:
51
  return jsonify({'error': str(e)}), 500
52
+
53
+
54
 
55
  @app.route('/classify', methods=['POST'])
56
  def classify():