Skip to content

Instantly share code, notes, and snippets.

View mseeks's full-sized avatar
©️
1992

Matthew Sullivan mseeks

©️
1992
View GitHub Profile
@mseeks
mseeks / flask_serve_dynamic_file.py
Created February 13, 2024 02:12
Python Flask snippet for serving a file at an endpoint with a file name derived from an argument passed in, specifically the ID of the file.
from flask import Flask, send_file, request
app = Flask(__name__)
@app.route('/download-file/<file_id>')
def download_file(file_id):
# Construct file path using file ID
file_path = f'path/to/your/files/{file_id}.extension'
# Serve the file for download
return send_file(file_path, as_attachment=True, download_name=f'{file_id}.extension')
@mseeks
mseeks / flask_serve_file.py
Created February 13, 2024 02:11
Python Flask snippet for serving a file at an endpoint. This snippet demonstrates how to use the `send_file` method to allow users to download a file when they access a specific route.
from flask import Flask, send_file
app = Flask(__name__)
@app.route('/download-file')
def download_file():
# Path to the file you want to serve up
path_to_file = 'path/to/your/file.extension'
# Serve the file for download
return send_file(path_to_file, as_attachment=True)
@mseeks
mseeks / convert_mp3_to_m4a_for_twilio.sh
Created February 13, 2024 02:08
Shell command snippet for converting MP3 files to M4A using ffmpeg, optimizing for compatibility with iOS devices.
# Command to convert MP3 to M4A for broad compatibility, especially with iOS devices
ffmpeg -i input.mp3 -acodec aac output.m4a
# Remember to replace `input.mp3` and `output.m4a` with your actual file names.
@mseeks
mseeks / twilio_send_mms_with_aac.py
Created February 13, 2024 02:06
Python code to send an MMS with Twilio using an AAC audio file for compatibility with iOS devices.
from twilio.rest import Client
# Twilio credentials
account_sid = 'YOUR_ACCOUNT_SID'
auth_token = 'YOUR_AUTH_TOKEN'
# Your Twilio phone number
from_phone_number = 'TWILIO_PHONE_NUMBER'
# Recipient's phone number
@mseeks
mseeks / auto_cot_example.py
Created February 13, 2024 02:03
Auto-CoT Example Pseudo-Code
def auto_cot(prompt, model):
# Initialize an empty list to store reasoning steps
reasoning_steps = []
# Generate initial reasoning step
current_step = model.generate_initial_step(prompt)
while not is_final_step(current_step):
# Append the current reasoning step to the list
reasoning_steps.append(current_step)
# Use the current reasoning step as the next prompt
prompt = form_next_prompt(current_step)
@mseeks
mseeks / flask_async_webhook_thread.py
Created February 10, 2024 01:57
Example Flask app showing how to handle a webhook with an asynchronous task using threading, returning a 200 response immediately.
from flask import Flask, request
from threading import Thread
app = Flask(__name__)
def async_task(data):
# Your async operation here, e.g., process data
pass
@app.route('/webhook', methods=['POST'])
@mseeks
mseeks / flask_twilio_webhook_mms.py
Created February 8, 2024 02:27
Python Flask app code snippet for handling MMS recorded via Twilio webhook, extracting the Media URL.
from flask import Flask, request
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def webhook():
# Assuming the incoming request is a form with 'MediaUrl0' for the first media item
media_url = request.form.get('MediaUrl0')
print(f'Media URL: {media_url}')
@mseeks
mseeks / twilio_fetch_audio_from_mms.py
Created February 8, 2024 02:24
Python code snippet for fetching an audio file URL from an MMS message using Twilio's Python SDK.
from twilio.rest import Client
# Twilio credentials
account_sid = 'your_account_sid'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)
# Fetch a specific message by SID
message_sid = 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
message = client.messages(message_sid).fetch()
@mseeks
mseeks / whisper_transcribe_example.py
Created February 8, 2024 02:20
Python code for transcribing audio using OpenAI's Whisper model.
import whisper
model = whisper.load_model("base")
# Load your audio file
audio_path = "path/to/your/audio.mp3"
# Transcribe the audio file
def transcribe_audio(audio_path):
result = model.transcribe(audio_path)
def flattener(array_to_flatten)
array_to_flatten.each_with_object([]) do |array_element, flat_array|
flat_array.push *(array_element.is_a?(Array) ? flattener(array_element) : array_element)
end
end