abhishekjoel commited on
Commit
18e5f93
·
verified ·
1 Parent(s): c6f7875

Create utils.py

Browse files
Files changed (1) hide show
  1. utils.py +37 -0
utils.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pydub import AudioSegment
3
+ import openai
4
+
5
+ def split_audio(file_path, chunk_length=10*60*1000):
6
+ audio = AudioSegment.from_file(file_path)
7
+ chunks = []
8
+ for start in range(0, len(audio), chunk_length):
9
+ chunk = audio[start:start + chunk_length]
10
+ chunk_file = f"chunk_{start // chunk_length}.mp3"
11
+ chunk.export(chunk_file, format="mp3")
12
+ chunks.append(chunk_file)
13
+ return chunks
14
+
15
+ def transcribe_audio(chunks):
16
+ transcriptions = []
17
+ timestamps = []
18
+ for idx, chunk in enumerate(chunks):
19
+ with open(chunk, "rb") as audio_file:
20
+ response = openai.Audio.transcribe("whisper-1", audio_file)
21
+ transcriptions.append(response['text'])
22
+ timestamps.append(f"Chunk {idx} - {idx * 10} min to {(idx + 1) * 10} min")
23
+ return transcriptions, timestamps
24
+
25
+ def generate_lesson_plan(transcriptions):
26
+ # Combine transcriptions into a single text
27
+ combined_transcript = " ".join(transcriptions)
28
+
29
+ # Call ChatGPT to generate a lesson plan
30
+ response = openai.ChatCompletion.create(
31
+ model="gpt-4",
32
+ messages=[
33
+ {"role": "user", "content": f"Generate a detailed lesson plan based on the following transcript:\n\n{combined_transcript}"}
34
+ ]
35
+ )
36
+ lesson_plan = response['choices'][0]['message']['content']
37
+ return lesson_plan