hype streaming take two plus better handling of multiple users (it still doesn't work)
This commit is contained in:
74
app/app.py
74
app/app.py
@@ -1,62 +1,40 @@
|
||||
import logging
|
||||
from flask import Flask, render_template, Response, request
|
||||
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, pytz
|
||||
from main import yoink, process
|
||||
|
||||
app = Flask(__name__, static_folder="website/static", template_folder="website")
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(
|
||||
filename='./logs/app.log',
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s %(levelname)s: %(message)s',
|
||||
datefmt='%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
logging.info("Home page accessed.")
|
||||
return render_template('index.html')
|
||||
|
||||
@app.route('/process_url', methods=['POST'])
|
||||
def process_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)
|
||||
if not video_id:
|
||||
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.\n")
|
||||
return "Parsed video ID, but transcript retrieval failed (might be disabled by the video owner).", 400
|
||||
|
||||
# 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)
|
||||
global most_recent_thread
|
||||
url = request.form['url']
|
||||
logging.info(f"Received URL for processing: {url}")
|
||||
success, msg, status_code, most_recent_thread = process(url)
|
||||
if success:
|
||||
logging.info("Processing started successfully.")
|
||||
return Response("Processing started. Check /stream_output for updates.", content_type='text/plain', status=200)
|
||||
else:
|
||||
logging.error(f"Processing failed: {msg}")
|
||||
return Response(msg, content_type='text/plain', status=status_code)
|
||||
|
||||
@app.route('/stream_output')
|
||||
def stream_output_route():
|
||||
def generate():
|
||||
log("<details>\n<summary>Starting stream thread...</summary>\n\n")
|
||||
|
||||
# Start streaming output from output_stream
|
||||
log("Starting to stream output.\n")
|
||||
while not output_stream.done:
|
||||
if output_stream.buffer:
|
||||
delta = output_stream.buffer.pop(0)
|
||||
yield delta.encode("utf-8")
|
||||
else:
|
||||
awaiter(sleep(0.05))
|
||||
log(f"\nStream completed.\n</details>\n\n---\n\n### Completed Assistant Response:\n{output_stream.response}\n\n---\n\n")
|
||||
output_stream.reset()
|
||||
log("\n### Task completed successfully without errors!\n")
|
||||
|
||||
return Response(generate(), content_type='text/plain', status=200)
|
||||
def stream_output():
|
||||
logging.info("Streaming output requested.")
|
||||
return Response(yoink(most_recent_thread), content_type='text/plain', status=200)
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True, host='0.0.0.0', port=1986)
|
||||
logging.info("Starting Flask application.")
|
||||
app.run(debug=True)
|
||||
Reference in New Issue
Block a user