Jaward commited on
Commit
126a36b
·
verified ·
1 Parent(s): 2c703fe

fixed audio src bug

Browse files
Files changed (1) hide show
  1. app.py +34 -18
app.py CHANGED
@@ -32,8 +32,6 @@ import markdown
32
  import PyPDF2
33
  import io
34
  import copy
35
- from pathlib import Path
36
- import time
37
 
38
  def get_instructor_name(speaker):
39
  instructor_names = {
@@ -409,7 +407,14 @@ def create_zip_of_files(file_paths):
409
  # Access local files
410
  def get_gradio_file_url(local_path):
411
  relative_path = os.path.relpath(local_path, os.getcwd())
412
- print(f"FILE PATH: {relative_path}")
 
 
 
 
 
 
 
413
  return f"/gradio_api/file={relative_path}"
414
 
415
  # Async generate lecture materials and audio
@@ -425,24 +430,30 @@ async def on_generate(api_service, api_key, serpapi_key, title, lecture_content_
425
  instructor_name = get_instructor_name(speaker)
426
  logger.info(f"Using instructor: {instructor_name}")
427
 
428
- # Clear output directory and cache
429
  if os.path.exists(OUTPUT_DIR):
430
  try:
 
 
431
  for item in os.listdir(OUTPUT_DIR):
432
  item_path = os.path.join(OUTPUT_DIR, item)
433
- if os.path.isfile(item_path):
434
- os.unlink(item_path)
435
- elif os.path.isdir(item_path):
436
- shutil.rmtree(item_path)
 
 
 
 
 
 
437
  except Exception as e:
438
- logger.error(f"Error clearing output directory: {e}")
439
- raise gr.Error(f"Failed to clear output directory: {str(e)}")
440
  else:
441
- os.makedirs(OUTPUT_DIR)
442
-
443
- # Clear browser cache by adding timestamp to file URLs
444
- cache_buster = int(time.time())
445
-
446
  # Total slides include user-specified content slides plus Introduction and Closing slides
447
  content_slides = num_slides
448
  total_slides = content_slides + 2
@@ -830,7 +841,7 @@ Example: 'Received {total_slides} slides, {total_slides} scripts, and HTML files
830
  for i in range(len(scripts)):
831
  audio_timeline += f'<audio id="audio-{i+1}" controls src="" style="display: inline-block; margin: 0 10px; width: 200px;"><span>Loading...</span></audio>'
832
 
833
- file_paths = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(('.md', '.txt'))]
834
  file_paths.sort()
835
  file_paths = [os.path.join(OUTPUT_DIR, f) for f in file_paths]
836
 
@@ -928,7 +939,9 @@ Example: 'Received {total_slides} slides, {total_slides} scripts, and HTML files
928
  audio_timeline = ""
929
  for j, url in enumerate(audio_urls):
930
  if url:
931
- audio_timeline += f'<audio id="audio-{j+1}" controls src="{url}" style="display: inline-block; margin: 0 10px; width: 200px;"></audio>'
 
 
932
  else:
933
  audio_timeline += f'<audio id="audio-{j+1}" controls src="" style="display: inline-block; margin: 0 10px; width: 200px;"><span>Audio unavailable</span></audio>'
934
 
@@ -2306,8 +2319,11 @@ with gr.Blocks(
2306
  "audience": audience_val or "University"
2307
  }
2308
 
2309
- def show_note_content(evt: dict, notes):
2310
  # evt['index'] gives the row index
 
 
 
2311
  idx = evt.get('index', 0)
2312
  if 0 <= idx < len(notes):
2313
  note = notes[idx]
 
32
  import PyPDF2
33
  import io
34
  import copy
 
 
35
 
36
  def get_instructor_name(speaker):
37
  instructor_names = {
 
407
  # Access local files
408
  def get_gradio_file_url(local_path):
409
  relative_path = os.path.relpath(local_path, os.getcwd())
410
+ print(f"Relative path: {relative_path}")
411
+
412
+ # Handle audio files differently for Hugging Face deployment
413
+ if relative_path.endswith(('.mp3', '.wav')):
414
+ # For audio files, use the static file serving
415
+ return f"/file/{relative_path}"
416
+
417
+ # For other files, use the gradio API
418
  return f"/gradio_api/file={relative_path}"
419
 
420
  # Async generate lecture materials and audio
 
430
  instructor_name = get_instructor_name(speaker)
431
  logger.info(f"Using instructor: {instructor_name}")
432
 
433
+ # Clear the outputs directory with detailed logging
434
  if os.path.exists(OUTPUT_DIR):
435
  try:
436
+ logger.info(f"Starting to clear outputs directory: {OUTPUT_DIR}")
437
+ cleared_files = []
438
  for item in os.listdir(OUTPUT_DIR):
439
  item_path = os.path.join(OUTPUT_DIR, item)
440
+ try:
441
+ if os.path.isfile(item_path):
442
+ os.unlink(item_path)
443
+ cleared_files.append(f"Deleted file: {item}")
444
+ elif os.path.isdir(item_path):
445
+ shutil.rmtree(item_path)
446
+ cleared_files.append(f"Deleted directory: {item}")
447
+ except Exception as e:
448
+ logger.error(f"Failed to delete {item_path}: {str(e)}")
449
+ logger.info(f"Successfully cleared outputs directory. Deleted items: {', '.join(cleared_files)}")
450
  except Exception as e:
451
+ logger.error(f"Failed to clear outputs directory: {str(e)}")
452
+ raise RuntimeError(f"Failed to clear outputs directory: {str(e)}")
453
  else:
454
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
455
+ logger.info(f"Created outputs directory: {OUTPUT_DIR}")
456
+
 
 
457
  # Total slides include user-specified content slides plus Introduction and Closing slides
458
  content_slides = num_slides
459
  total_slides = content_slides + 2
 
841
  for i in range(len(scripts)):
842
  audio_timeline += f'<audio id="audio-{i+1}" controls src="" style="display: inline-block; margin: 0 10px; width: 200px;"><span>Loading...</span></audio>'
843
 
844
+ file_paths = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(('.md', '.txt', '.mp3', '.wav'))]
845
  file_paths.sort()
846
  file_paths = [os.path.join(OUTPUT_DIR, f) for f in file_paths]
847
 
 
939
  audio_timeline = ""
940
  for j, url in enumerate(audio_urls):
941
  if url:
942
+ # Ensure audio URL is properly formatted for Hugging Face deployment
943
+ audio_url = get_gradio_file_url(url)
944
+ audio_timeline += f'<audio id="audio-{j+1}" controls src="{audio_url}" style="display: inline-block; margin: 0 10px; width: 200px;"></audio>'
945
  else:
946
  audio_timeline += f'<audio id="audio-{j+1}" controls src="" style="display: inline-block; margin: 0 10px; width: 200px;"><span>Audio unavailable</span></audio>'
947
 
 
2319
  "audience": audience_val or "University"
2320
  }
2321
 
2322
+ def show_note_content(evt: dict, notes=None):
2323
  # evt['index'] gives the row index
2324
+ if not notes:
2325
+ return gr.update(value="Click any button above to generate content...")
2326
+
2327
  idx = evt.get('index', 0)
2328
  if 0 <= idx < len(notes):
2329
  note = notes[idx]