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

This commit is contained in:
ForeverPyrite
2024-10-05 16:55:53 -04:00
parent d7328a5b7e
commit 5601e8a874
4 changed files with 78 additions and 16 deletions

37
app.py
View File

@@ -1,18 +1,43 @@
from flask import Flask, render_template, request
from main import get_auto_transcript, get_video_id, create_and_stream
from flask import Flask, render_template, Response, request
from main import get_auto_transcript, get_video_id, create_and_stream, EventHandler
from datetime import datetime
import sys
import io
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')
@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():
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)
#log = open("log.txt", "at", 1)
url = request.form['url']
# Extract the video ID from the URL
@@ -26,10 +51,10 @@ def process_url():
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."
# 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}")
# 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 # Add more detailed output if needed
return Response(create_and_stream(transcript), content_type="text/plain", status=200, direct_passthrough=True) # Add more detailed output if needed
if __name__ == '__main__': # Change this line to properly check for main
app.run(debug=True)