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

fixed static file paths

Browse files
Files changed (1) hide show
  1. app.py +22 -33
app.py CHANGED
@@ -32,6 +32,7 @@ import markdown
32
  import PyPDF2
33
  import io
34
  import copy
 
35
 
36
  def get_instructor_name(speaker):
37
  instructor_names = {
@@ -54,14 +55,16 @@ logging.basicConfig(
54
  )
55
  logger = logging.getLogger(__name__)
56
 
57
- # Set up environment
58
- OUTPUT_DIR = os.path.join(os.getcwd(), "outputs")
59
- UPLOAD_DIR = os.path.join(os.getcwd(), "uploads")
60
  os.makedirs(OUTPUT_DIR, exist_ok=True)
61
  os.makedirs(UPLOAD_DIR, exist_ok=True)
62
  logger.info(f"Using output directory: {OUTPUT_DIR}")
63
  logger.info(f"Using upload directory: {UPLOAD_DIR}")
64
- os.environ["COQUI_TOS_AGREED"] = "1"
 
 
65
 
66
  # Initialize TTS model
67
  device = "cuda" if torch.cuda.is_available() else "cpu"
@@ -406,16 +409,13 @@ def create_zip_of_files(file_paths):
406
 
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
421
  async def on_generate(api_service, api_key, serpapi_key, title, lecture_content_description, lecture_type, lecture_style, speaker_audio, num_slides):
@@ -430,29 +430,20 @@ async def on_generate(api_service, api_key, serpapi_key, title, lecture_content_
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
@@ -841,7 +832,7 @@ Example: 'Received {total_slides} slides, {total_slides} scripts, and HTML files
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,9 +930,7 @@ Example: 'Received {total_slides} slides, {total_slides} scripts, and HTML files
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
 
 
32
  import PyPDF2
33
  import io
34
  import copy
35
+ from pathlib import Path
36
 
37
  def get_instructor_name(speaker):
38
  instructor_names = {
 
55
  )
56
  logger = logging.getLogger(__name__)
57
 
58
+ # Set up environment with absolute paths
59
+ OUTPUT_DIR = os.path.abspath(os.path.join(os.getcwd(), "outputs"))
60
+ UPLOAD_DIR = os.path.abspath(os.path.join(os.getcwd(), "uploads"))
61
  os.makedirs(OUTPUT_DIR, exist_ok=True)
62
  os.makedirs(UPLOAD_DIR, exist_ok=True)
63
  logger.info(f"Using output directory: {OUTPUT_DIR}")
64
  logger.info(f"Using upload directory: {UPLOAD_DIR}")
65
+
66
+ # Set up static paths for Gradio
67
+ gr.set_static_paths(paths=[Path(OUTPUT_DIR).absolute()])
68
 
69
  # Initialize TTS model
70
  device = "cuda" if torch.cuda.is_available() else "cpu"
 
409
 
410
  # Access local files
411
  def get_gradio_file_url(local_path):
412
+ if not local_path:
413
+ return None
414
+ # Get the relative path from the current working directory
415
  relative_path = os.path.relpath(local_path, os.getcwd())
416
+ final_path = f"/gradio_api/file={relative_path}"
417
+ print(f"File URL path: {final_path}")
418
+ return final_path
 
 
 
 
 
 
419
 
420
  # Async generate lecture materials and audio
421
  async def on_generate(api_service, api_key, serpapi_key, title, lecture_content_description, lecture_type, lecture_style, speaker_audio, num_slides):
 
430
  instructor_name = get_instructor_name(speaker)
431
  logger.info(f"Using instructor: {instructor_name}")
432
 
 
433
  if os.path.exists(OUTPUT_DIR):
434
  try:
 
 
435
  for item in os.listdir(OUTPUT_DIR):
436
  item_path = os.path.join(OUTPUT_DIR, item)
437
+ if os.path.isfile(item_path):
438
+ os.unlink(item_path)
439
+ elif os.path.isdir(item_path):
440
+ shutil.rmtree(item_path)
441
+ logger.info("Cleared outputs directory: %s", OUTPUT_DIR)
 
 
 
 
 
442
  except Exception as e:
443
+ logger.error("Failed to clear outputs directory: %s", str(e))
 
444
  else:
445
  os.makedirs(OUTPUT_DIR, exist_ok=True)
446
+ logger.info("Created outputs directory: %s", OUTPUT_DIR)
447
 
448
  # Total slides include user-specified content slides plus Introduction and Closing slides
449
  content_slides = num_slides
 
832
  for i in range(len(scripts)):
833
  audio_timeline += f'<audio id="audio-{i+1}" controls src="" style="display: inline-block; margin: 0 10px; width: 200px;"><span>Loading...</span></audio>'
834
 
835
+ file_paths = [f for f in os.listdir(OUTPUT_DIR) if f.endswith(('.md', '.txt'))]
836
  file_paths.sort()
837
  file_paths = [os.path.join(OUTPUT_DIR, f) for f in file_paths]
838
 
 
930
  audio_timeline = ""
931
  for j, url in enumerate(audio_urls):
932
  if url:
933
+ audio_timeline += f'<audio id="audio-{j+1}" controls src="{url}" style="display: inline-block; margin: 0 10px; width: 200px;"></audio>'
 
 
934
  else:
935
  audio_timeline += f'<audio id="audio-{j+1}" controls src="" style="display: inline-block; margin: 0 10px; width: 200px;"><span>Audio unavailable</span></audio>'
936