streaming actually works 🤨
This commit is contained in:
52
app.py
52
app.py
@@ -1,30 +1,28 @@
|
||||
from flask import Flask, render_template, Response, request
|
||||
from main import get_auto_transcript, get_video_id, create_and_stream, output_buffer, output_lock
|
||||
from main import get_auto_transcript, get_video_id, create_and_stream, output_stream, fake_stream, awaiter
|
||||
from asyncio import sleep
|
||||
from datetime import datetime
|
||||
import threading
|
||||
import pytz
|
||||
import time
|
||||
|
||||
|
||||
|
||||
app = Flask(__name__, static_folder="website/static", template_folder="website")
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/streamtest', methods=['POST'])
|
||||
def streaming():
|
||||
def generate():
|
||||
for i in range(10):
|
||||
yield f"Data chunk {i}\n"
|
||||
time.sleep(1) # Simulating a delay in data generation
|
||||
|
||||
return Response(generate(), content_type='text/plain')
|
||||
|
||||
@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)
|
||||
url = request.form['url']
|
||||
if url == "test":
|
||||
global thread
|
||||
thread = threading.Thread(name="test_thread", target=fake_stream)
|
||||
return Response("teehee", status=200)
|
||||
|
||||
# Extract the video ID from the URL
|
||||
video_id = get_video_id(url) # Modify this function to accept the URL
|
||||
@@ -36,31 +34,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."
|
||||
|
||||
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.
|
||||
|
||||
# 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("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')
|
||||
print("Starting stream thread.")
|
||||
thread.start()
|
||||
# Start streaming output from output_stream
|
||||
print("Starting to stream output...")
|
||||
most_recent = ""
|
||||
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))
|
||||
output_stream.reset()
|
||||
thread.join()
|
||||
return
|
||||
return Response(yoink(), content_type='text/plain', status=200)
|
||||
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__': # Change this line to properly check for main
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user