abhishekjoel commited on
Commit
e9b2818
Β·
verified Β·
1 Parent(s): b2ed869

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -71
app.py CHANGED
@@ -12,51 +12,60 @@ load_dotenv()
12
  client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
13
 
14
  def transcribe_audio(audio_file):
15
- """Transcribe audio using Whisper API"""
16
  try:
17
  with open(audio_file, "rb") as audio:
18
  transcript = client.audio.transcriptions.create(
19
  model="whisper-1",
20
  file=audio,
21
- response_format="verbose_json"
 
22
  )
23
  return transcript
24
  except Exception as e:
25
  st.error(f"Error in transcription: {str(e)}")
26
  return None
27
 
28
- def analyze_content(transcript, lesson_plan):
29
- """Analyze transcript using GPT-4 to generate structured notes"""
 
 
 
 
 
 
 
30
  try:
31
- system_prompt = """You are an expert educational content analyzer.
32
- Your task is to:
33
- 1. Analyze the lecture transcript
34
- 2. Structure the content according to the lesson plan
35
- 3. Identify key topics and subtopics
36
- 4. Create timestamps for important points
37
- 5. Generate a comprehensive summary
38
- Format the output in markdown with clear sections."""
39
 
40
  response = client.chat.completions.create(
41
  model="gpt-4-turbo-preview",
42
  messages=[
43
  {"role": "system", "content": system_prompt},
44
- {"role": "user", "content": f"Lesson Plan:\n{lesson_plan}\n\nTranscript:\n{transcript}"}
45
  ],
46
  temperature=0.3,
47
- max_tokens=4000
48
  )
49
 
50
  return response.choices[0].message.content
51
  except Exception as e:
52
- st.error(f"Error in analysis: {str(e)}")
53
  return None
54
 
55
- def save_notes(notes, filename):
56
- """Save generated notes to a file"""
57
- with open(filename, 'w', encoding='utf-8') as f:
58
- f.write(notes)
59
- return filename
 
 
60
 
61
  # Streamlit UI
62
  def main():
@@ -64,65 +73,76 @@ def main():
64
 
65
  st.title("πŸŽ“ Lecture Notes Generator")
66
 
67
- # Sidebar for lesson plan
68
- st.sidebar.header("Lesson Plan")
69
- lesson_plan = st.sidebar.text_area(
70
- "Enter the lesson plan or topics to be covered:",
71
- height=300
72
- )
73
-
74
- # Main content area
75
- col1, col2 = st.columns([1, 1])
76
 
 
77
  with col1:
78
- st.header("Upload Lecture Recording")
79
  uploaded_file = st.file_uploader("Choose an audio file", type=['mp3', 'wav', 'm4a'])
80
 
81
  if uploaded_file:
82
  st.audio(uploaded_file)
83
 
84
- if st.button("Generate Notes"):
85
- if not lesson_plan:
86
- st.warning("Please enter a lesson plan first.")
87
- return
88
-
89
- with st.spinner("Processing audio..."):
90
- # Save uploaded file temporarily
91
- temp_path = f"temp_audio_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
92
- with open(temp_path, "wb") as f:
93
- f.write(uploaded_file.getvalue())
94
 
95
- # Transcribe audio
96
- transcript = transcribe_audio(temp_path)
97
- if transcript:
98
- st.success("Transcription completed!")
 
99
 
100
- # Analyze content
101
- with st.spinner("Generating structured notes..."):
102
- notes = analyze_content(transcript.text, lesson_plan)
103
- if notes:
104
- # Save notes
105
- filename = f"lecture_notes_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
106
- save_notes(notes, filename)
107
-
108
- # Display notes
109
- with col2:
110
- st.header("Generated Notes")
111
- st.markdown(notes)
112
-
113
- # Download button
114
- with open(filename, 'r', encoding='utf-8') as f:
115
- st.download_button(
116
- label="Download Notes",
117
- data=f.read(),
118
- file_name=filename,
119
- mime="text/markdown"
120
- )
121
-
122
- # Cleanup
123
- os.remove(temp_path)
124
- if os.path.exists(filename):
125
- os.remove(filename)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  if __name__ == "__main__":
128
- main()
 
12
  client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
13
 
14
  def transcribe_audio(audio_file):
15
+ """Transcribe audio using Whisper API with timestamps"""
16
  try:
17
  with open(audio_file, "rb") as audio:
18
  transcript = client.audio.transcriptions.create(
19
  model="whisper-1",
20
  file=audio,
21
+ response_format="verbose_json",
22
+ timestamp_granularities=["segment"]
23
  )
24
  return transcript
25
  except Exception as e:
26
  st.error(f"Error in transcription: {str(e)}")
27
  return None
28
 
29
+ def format_timestamp(seconds):
30
+ """Convert seconds to HH:MM:SS format"""
31
+ hours = int(seconds // 3600)
32
+ minutes = int((seconds % 3600) // 60)
33
+ seconds = int(seconds % 60)
34
+ return f"{hours:02d}:{minutes:02d}:{seconds:02d}"
35
+
36
+ def generate_lesson_plan(transcript):
37
+ """Generate a structured lesson plan from the transcript"""
38
  try:
39
+ system_prompt = """You are an educational content expert. Generate a detailed lesson plan from the lecture transcript.
40
+ The lesson plan should include:
41
+ 1. Main Topics
42
+ 2. Subtopics
43
+ 3. Key Learning Objectives
44
+ 4. Important Concepts
45
+ Format the output in markdown with clear hierarchical structure."""
 
46
 
47
  response = client.chat.completions.create(
48
  model="gpt-4-turbo-preview",
49
  messages=[
50
  {"role": "system", "content": system_prompt},
51
+ {"role": "user", "content": f"Generate a lesson plan from this transcript:\n{transcript}"}
52
  ],
53
  temperature=0.3,
54
+ max_tokens=2000
55
  )
56
 
57
  return response.choices[0].message.content
58
  except Exception as e:
59
+ st.error(f"Error generating lesson plan: {str(e)}")
60
  return None
61
 
62
+ def format_transcript_with_timestamps(transcript_data):
63
+ """Format transcript with timestamps in a readable format"""
64
+ formatted_text = "# Lecture Transcript with Timestamps\n\n"
65
+ for segment in transcript_data.segments:
66
+ start_time = format_timestamp(segment.start)
67
+ formatted_text += f"**[{start_time}]** {segment.text}\n\n"
68
+ return formatted_text
69
 
70
  # Streamlit UI
71
  def main():
 
73
 
74
  st.title("πŸŽ“ Lecture Notes Generator")
75
 
76
+ # Create two columns with custom widths
77
+ col1, col2 = st.columns([1, 3])
 
 
 
 
 
 
 
78
 
79
+ # Left column for upload (smaller)
80
  with col1:
81
+ st.header("Upload Recording")
82
  uploaded_file = st.file_uploader("Choose an audio file", type=['mp3', 'wav', 'm4a'])
83
 
84
  if uploaded_file:
85
  st.audio(uploaded_file)
86
 
87
+ if st.button("Generate Notes", type="primary", use_container_width=True):
88
+ # Create tabs in the right column for different outputs
89
+ with col2:
90
+ tab1, tab2 = st.tabs(["πŸ“ Transcript", "πŸ“‹ Lesson Plan"])
 
 
 
 
 
 
91
 
92
+ with st.spinner("Processing audio..."):
93
+ # Save uploaded file temporarily
94
+ temp_path = f"temp_audio_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
95
+ with open(temp_path, "wb") as f:
96
+ f.write(uploaded_file.getvalue())
97
 
98
+ # Transcribe audio
99
+ transcript_data = transcribe_audio(temp_path)
100
+ if transcript_data:
101
+ # Format transcript with timestamps
102
+ formatted_transcript = format_transcript_with_timestamps(transcript_data)
103
+
104
+ # Generate lesson plan
105
+ lesson_plan = generate_lesson_plan(transcript_data.text)
106
+
107
+ # Display transcript in first tab
108
+ with tab1:
109
+ st.markdown(formatted_transcript)
110
+ # Download button for transcript
111
+ st.download_button(
112
+ label="Download Transcript",
113
+ data=formatted_transcript,
114
+ file_name=f"transcript_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
115
+ mime="text/markdown"
116
+ )
117
+
118
+ # Display lesson plan in second tab
119
+ with tab2:
120
+ if lesson_plan:
121
+ st.markdown(lesson_plan)
122
+ # Download button for lesson plan
123
+ st.download_button(
124
+ label="Download Lesson Plan",
125
+ data=lesson_plan,
126
+ file_name=f"lesson_plan_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md",
127
+ mime="text/markdown"
128
+ )
129
+
130
+ # Cleanup
131
+ os.remove(temp_path)
132
+
133
+ # Right column instructions when no file is uploaded
134
+ if not uploaded_file:
135
+ with col2:
136
+ st.info("""
137
+ πŸ‘ˆ Start by uploading an audio file on the left side.
138
+
139
+ The system will automatically:
140
+ 1. Transcribe the lecture with timestamps
141
+ 2. Generate a structured lesson plan
142
+ 3. Provide downloadable versions of both
143
+
144
+ Supported formats: MP3, WAV, M4A
145
+ """)
146
 
147
  if __name__ == "__main__":
148
+ main()