diff --git a/app/app.py b/app/app.py index e65d7b1..8bd1ab3 100644 --- a/app/app.py +++ b/app/app.py @@ -1,11 +1,9 @@ from flask import Flask, render_template, Response, request -from main import get_auto_transcript, get_video_id, create_and_stream, log, output_stream, awaiter +from main import get_auto_transcript, get_video_id, create_and_stream, log, output_stream, awaiter from asyncio import sleep from datetime import datetime import threading, pytz - - app = Flask(__name__, static_folder="website/static", template_folder="website") @app.route('/') @@ -14,49 +12,51 @@ def home(): @app.route('/process_url', methods=['POST']) def process_url(): - global thread - log(f"\n\n\n## New Entry at {datetime.now(pytz.timezone('America/New_York')).strftime('%Y-%m-%d %H:%M:%S')}\n\n") - url = request.form['url'] + url = request.form.get('url', '').strip() + if not url: + log("No URL provided.\n") + return "No URL provided.", 400 + + log(f"\n\n\n## New Entry at {datetime.now(pytz.timezone('America/New_York')).strftime('%Y-%m-%d %H:%M:%S')}\n") log(f"URL: {url}\n") + # Extract the video ID from the URL - video_id = get_video_id(url) # Modify this function to accept the URL + video_id = get_video_id(url) if not video_id: - log(f"Could not parse video id from URL: {url}") - return "Couldn't parse video ID from URL. (Are you sure you entered a valid YouTube.com or YouTu.be URL?)" - log(f"Video ID: {video_id}\n\n") - + log(f"Could not parse video id from URL: {url}\n") + return "Couldn't parse video ID from URL. (Ensure it's a valid YouTube.com or YouTu.be URL.)", 400 + log(f"Video ID: {video_id}\n") + # Get the transcript for that video ID transcript = get_auto_transcript(video_id) - if (not transcript): - log("## Error: could not retrieve transcript, Assistant won't be called.") - return "Successfully parsed video ID from URL, however the ID was either invalid, the transcript was disabled by the video owner, or some other error was raised because of YouTube." - - thread = threading.Thread(name="create_stream", target=create_and_stream, args=(transcript,)) # The comma here is very intentional, it's so that it iterates it as a tuple rather than iterateing the string. - log("Stream preperation complete, sending reply...\n\n") - return Response("Processing started. Check /stream_output for updates.", content_type='text/plain', status=200) # Add more detailed output if needed + if not transcript: + log("## Error: Could not retrieve transcript. Assistant won't be called.\n") + return "Parsed video ID, but transcript retrieval failed (might be disabled by the video owner).", 400 + + # Start the stream thread + thread = threading.Thread(target=create_and_stream, args=(transcript,)) + thread.start() + log("Stream preparation complete, sending reply...\n") + return Response("Processing started. Check /stream_output for updates.", content_type='text/plain', status=200) @app.route('/stream_output') -def stream_output(): - def yoink(): +def stream_output_route(): + def generate(): log("
\nStarting stream thread...\n\n") - thread.start() + # Start streaming output from output_stream - log("Starting to stream output.") + log("Starting to stream output.\n") while not output_stream.done: - if output_stream.buffer != []: + if output_stream.buffer: delta = output_stream.buffer.pop(0) - yield bytes(delta, encoding="utf-8") + yield delta.encode("utf-8") else: awaiter(sleep(0.05)) - log(f"\nStream successfully completely.\n\n
\n\n---\n\n### Completed Assistant Response:\n{output_stream.response}\n\n---\n\n") + log(f"\nStream completed.\n\n\n---\n\n### Completed Assistant Response:\n{output_stream.response}\n\n---\n\n") output_stream.reset() - thread.join() - log("\n### Task completed sucessfully without errors!") - return - return Response(yoink(), content_type='text/plain', status=200) + log("\n### Task completed successfully without errors!\n") + + return Response(generate(), content_type='text/plain', status=200) - - - -if __name__ == '__main__': - app.run(debug=True) \ No newline at end of file +if __name__ == '__main__': + app.run(debug=True, host='0.0.0.0', port=1986)