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 flask import Flask, render_template, Response, request
from main import get_auto_transcript, get_video_id, create_and_stream from main import get_auto_transcript, get_video_id, create_and_stream, EventHandler
from datetime import datetime from datetime import datetime
import sys
import io
import pytz import pytz
import time
app = Flask(__name__, static_folder="website/static", template_folder="website") 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('/') @app.route('/')
def home(): def home():
return render_template('index.html') 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']) @app.route('/process_url', methods=['POST'])
def process_url(): 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: # 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'] url = request.form['url']
# Extract the video ID from the 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." 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. # Process the transcript and stream the result.
response = create_and_stream(transcript) # 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}") # 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 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 if __name__ == '__main__': # Change this line to properly check for main
app.run(debug=True) app.run(debug=True)

View File

@@ -27,24 +27,30 @@ class EventHandler(AssistantEventHandler):
def on_text_created(self, text) -> None: def on_text_created(self, text) -> None:
print(f"\nassistant > ", end="", flush=True) print(f"\nassistant > ", end="", flush=True)
@override @override
def on_text_delta(self, delta, snapshot): def on_text_delta(self, delta, snapshot):
print(delta.value, end="", flush=True) print(delta.value, end="", flush=True)
def on_tool_call_created(self, tool_call): def on_tool_call_created(self, tool_call):
print(f"\nassistant > {tool_call.type}\n", flush=True) print(f"\nassistant > {tool_call.type}\n", flush=True)
def on_tool_call_delta(self, delta, snapshot): def on_tool_call_delta(self, delta, snapshot):
if delta.type == 'code_interpreter': if delta.type == 'code_interpreter':
if delta.code_interpreter.input: if delta.code_interpreter.input:
print(delta.code_interpreter.input, end="", flush=True) print(delta.code_interpreter.input, end="", flush=True)
if delta.code_interpreter.outputs: if delta.code_interpreter.outputs:
print(f"\n\noutput >", flush=True) print(f"\n\noutput >", flush=True)
for output in delta.code_interpreter.outputs: for output in delta.code_interpreter.outputs:
if output.type == "logs": if output.type == "logs":
print(f"\n{output.logs}", flush=True) print(f"\n{output.logs}", flush=True)
# Setting up OpenAI Client with API Key # Setting up OpenAI Client with API Key
api_key = os.getenv("OPENAI_API_KEY") api_key = os.getenv("OPENAI_API_KEY")
client = OpenAI( client = OpenAI(
@@ -69,7 +75,7 @@ def create_and_stream(transcript):
}, },
event_handler=EventHandler() event_handler=EventHandler()
) as stream: ) as stream:
stream.until_done() stream.until_done()
messages = stream.get_final_messages() messages = stream.get_final_messages()
return messages[0].content[0].text.value return messages[0].content[0].text.value

View File

@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "."
}
],
"settings": {
"html.format.enable": true
}
}

View File

@@ -8,10 +8,6 @@ document.addEventListener("DOMContentLoaded", (event) => {
response_area.innerText = 'Please enter a URL.'; response_area.innerText = 'Please enter a URL.';
return; return;
} }
else{
response_area.innerText = "Sending URL and retriving transcript."
}
fetch('/process_url', { fetch('/process_url', {
method: 'POST', method: 'POST',
headers: { headers: {
@@ -19,13 +15,38 @@ document.addEventListener("DOMContentLoaded", (event) => {
}, },
body: new URLSearchParams({ url: url }) body: new URLSearchParams({ url: url })
}) })
.then(response => response.text()) .then(response => {
.then(data => { const reader = response.body.getReader();
response_area.innerText = data; const decoder = new TextDecoder("utf-8");
function readStream() {
reader.read().then(({ done, value }) => {
if (done) {
console.log("Stream finished.");
return;
}
// Decode and process the chunk
const chunk = decoder.decode(value, { stream: true });
// Split the received chunk by new line to handle multiple lines (if any)
chunk.split('\n').forEach(data => {
if (data.trim()) { // Avoid empty strings
// Update the inner HTML of the output div
response_area.innerHTML += `<p>${data}</p>`;
}
});
// Continue reading
readStream();
});
}
// Start reading the stream
readStream();
}) })
.catch(error => { .catch(error => {
console.error('Error:', error); console.error('Error fetching stream:', error);
response_area.innerText = 'An error occurred. Please try again.';
}); });
}); });
}); });