geethareddy commited on
Commit
9fcd178
·
verified ·
1 Parent(s): 0d363f8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -5
app.py CHANGED
@@ -2,6 +2,7 @@ from flask import Flask, render_template, request, jsonify
2
  import os
3
  import torch
4
  import re
 
5
  from transformers import pipeline
6
  from gtts import gTTS
7
  from pydub import AudioSegment
@@ -44,6 +45,14 @@ SYMBOL_MAPPING = {
44
  "space": " "
45
  }
46
 
 
 
 
 
 
 
 
 
47
  # Function to extract meaningful words (Removes unnecessary phrases)
48
  def extract_meaningful_text(text):
49
  text = text.lower().strip()
@@ -75,16 +84,20 @@ def transcribe():
75
  return jsonify({"error": "No audio file provided"}), 400
76
 
77
  audio_file = request.files["audio"]
78
- audio_path = os.path.join("static", "temp.wav")
79
- audio_file.save(audio_path)
 
80
 
81
  try:
 
 
 
82
  # Check if the audio contains valid speech
83
- if is_silent_audio(audio_path):
84
  return jsonify({"error": "No speech detected. Please try again."}), 400
85
 
86
  # Transcribe using Whisper
87
- result = asr_model.transcribe(audio_path, language="en")
88
  transcribed_text = clean_transcription(result["text"])
89
 
90
  return jsonify({"text": transcribed_text})
@@ -93,4 +106,4 @@ def transcribe():
93
 
94
  # Use Waitress for Production Server
95
  if __name__ == "__main__":
96
- serve(app, host="0.0.0.0", port=7860)
 
2
  import os
3
  import torch
4
  import re
5
+ import ffmpeg # Ensure FFmpeg is installed
6
  from transformers import pipeline
7
  from gtts import gTTS
8
  from pydub import AudioSegment
 
45
  "space": " "
46
  }
47
 
48
+ # Function to convert audio to WAV format (Fix FFmpeg error)
49
+ def convert_to_wav(input_path, output_path):
50
+ try:
51
+ audio = AudioSegment.from_file(input_path)
52
+ audio.export(output_path, format="wav")
53
+ except Exception as e:
54
+ raise Exception(f"Audio conversion failed: {str(e)}")
55
+
56
  # Function to extract meaningful words (Removes unnecessary phrases)
57
  def extract_meaningful_text(text):
58
  text = text.lower().strip()
 
84
  return jsonify({"error": "No audio file provided"}), 400
85
 
86
  audio_file = request.files["audio"]
87
+ input_audio_path = os.path.join("static", "temp_input")
88
+ output_audio_path = os.path.join("static", "temp.wav")
89
+ audio_file.save(input_audio_path)
90
 
91
  try:
92
+ # Convert audio to proper WAV format
93
+ convert_to_wav(input_audio_path, output_audio_path)
94
+
95
  # Check if the audio contains valid speech
96
+ if is_silent_audio(output_audio_path):
97
  return jsonify({"error": "No speech detected. Please try again."}), 400
98
 
99
  # Transcribe using Whisper
100
+ result = asr_model.transcribe(output_audio_path, language="en")
101
  transcribed_text = clean_transcription(result["text"])
102
 
103
  return jsonify({"text": transcribed_text})
 
106
 
107
  # Use Waitress for Production Server
108
  if __name__ == "__main__":
109
+ serve(app, host="0.0.0.0", port=7860)