Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,10 @@
|
|
| 1 |
-
# app.py (Final version with
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import uuid
|
| 5 |
import subprocess
|
| 6 |
import threading
|
| 7 |
import os
|
| 8 |
-
import time
|
| 9 |
import sys
|
| 10 |
import re
|
| 11 |
import traceback
|
|
@@ -21,7 +20,6 @@ kokoro_voices_path = os.path.join(model_dir, "voices.bin")
|
|
| 21 |
if not os.path.exists(kokoro_model_path) or not os.path.exists(kokoro_voices_path):
|
| 22 |
print("Downloading Kokoro TTS models...")
|
| 23 |
os.makedirs(model_dir, exist_ok=True)
|
| 24 |
-
# Using specific wget commands for clarity and robustness
|
| 25 |
os.system(f"wget -O {kokoro_model_path} https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files/kokoro-v0_19.onnx")
|
| 26 |
os.system(f"wget -O {kokoro_voices_path} https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files/voices.bin")
|
| 27 |
print("Model download complete.")
|
|
@@ -38,21 +36,19 @@ def run_video_generation(task_id: str, topic: str, context: str, model: str):
|
|
| 38 |
|
| 39 |
# Sanitize topic name to create a valid directory/file prefix
|
| 40 |
file_prefix = re.sub(r'[^a-z0-9_]+', '_', topic.lower())
|
| 41 |
-
# The generate_video.py script will create this directory inside the general 'output' folder
|
| 42 |
output_dir = os.path.join("output", file_prefix)
|
| 43 |
|
| 44 |
-
# ---
|
|
|
|
| 45 |
command = [
|
| 46 |
"python", "-u", "generate_video.py", # '-u' for unbuffered output
|
| 47 |
"--model", model,
|
| 48 |
"--topic", topic,
|
| 49 |
"--context", context,
|
| 50 |
-
"--output_dir", output_dir
|
| 51 |
-
# Langfuse is disabled by not including the --use_langfuse flag
|
| 52 |
]
|
| 53 |
|
| 54 |
try:
|
| 55 |
-
# Use Popen to run the process in the background and stream output
|
| 56 |
process = subprocess.Popen(
|
| 57 |
command,
|
| 58 |
stdout=subprocess.PIPE,
|
|
@@ -62,17 +58,14 @@ def run_video_generation(task_id: str, topic: str, context: str, model: str):
|
|
| 62 |
universal_newlines=True,
|
| 63 |
)
|
| 64 |
|
| 65 |
-
# Read output line-by-line in real-time
|
| 66 |
for line in iter(process.stdout.readline, ''):
|
| 67 |
-
print(line, end='')
|
| 68 |
tasks[task_id]['log'] += line
|
| 69 |
|
| 70 |
-
process.wait()
|
| 71 |
|
| 72 |
if process.returncode == 0:
|
| 73 |
-
# Check for the final combined video file
|
| 74 |
final_video_path = os.path.join(output_dir, f"{file_prefix}_combined.mp4")
|
| 75 |
-
|
| 76 |
if os.path.exists(final_video_path):
|
| 77 |
tasks[task_id]['status'] = 'completed'
|
| 78 |
tasks[task_id]['video_path'] = final_video_path
|
|
@@ -80,11 +73,11 @@ def run_video_generation(task_id: str, topic: str, context: str, model: str):
|
|
| 80 |
else:
|
| 81 |
tasks[task_id]['status'] = 'failed'
|
| 82 |
tasks[task_id]['error'] = "Script finished, but the final combined video file was not found."
|
| 83 |
-
tasks[task_id]['log'] += f"\nβ Error: Output video not found at {final_video_path}"
|
| 84 |
else:
|
| 85 |
tasks[task_id]['status'] = 'failed'
|
| 86 |
tasks[task_id]['error'] = f"Process failed with return code {process.returncode}."
|
| 87 |
-
tasks[task_id]['log'] += f"\nβ Error: Process failed.
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
print(f"Caught an exception: {e}")
|
|
@@ -109,7 +102,7 @@ def start_generation(topic: str, context: str, model: str):
|
|
| 109 |
|
| 110 |
def check_status(task_id: str):
|
| 111 |
if not task_id:
|
| 112 |
-
return "Please
|
| 113 |
|
| 114 |
task = tasks.get(task_id)
|
| 115 |
if not task:
|
|
@@ -131,8 +124,7 @@ def check_status(task_id: str):
|
|
| 131 |
status_message = f"π Status: {status} (Model: {model})"
|
| 132 |
return status_message, None, log
|
| 133 |
|
| 134 |
-
|
| 135 |
-
with gr.Blocks(css="footer {display: none !important}", title="Theorem Explain Agent") as demo:
|
| 136 |
gr.Markdown("# π Theorem Explain Agent: Video Generation")
|
| 137 |
gr.Markdown("Generate educational videos explaining mathematical theorems and concepts. This may take several minutes.")
|
| 138 |
|
|
@@ -163,7 +155,6 @@ with gr.Blocks(css="footer {display: none !important}", title="Theorem Explain A
|
|
| 163 |
video_output = gr.Video(label="Generated Video", interactive=False)
|
| 164 |
log_display = gr.Textbox(label="Live Generation Logs", lines=15, interactive=False)
|
| 165 |
|
| 166 |
-
# Connect the functions to the interface
|
| 167 |
start_button.click(
|
| 168 |
fn=start_generation,
|
| 169 |
inputs=[topic_input, context_input, model_input],
|
|
@@ -174,9 +165,7 @@ with gr.Blocks(css="footer {display: none !important}", title="Theorem Explain A
|
|
| 174 |
fn=check_status,
|
| 175 |
inputs=[task_id_input],
|
| 176 |
outputs=[status_display, video_output, log_display],
|
| 177 |
-
# Every 2 seconds, poll the status
|
| 178 |
every=2
|
| 179 |
)
|
| 180 |
|
| 181 |
-
# Launch the app
|
| 182 |
demo.launch()
|
|
|
|
| 1 |
+
# app.py (Final version with directory fix and live logging)
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
import uuid
|
| 5 |
import subprocess
|
| 6 |
import threading
|
| 7 |
import os
|
|
|
|
| 8 |
import sys
|
| 9 |
import re
|
| 10 |
import traceback
|
|
|
|
| 20 |
if not os.path.exists(kokoro_model_path) or not os.path.exists(kokoro_voices_path):
|
| 21 |
print("Downloading Kokoro TTS models...")
|
| 22 |
os.makedirs(model_dir, exist_ok=True)
|
|
|
|
| 23 |
os.system(f"wget -O {kokoro_model_path} https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files/kokoro-v0_19.onnx")
|
| 24 |
os.system(f"wget -O {kokoro_voices_path} https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files/voices.bin")
|
| 25 |
print("Model download complete.")
|
|
|
|
| 36 |
|
| 37 |
# Sanitize topic name to create a valid directory/file prefix
|
| 38 |
file_prefix = re.sub(r'[^a-z0-9_]+', '_', topic.lower())
|
|
|
|
| 39 |
output_dir = os.path.join("output", file_prefix)
|
| 40 |
|
| 41 |
+
# --- THIS IS THE FIX ---
|
| 42 |
+
# The command now correctly passes the specific output directory.
|
| 43 |
command = [
|
| 44 |
"python", "-u", "generate_video.py", # '-u' for unbuffered output
|
| 45 |
"--model", model,
|
| 46 |
"--topic", topic,
|
| 47 |
"--context", context,
|
| 48 |
+
"--output_dir", output_dir # Use the variable, not the string "output"
|
|
|
|
| 49 |
]
|
| 50 |
|
| 51 |
try:
|
|
|
|
| 52 |
process = subprocess.Popen(
|
| 53 |
command,
|
| 54 |
stdout=subprocess.PIPE,
|
|
|
|
| 58 |
universal_newlines=True,
|
| 59 |
)
|
| 60 |
|
|
|
|
| 61 |
for line in iter(process.stdout.readline, ''):
|
| 62 |
+
print(line, end='')
|
| 63 |
tasks[task_id]['log'] += line
|
| 64 |
|
| 65 |
+
process.wait()
|
| 66 |
|
| 67 |
if process.returncode == 0:
|
|
|
|
| 68 |
final_video_path = os.path.join(output_dir, f"{file_prefix}_combined.mp4")
|
|
|
|
| 69 |
if os.path.exists(final_video_path):
|
| 70 |
tasks[task_id]['status'] = 'completed'
|
| 71 |
tasks[task_id]['video_path'] = final_video_path
|
|
|
|
| 73 |
else:
|
| 74 |
tasks[task_id]['status'] = 'failed'
|
| 75 |
tasks[task_id]['error'] = "Script finished, but the final combined video file was not found."
|
| 76 |
+
tasks[task_id]['log'] += f"\nβ Error: Output video not found at {final_video_path}. Check full logs."
|
| 77 |
else:
|
| 78 |
tasks[task_id]['status'] = 'failed'
|
| 79 |
tasks[task_id]['error'] = f"Process failed with return code {process.returncode}."
|
| 80 |
+
tasks[task_id]['log'] += f"\nβ Error: Process failed. See logs above for details."
|
| 81 |
|
| 82 |
except Exception as e:
|
| 83 |
print(f"Caught an exception: {e}")
|
|
|
|
| 102 |
|
| 103 |
def check_status(task_id: str):
|
| 104 |
if not task_id:
|
| 105 |
+
return "Please enter a Task ID.", None, "Please enter a Task ID above and click 'Check Status'."
|
| 106 |
|
| 107 |
task = tasks.get(task_id)
|
| 108 |
if not task:
|
|
|
|
| 124 |
status_message = f"π Status: {status} (Model: {model})"
|
| 125 |
return status_message, None, log
|
| 126 |
|
| 127 |
+
with gr.Blocks(title="Theorem Explain Agent") as demo:
|
|
|
|
| 128 |
gr.Markdown("# π Theorem Explain Agent: Video Generation")
|
| 129 |
gr.Markdown("Generate educational videos explaining mathematical theorems and concepts. This may take several minutes.")
|
| 130 |
|
|
|
|
| 155 |
video_output = gr.Video(label="Generated Video", interactive=False)
|
| 156 |
log_display = gr.Textbox(label="Live Generation Logs", lines=15, interactive=False)
|
| 157 |
|
|
|
|
| 158 |
start_button.click(
|
| 159 |
fn=start_generation,
|
| 160 |
inputs=[topic_input, context_input, model_input],
|
|
|
|
| 165 |
fn=check_status,
|
| 166 |
inputs=[task_id_input],
|
| 167 |
outputs=[status_display, video_output, log_display],
|
|
|
|
| 168 |
every=2
|
| 169 |
)
|
| 170 |
|
|
|
|
| 171 |
demo.launch()
|