Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 35 additions & 37 deletions projects/Alarm Clock/alarm_clock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from threading import *
from pygame import mixer

# create object
# Create object
root = Tk()
root.geometry("500x250")

Expand All @@ -15,27 +15,33 @@ def Threading():


def alarm():
# alarm set to an infinite loop
# Alarm set to an infinite loop
while True:
# alarm set
# Alarm set
set_alarm_time = f"{hour.get()}:{minute.get()}:{second.get()}"
time.sleep(1)

# get current time
# Get current time
current_time = datetime.datetime.now().strftime("%H:%M:%S")
print(current_time, set_alarm_time)

# condition to check if set time is equal to current time
# Condition to check if set time is equal to current time
if current_time == set_alarm_time:
print("Wake Up now!")
# play sound continuously
mixer.init()
mixer.music.load("sound.wav")
mixer.music.play()
# Play sound continuously
try:
mixer.init()
mixer.music.load("sound.wav")
mixer.music.play()
break
except Exception as e:
print(f"Error playing sound: {e}")
break


def stop_alarm():
mixer.music.stop()
if mixer.get_init():
mixer.music.stop()


Label(root, text="Alarm Clock", font=("Helvetica 20 bold"), fg="red").pack(pady=10)
Expand All @@ -45,39 +51,31 @@ def stop_alarm():
frame.pack()

hour = StringVar(root)
hours = (
"00",
"01",
"02",
"03",
"04",
"05",
"06",
"07",
"08",
"09",
"10",
"11",
"12",
"13",
"14",
"15",
"16",
"17",
"18",
"19",
"20",
"21",
"22",
"23",
"24",
)
hours = [f"{i:02}" for i in range(24)]
hour.set(hours[0])

hrs = OptionMenu(frame, hour, *hours)
hrs.pack(side=LEFT)

minute = StringVar(root)
minutes = [f"{i:02}" for i in range(60)]
minute.set(minutes[0])

mins = OptionMenu(frame, minute, *minutes)
mins.pack(side=LEFT)

second = StringVar(root)
seconds = [f"{i:02}" for i in range(60)]
second.set(seconds[0])

secs = OptionMenu(frame, second, *seconds)
secs.pack(side=LEFT)

Button(root, text="Set Alarm", font=("Helvetica 15"), command=Threading).pack(pady=20)

Button(root, text="Stop Alarm", bg="red", fg="white", command=stop_alarm).pack(pady=30)

root.mainloop()
minutes = (
"00",
"01",
Expand Down
63 changes: 29 additions & 34 deletions projects/AudioAPI/transcription.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,61 @@
from werkzeug.utils import secure_filename
from datetime import timedelta
import os
import tempfile
import whisper

app = Flask(__name__)

TEMP_DIR = tempfile.gettempdir()

@app.route("/transcribe", methods=["POST"])
def transcribe():
# check if the post request has the file part
# Check if the post request has the file part
if "audio" not in request.files:
return jsonify({"error": "No audio file found."}), 400

file = request.files["audio"]

if file.filename == "":
return jsonify({"error": "No audio file selected."}), 400

if not allowed_file(file.filename):
return (
jsonify({"error": "Only WAV, MP3, and OGG audio files are allowed."}),
400,
)
return jsonify({"error": "Only WAV, MP3, and OGG audio files are allowed."}), 400

filename = secure_filename(file.filename)
file.save(filename)
file_path = os.path.join(TEMP_DIR, filename)

model = whisper.load_model("small") # Change this to your desired model
print("Whisper model loaded.")
transcribe = model.transcribe(audio=filename)
segments = transcribe["segments"]
try:
file.save(file_path)

srt_file = open("subtitles.vtt", "w", encoding="utf-8")
srt_file.write("WEBVTT\n\n")
model = whisper.load_model("small") # Specify your desired model here
print("Whisper model loaded.")

for segment in segments:
startTime = str(0) + str(timedelta(seconds=int(segment["start"]))) + ".000"
endTime = str(0) + str(timedelta(seconds=int(segment["end"]))) + ".000"
text = segment["text"]
segmentId = segment["id"] + 1
segment = f"{segmentId}\n{startTime} --> {endTime}\n{text[1:] if text[0] == ' ' else text}\n\n"
transcription = model.transcribe(audio=file_path)
segments = transcription.get("segments", [])

srt_file.write(segment)
srt_file_path = os.path.join(TEMP_DIR, "subtitles.vtt")
with open(srt_file_path, "w", encoding="utf-8") as srt_file:
srt_file.write("WEBVTT\n\n")
for segment in segments:
start_time = str(timedelta(seconds=segment["start"])).split(".")[0]
end_time = str(timedelta(seconds=segment["end"])).split(".")[0]
text = segment["text"].strip()
segment_id = segment["id"] + 1
srt_segment = f"{segment_id}\n{start_time}.000 --> {end_time}.000\n{text}\n\n"
srt_file.write(srt_segment)

srt_file.close()
os.remove(filename)
return send_file(srt_file_path, as_attachment=True, download_name="subtitles.vtt", mimetype="text/vtt")

return send_file(
"subtitles.vtt",
as_attachment=True,
download_name="subtitles.vtt",
mimetype="text/vtt",
)
except Exception as e:
return jsonify({"error": str(e)}), 500

finally:
if os.path.exists(file_path):
os.remove(file_path)

def allowed_file(filename):
return "." in filename and filename.rsplit(".", 1)[1].lower() in [
"wav",
"mp3",
"ogg",
]

return '.' in filename and filename.rsplit('.', 1)[1].lower() in ["wav", "mp3", "ogg"]

if __name__ == "__main__":
app.run()