diff --git a/bot.py b/bot.py index 9dadfad..1789781 100644 --- a/bot.py +++ b/bot.py @@ -929,17 +929,17 @@ async def handle_response( "Rozmiar zapytania przed dodaniem historii %s", chat_gpt_config_request_size ) if request_type == "MUSIC": - algorithm = "gpt-3.5-turbo-0125" + algorithm = "gpt-4o" table = message_table_muzyka token_amount = 10700 elif request_type == "RANDOM": - algorithm = "gpt-3.5-turbo-0125" + algorithm = "gpt-4o" table = MESSAGE_TABLE token_amount = 10700 else: table = MESSAGE_TABLE - algorithm = "gpt-4" - token_amount = 7000 + algorithm = "gpt-4o" + token_amount = 10700 prompt_gpt_request_size = num_tokens_from_string( {"role": "user", "content": final_prompt}, "gpt-4" @@ -1021,26 +1021,33 @@ async def handle_response( logger.info(result) temp = {"role": "assistant", "content": result} history.append(temp) - if request_type: - with open(MEMORY_FIVE_MUZYKA, "r+", encoding=ENCODING) as file: + if request_type == "MUSIC": + with open(MEMORY_FIVE_MUZYKA, "r+", encoding=ENCODING) as file_music_memory: # First we load existing data into a dict. - file_data = json.load(file) + file_data = json.load(file_music_memory) # Join new_data with file_data inside emp_details file_data.append(temp) - # Sets file's current position at offset. - file.seek(0) + file_music_memory.seek(0) # convert back to json. - json.dump(file_data, file, indent=4) + json.dump(file_data, file_music_memory, indent=4) + elif request_type == "RANDOM": + with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as file_memory: + # First we load existing data into a dict. + file_data = json.load(file_memory) + # Join new_data with file_data inside emp_details + file_data.append(temp) + file_memory.seek(0) + # convert back to json. + json.dump(file_data, file_memory, indent=4) else: - with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as file: + with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as file_memory: # First we load existing data into a dict. - file_data = json.load(file) + file_data = json.load(file_memory) # Join new_data with file_data inside emp_details file_data.append(temp) - # Sets file's current position at offset. - file.seek(0) + file_memory.seek(0) # convert back to json. - json.dump(file_data, file, indent=4) + json.dump(file_data, file_memory, indent=4) return result, MESSAGE_TABLE diff --git a/conjurer_musician/conjurer_musician.py b/conjurer_musician/conjurer_musician.py index 5244f5e..525f248 100644 --- a/conjurer_musician/conjurer_musician.py +++ b/conjurer_musician/conjurer_musician.py @@ -14,8 +14,7 @@ from logging import handlers from pathlib import Path from platform import uname from sys import platform - -from flask import Flask, jsonify, redirect, request, send_from_directory +from flask import Flask, jsonify, redirect, request, send_from_directory, render_template from waitress import serve HOST_ADDRESS = "192.168.1.15" @@ -246,8 +245,9 @@ def stream_music(): :return: A JSON response containing a key "music_file_list" with the value of the variable `music_file_list`. """ - return send_from_directory("/tmp/hls", "stream.m3u8") + #return send_from_directory("/tmp/hls", "stream.m3u8") + return render_template("/home/pi/Conjurer/stream.html") @app.route("/") def stream(file_name): @@ -260,10 +260,10 @@ def stream(file_name): Returns: Response: The response containing the streamed file. """ + # trunk-ignore(bandit/B108) video_dir = "/tmp/hls" return send_from_directory(video_dir, file_name) - @app.route("/stream_mp3", methods=["GET"]) def stream_music_mp3(): """ diff --git a/conjurer_musician/music_utils.py b/conjurer_musician/music_utils.py new file mode 100644 index 0000000..817b880 --- /dev/null +++ b/conjurer_musician/music_utils.py @@ -0,0 +1,26 @@ +# This Python file uses the following encoding: utf-8 +"""This module contains utility scripts used by musi radiostation Conjurer""" +import time +import re + +NAME = "/home/pi/Conjurer/persistence.log" + +def print_top(): + """ + Prints the top 65 lines from a file specified by the NAME variable. + """ + while True: + line = "" + buffer = "" + with open(NAME, "r", encoding="utf-8") as f: + for _ in range (45): + line = f.readline() + if re.match(".*mp3", line): + buffer += line + print("=====================================") + print(buffer) + print("=====================================") + time.sleep(180) + +if __name__ == "__main__": + print_top() diff --git a/conjurer_musician/radio_conjurer.liq b/conjurer_musician/radio_conjurer.liq index 71bcced..dbd8695 100644 --- a/conjurer_musician/radio_conjurer.liq +++ b/conjurer_musician/radio_conjurer.liq @@ -5,136 +5,157 @@ # Load icecast credentials from a JSON file let json.parse credentials = file.contents("/home/pi/Conjurer/icecast_credentials.json") - # Enable replaygain metadata processing - enable_replaygain_metadata() - l = playlog(duration = 14400.0, persistency="/home/pi/Conjurer/persistence.log") - def check(r) - m = request.metadata(r) - if l.last(m) < 3600. then - log.info("Rejecting #{m['filename']} (played #{l.last(m)}s ago).") - false - else - l.add(m) - true - end +# Enable replaygain metadata processing +enable_replaygain_metadata() + +# Set up a playlog for tracking played tracks + +l = playlog(duration = 72000.0, persistency="/home/pi/Conjurer/persistence.log") + +# Function to check if a track can be played based on its metadata +def check(r) + m = request.metadata(r) + if l.last(m) < 36000. then + log.info("Rejecting #{m['filename']} (played #{l.last(m)}s ago).") + false + else + l.add(m) + true end +end +# Define playlists to be used in the stream +s1 = replaygain(playlist(reload_mode="watch", check_next=check, "/home/pi/Conjurer/all_playlist.playlist")) +s2 = replaygain(playlist(reload_mode="watch", check_next=check, "/home/pi/Conjurer/priority_queue.playlist")) +s3 = replaygain(playlist(reload_mode="watch", check_next=check, "/home/pi/Conjurer/hit.playlist")) +# Create a request queue for user-generated requests +requests_queue = request.queue() - # Define playlists to be used in the stream - s1 = replaygain(playlist(reload_mode="watch", check_next=check, "/home/pi/Conjurer/all_playlist.playlist")) - s2 = replaygain(playlist(reload_mode="watch", check_next=check, "/home/pi/Conjurer/priority_queue.playlist")) - s3 = replaygain(playlist(reload_mode="watch", check_next=check, "/home/pi/Conjurer/hit.playlist")) - requests_queue = request.queue() - def queue_processing() - f1=file.read("/home/pi/Conjurer/request.playlist") - text = f1() - if text != "" then - requests_queue.push.uri(text) - file.remove("/home/pi/Conjurer/request.playlist") - f = file.open("/home/pi/Conjurer/request.playlist", create=true) - f.close() - log.info("done") - else - log.info("no requests") - end +# Function to process the request queue and add new requests +def queue_processing() + f1=file.read("/home/pi/Conjurer/request.playlist") + text = f1() + if text != "" then + requests_queue.push.uri(text) + file.remove("/home/pi/Conjurer/request.playlist") + f = file.open("/home/pi/Conjurer/request.playlist", create=true) + f.close() end +end - # Randomly select a playlist with weights - s = random(id="randomizer", weights=[1, 3, 2], [s1, s2, s3]) - #s = delay(duration=5., s) - #radio = add([delay(1800., jingles), music]) - #radio = fallback([delay(1800., jingles), music]) - #p = predicate.signal() - #radio = switch([(p, jingles), ({true}, music)]) - #thread.run(every=1200., {p.signal()}) -# def cmd(_) -# p.signal() -# "Jingle inserted!" -# end -# server.register("insert_jingle", cmd) +# Randomly select a playlist with weights +s4 = random(id="randomizer", weights=[1, 3, 5], [s1, s2, s3]) - # Set up an interactive harbor for controlling the stream - interactive.harbor(port = 9999) - # Set up interactive controls for bass boost - f = interactive.float("f", description="Frequency", min=0., max=1000., unit="Hz", 200.) - g = interactive.float("g", description="Gain", min=0., max=20., unit="dB", 8.) - b = bass_boost(frequency=f, gain=g, s) - s = add([s, b]) - # Set up interactive control for main volume - a = interactive.float("main_volume", min=0., max=20., 1.) - s = compress.multiband.interactive(bands=3, s) +# Load jingles playlist +jingles = (playlist(reload_mode="watch", "/home/pi/Conjurer/jingles.playlist")) - # Apply audio processing effects - s = nrj(normalize(s)) - s = amplify(a, s) +# Create the main stream with random playlist and jingles +s = rotate(id="randomizer", weights=[10, 1], [s4, jingles]) - # Skip blank sections in the stream - s = blank.skip(max_blank=2., s) +s = crossfade(fade_out=2., fade_in=2., duration=4., fallback(id="switcher", track_sensitive=false, [requests_queue, s])) - # Configure logging settings - log_to_stdout = true - log_to_file = true - logpath = "/home/pi/Conjurer/radio_log.log" - loglevel = 4 - set("log.stdout", log_to_stdout) - set("log.level", loglevel) +# Set up an interactive harbor for controlling the stream +interactive.harbor(port = 9999) - # Enable logging to file - set("log.file", log_to_file) - set("log.file.path", logpath) +# Set up interactive controls for bass boost +f = interactive.float("f", description="Frequency", min=0., max=1000., unit="Hz", 200.) +g = interactive.float("g", description="Gain", min=0., max=20., unit="dB", 8.) +b = bass_boost(frequency=f, gain=g, s) +s = add([s, b]) - # Set up emergency fallback track - emergency = single("/home/pi/RetroPie/mp3/Youtube/Dr. Peacock - Trip to Ireland [GvrvQTUbUcA].mp3") +# Set up interactive control for main volume +a = interactive.float("main_volume", min=0., max=20., 1.) +s = compress.multiband.interactive(bands=7, s) - # Create a fallback stream with the main stream and emergency track - cross_s = crossfade(fade_out=3., fade_in=3., duration=5., fallback(id="switcher", track_sensitive=false, [requests_queue, s])) - radio = fallback(id="switcher2", track_sensitive=false, [cross_s, emergency]) +# Apply audio processing effects +s = nrj(normalize(s)) +s = amplify(a, s) - thread.run(every=60., queue_processing) +mic = buffer(input.pulseaudio()) # Microphone +mic = gate(threshold=-30., range=-80., mic) +mic = blank.strip(max_blank=2., min_noise=.1, threshold=-20., mic) - # Handle metadata events - handle_metadata = fun (m) -> begin - log.info(m["title"]) - log.info(m["artist"]) - log.info(m["filename"]) +s = add([s, mic]) +#w = interactive.float("wetness", min=0., max=1., 1.) +#s = dry_wet(w,s,s2) + + +# Skip blank sections in the stream +s = blank.skip(max_blank=2., s) + +# Configure logging settings +log_to_stdout = true +log_to_file = true +logpath = "/home/pi/Conjurer/radio_log.log" +loglevel = 3 +set("log.stdout", log_to_stdout) +set("log.level", loglevel) + +# Enable logging to file +set("log.file", log_to_file) +set("log.file.path", logpath) + +# Set up emergency fallback track +emergency = single("/home/pi/RetroPie/mp3/Youtube/Dr. Peacock - Trip to Ireland [GvrvQTUbUcA].mp3") +radio = fallback(id="switcher2", track_sensitive=false, [s, emergency]) +# Set up an interactive control for skipping tracks +p = interactive.bool("Skip track", false) + +# Function to process the request queue and skip the current track if requested +def check_skip() + if p() then + log.info("Skipping current track.") + radio.skip() + p.set(false) end - radio.on_metadata(handle_metadata) +end - # Enable persistent script parameters - interactive.persistent("script.params") +# Run the queue processing function every 60 seconds +thread.run(every=60., queue_processing) - # Configure output formats and destinations - output.file.hls("/tmp/hls", [("mp3-low", %mp3(bitrate=96)), ("mp3-hi", %mp3(bitrate=160))], radio) - output.icecast(%mp3, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="mp3-stream", radio) -# output.icecast(%opus, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="opus-stream", radio) -# output.icecast(%ogg, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="ogg-stream", radio) -# output.icecast(%aac, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="aac-stream", radio) -# output.icecast(%flac, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="flac-stream", radio) -# output.icecast(%vorbis, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="vorbis-stream", radio) -# output.icecast(%speex, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="speex-stream", radio) -# output.icecast(%wav, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="wav-stream", radio) -# output.icecast(%pcm, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="pcm-stream", radio) -# output.icecast(%raw, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="raw-stream", radio) -# output.icecast(%s16l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s16l-stream", radio) -# output.icecast(%s16b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s16b-stream", radio) -# output.icecast(%s24l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s24l-stream", radio) -# output.icecast(%s24b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s24b-stream", radio) -# output.icecast(%s32l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s32l-stream", radio) -# output.icecast(%s32b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s32b-stream", radio) -# output.icecast(%s64l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s64l-stream", radio) -# output.icecast(%s64b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s64b-stream", radio) -# output.icecast(%s128l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s128l-stream", radio) -# output.icecast(%s128b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s128b-stream", radio) -# output.icecast(%s256l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s256l-stream", radio) -# output.icecast(%s256b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s256b-stream", radio) -# output.icecast(%s512l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s512l-stream", radio) -# output.icecast(%s512b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s512b-stream", radio) -# output.icecast(%s1024l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s1024l-stream", radio) -# output.icecast(%s1024b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s1024b-stream", radio) -# output.icecast(%s2048l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s2048l-stream", radio) -# output.icecast(%s2048b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s2048b-stream", radio) -# output.icecast(%s4096l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s4096l-stream", radio) -# output.icecast(%s4096b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s4096b-stream", radio) -# output.icecast(%s8192l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s8192l-stream", radio) -# output.icecast(%s8192b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s8192b-stream", radio) + +# Run the check_skip function every 5 seconds +thread.run(every=15., check_skip) + + +# Enable persistent script parameters +interactive.persistent("script.params") + +# Configure output formats and destinations + +output.icecast(%mp3, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="mp3-stream", radio) +output.pulseaudio(radio) +#output.file.hls("/tmp/hls", [("mp3-low", %mp3(bitrate=96)), ("mp3-hi", %mp3(bitrate=160))], radio) +# Uncomment the following lines to enable additional output formats +# output.icecast(%opus, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="opus-stream", radio) +# output.icecast(%ogg, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="ogg-stream", radio) +# output.icecast(%aac, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="aac-stream", radio) +# output.icecast(%flac, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="flac-stream", radio) +# output.icecast(%vorbis, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="vorbis-stream", radio) +# output.icecast(%speex, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="speex-stream", radio) +# output.icecast(%wav, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="wav-stream", radio) +# output.icecast(%pcm, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="pcm-stream", radio) +# output.icecast(%raw, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="raw-stream", radio) +# output.icecast(%s16l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s16l-stream", radio) +# output.icecast(%s16b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s16b-stream", radio) +# output.icecast(%s24l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s24l-stream", radio) +# output.icecast(%s24b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s24b-stream", radio) +# output.icecast(%s32l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s32l-stream", radio) +# output.icecast(%s32b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s32b-stream", radio) +# output.icecast(%s64l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s64l-stream", radio) +# output.icecast(%s64b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s64b-stream", radio) +# output.icecast(%s128l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s128l-stream", radio) +# output.icecast(%s128b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s128b-stream", radio) +# output.icecast(%s256l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s256l-stream", radio) +# output.icecast(%s256b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s256b-stream", radio) +# output.icecast(%s512l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s512l-stream", radio) +# output.icecast(%s512b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s512b-stream", radio) +# output.icecast(%s1024l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s1024l-stream", radio) +# output.icecast(%s1024b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s1024b-stream", radio) +# output.icecast(%s2048l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s2048l-stream", radio) +# output.icecast(%s2048b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s2048b-stream", radio) +# output.icecast(%s4096l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s4096l-stream", radio) +# output.icecast(%s4096b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s4096b-stream", radio) +# output.icecast(%s8192l, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s8192l-stream", radio) +# output.icecast(%s8192b, host="retropie", port=8000, password=credentials.password, icy_metadata="true", mount="s8192b-stream", radio) diff --git a/conjurer_musician/stream.html b/conjurer_musician/stream.html new file mode 100644 index 0000000..3a7811f --- /dev/null +++ b/conjurer_musician/stream.html @@ -0,0 +1,9 @@ + + + HTTP Live Streaming Example + + + + + diff --git a/requirements_bot.txt b/requirements_bot.txt index e4d9ba0..7c8fed6 100644 --- a/requirements_bot.txt +++ b/requirements_bot.txt @@ -12,4 +12,5 @@ spotipy tiktoken PyNaCl flask -waitress \ No newline at end of file +waitress +clickupython \ No newline at end of file diff --git a/utils/update_memory.py b/utils/update_memory.py new file mode 100644 index 0000000..c310d41 --- /dev/null +++ b/utils/update_memory.py @@ -0,0 +1,33 @@ +import json +MEMORY_FIVE_SIARA = "/home/pi/Conjurer/pamiec.json" +ENCODING = "utf-8" + +messages = [ + {"role": "user", "content" : "Hammer: All systems operational"}, + {"role": "assistant", "content": "Aye, Szefie! Wszystkie systemy działają sprawnie. Gotów do dalszej służby! Co podać?"}, + {"role": "user", "content" : "Hammer: Podaj coś na orzeźwienie i przebudzenie. COś co rozgrzeje moje stare kości po ciężkiej pracy!"}, + {"role": "assistant", "content": "Z przyjemnością Szefie! Dla Ciebie przygotuję Midzynarodówkę Interstelarną Wolf's Dragoon - wysoce energetyczny koktajl rozgrzewający i pobudzający jednocześnie."+ + "Do przygotowania potrzebuję:"+ + "\n"+ + "60 ml mocnego rumu z Wolfa (lub innego, jeśli wolisz)"+ + "30 ml soku z cytryny"+ + "35 ml domowego syropu z mięty"+ + "15 ml świeżo zmielonej kawy"+ + "\n"+ + "Mieszam wszystko w shakerze z lodem, a potem przelewam przez sitko do kieliszka koktajlowego. W górę serwuję z bigoskiem z miętą i zestem z cytryny. Rozgrzewa, orzeźwia i daje kopa na resztę nocy."+ + "\n"+ + "Co powiesz Szefie, podać Ci taki?"}, + {"role": "user", "content" : "Hammer: Brzmi jak coś co zrobi robotę! Skal!"}, +] + + +for temp in messages: + + with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as file_memory: + # First we load existing data into a dict. + file_data = json.load(file_memory) + # Join new_data with file_data inside emp_details + file_data.append(temp) + file_memory.seek(0) + # convert back to json. + json.dump(file_data, file_memory, indent=4)