Somewhat messy, but ready for production.
This commit is contained in:
30
app.py
30
app.py
@@ -1,10 +1,8 @@
|
||||
from flask import Flask, render_template, Response, request
|
||||
from main import get_auto_transcript, get_video_id, create_and_stream, output_stream, fake_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
|
||||
import pytz
|
||||
import time
|
||||
import threading, pytz
|
||||
|
||||
|
||||
|
||||
@@ -16,44 +14,44 @@ def home():
|
||||
|
||||
@app.route('/process_url', methods=['POST'])
|
||||
def process_url():
|
||||
# Opens a file to log the video id and the assistants respone to see if I can further improve instructions:
|
||||
#log = open("log.txt", "at", 1)
|
||||
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']
|
||||
if url == "test":
|
||||
global thread
|
||||
thread = threading.Thread(name="test_thread", target=fake_stream)
|
||||
return Response("teehee", status=200)
|
||||
|
||||
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.
|
||||
|
||||
return Response("Processing started. Check /stream_output for updates.", status=200) # Add more detailed output if needed
|
||||
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():
|
||||
print("Starting stream thread.")
|
||||
log("<details>\n<summary>Starting stream thread...</summary>\n\n")
|
||||
thread.start()
|
||||
# Start streaming output from output_stream
|
||||
print("Starting to stream output...")
|
||||
most_recent = ""
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user