Streaming Update (NEEDS OPTIMIZATIONS AND FAILSAFES!!!!!!!!!!!!!!!!!!!!!!)

This commit is contained in:
ForeverPyrite
2024-10-05 16:57:03 -04:00
parent 5601e8a874
commit 3ea5681f86
5 changed files with 81 additions and 77 deletions

40
app.py
View File

@@ -1,23 +1,12 @@
from flask import Flask, render_template, Response, request
from main import get_auto_transcript, get_video_id, create_and_stream, EventHandler
from main import get_auto_transcript, get_video_id, create_and_stream, output_buffer, output_lock
from datetime import datetime
import sys
import io
import threading
import pytz
import time
app = Flask(__name__, static_folder="website/static", template_folder="website")
class StreamToLogger(io.StringIO):
def __init__(self):
super().__init__()
def write(self, message):
# I could probably log stuff here
print(message, end='') # Print to standard output (console)
# You could also log this message or handle it differently.
@app.route('/')
def home():
return render_template('index.html')
@@ -33,9 +22,6 @@ def streaming():
@app.route('/process_url', methods=['POST'])
def process_url():
old_stdout = sys.stdout
new_stdout = StreamToLogger()
sys.stdout = new_stdout
# 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)
url = request.form['url']
@@ -50,11 +36,31 @@ def process_url():
if (not transcript):
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."
# Start processing in a separate thread
threading.Thread(target=create_and_stream, args=(transcript,)).start()
# Process the transcript and stream the result.
# response = create_and_stream(transcript)
# log.write(f"\n\n\n### New Entry at {datetime.now(pytz.timezone('America/New_York')).strftime('%Y-%m-%d %H:%M:%S')}\n\n URL: {url}\n Video ID: {video_id}\n\nAssistant Response: \n{response}")
# Return a response
return Response(create_and_stream(transcript), content_type="text/plain", status=200, direct_passthrough=True) # Add more detailed output if needed
return Response("Processing started. Check /stream_output for updates.", status=200) # Add more detailed output if needed
@app.route('/stream_output')
def stream_output():
def yoink():
while True:
with output_lock:
if output_buffer:
message = output_buffer.pop(0)
yield f"{message}"
time.sleep(0.005) # Adjust as necessary for your application
return Response(yoink(), content_type='text/plain')
if __name__ == '__main__': # Change this line to properly check for main
app.run(debug=True)