i read PEP conventions

This commit is contained in:
ForeverPyrite
2024-11-20 15:50:41 -05:00
parent 0696ed21e9
commit c363f4c246

View File

@@ -4,8 +4,6 @@ from asyncio import sleep
from datetime import datetime
import threading, pytz
app = Flask(__name__, static_folder="website/static", template_folder="website")
@app.route('/')
@@ -14,49 +12,51 @@ def home():
@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']
url = request.form.get('url', '').strip()
if not url:
log("No URL provided.\n")
return "No URL provided.", 400
log(f"\n\n\n## New Entry at {datetime.now(pytz.timezone('America/New_York')).strftime('%Y-%m-%d %H:%M:%S')}\n")
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
video_id = get_video_id(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")
log(f"Could not parse video id from URL: {url}\n")
return "Couldn't parse video ID from URL. (Ensure it's a valid YouTube.com or YouTu.be URL.)", 400
log(f"Video ID: {video_id}\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."
if not transcript:
log("## Error: Could not retrieve transcript. Assistant won't be called.\n")
return "Parsed video ID, but transcript retrieval failed (might be disabled by the video owner).", 400
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
# Start the stream thread
thread = threading.Thread(target=create_and_stream, args=(transcript,))
thread.start()
log("Stream preparation complete, sending reply...\n")
return Response("Processing started. Check /stream_output for updates.", content_type='text/plain', status=200)
@app.route('/stream_output')
def stream_output():
def yoink():
def stream_output_route():
def generate():
log("<details>\n<summary>Starting stream thread...</summary>\n\n")
thread.start()
# Start streaming output from output_stream
log("Starting to stream output.")
log("Starting to stream output.\n")
while not output_stream.done:
if output_stream.buffer != []:
if output_stream.buffer:
delta = output_stream.buffer.pop(0)
yield bytes(delta, encoding="utf-8")
yield delta.encode("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")
log(f"\nStream completed.\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)
log("\n### Task completed successfully without errors!\n")
return Response(generate(), content_type='text/plain', status=200)
if __name__ == '__main__':
app.run(debug=True)
app.run(debug=True, host='0.0.0.0', port=1986)