Files
screw-bardo/app/app.py
2024-12-21 13:49:16 -05:00

63 lines
2.7 KiB
Python

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 asyncio import sleep
from datetime import datetime
import pytz
import threading
app = Flask(__name__, static_folder="website/static", template_folder="website")
@app.route('/')
def home():
return render_template('index.html')
@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']
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
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")
# 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
@app.route('/stream_output')
def stream_output():
def yoink():
log("<details>\n<summary>Starting stream thread...</summary>\n\n")
thread.start()
# Start streaming output from output_stream
log("Starting to stream output.")
while not output_stream.done:
if output_stream.buffer != []:
delta = output_stream.buffer.pop(0)
yield bytes(delta, encoding="utf-8")
else:
awaiter(sleep(0.05))
log(f"\nStream successfully completely.\n\n</details>\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)
if __name__ == '__main__':
app.run(debug=True)