Spaces:
Running
Running
fixed static file paths
Browse files
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 |
-
|
|
|
|
|
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 |
-
|
411 |
-
|
412 |
-
|
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 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
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(
|
452 |
-
raise RuntimeError(f"Failed to clear outputs directory: {str(e)}")
|
453 |
else:
|
454 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
455 |
-
logger.info(
|
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'
|
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 |
-
|
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 |
|