bluenevus commited on
Commit
f97123b
·
verified ·
1 Parent(s): c61e81a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -12
app.py CHANGED
@@ -19,7 +19,7 @@ logger = logging.getLogger(__name__)
19
 
20
  # Try to import moviepy with the simpler import statement
21
  try:
22
- from moviepy import VideoFileClip, AudioFileClip
23
  logger.info("MoviePy (VideoFileClip) successfully imported")
24
  except ImportError as e:
25
  logger.error(f"Error importing MoviePy (VideoFileClip): {str(e)}")
@@ -86,11 +86,12 @@ def chunk_audio(audio_segment, chunk_size_ms=60000):
86
  def transcribe_audio_chunks(chunks):
87
  transcriptions = []
88
  for chunk in chunks:
89
- with io.BytesIO() as audio_file:
90
- chunk.export(audio_file, format="wav")
91
- audio_file.seek(0)
92
- transcript = openai.Audio.transcribe("whisper-1", audio_file)
93
- transcriptions.append(transcript.get('text', ''))
 
94
  return ' '.join(transcriptions)
95
 
96
  def download_file(url):
@@ -117,14 +118,16 @@ def process_media(file_path, is_url=False):
117
  return f"Error downloading URL content: {str(e)}", False
118
  else:
119
  logger.info("Processing uploaded file")
120
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp4')
121
- temp_file.write(file_path)
 
 
122
  temp_file.close()
123
  temp_file = temp_file.name
124
  logger.info(f"Uploaded file saved: {temp_file}")
125
 
126
  # Convert to WAV using ffmpeg
127
- wav_path = temp_file + ".wav"
128
  try:
129
  subprocess.run(['ffmpeg', '-i', temp_file, '-acodec', 'pcm_s16le', '-ar', '44100', wav_path], check=True)
130
  logger.info(f"Audio extracted to WAV: {wav_path}")
@@ -174,9 +177,7 @@ def update_output(contents, n_clicks, filename, url):
174
  transcription_preview = ""
175
 
176
  if contents is not None:
177
- content_type, content_string = contents.split(',')
178
- decoded = base64.b64decode(content_string)
179
- status_message, success = process_media(decoded)
180
  elif url:
181
  status_message, success = process_media(url, is_url=True)
182
  else:
 
19
 
20
  # Try to import moviepy with the simpler import statement
21
  try:
22
+ from moviepy.editor import VideoFileClip, AudioFileClip
23
  logger.info("MoviePy (VideoFileClip) successfully imported")
24
  except ImportError as e:
25
  logger.error(f"Error importing MoviePy (VideoFileClip): {str(e)}")
 
86
  def transcribe_audio_chunks(chunks):
87
  transcriptions = []
88
  for chunk in chunks:
89
+ with tempfile.NamedTemporaryFile(suffix='.wav', delete=False) as temp_audio_file:
90
+ chunk.export(temp_audio_file.name, format="wav")
91
+ with open(temp_audio_file.name, 'rb') as audio_file:
92
+ transcript = openai.Audio.transcribe("whisper-1", audio_file)
93
+ transcriptions.append(transcript.get('text', ''))
94
+ os.unlink(temp_audio_file.name)
95
  return ' '.join(transcriptions)
96
 
97
  def download_file(url):
 
118
  return f"Error downloading URL content: {str(e)}", False
119
  else:
120
  logger.info("Processing uploaded file")
121
+ content_type, content_string = file_path.split(',')
122
+ decoded = base64.b64decode(content_string)
123
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.tmp')
124
+ temp_file.write(decoded)
125
  temp_file.close()
126
  temp_file = temp_file.name
127
  logger.info(f"Uploaded file saved: {temp_file}")
128
 
129
  # Convert to WAV using ffmpeg
130
+ wav_path = tempfile.NamedTemporaryFile(delete=False, suffix='.wav').name
131
  try:
132
  subprocess.run(['ffmpeg', '-i', temp_file, '-acodec', 'pcm_s16le', '-ar', '44100', wav_path], check=True)
133
  logger.info(f"Audio extracted to WAV: {wav_path}")
 
177
  transcription_preview = ""
178
 
179
  if contents is not None:
180
+ status_message, success = process_media(contents)
 
 
181
  elif url:
182
  status_message, success = process_media(url, is_url=True)
183
  else: