Spaces:
Build error
Build error
Update app.py
Browse files
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
|
| 29 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
try:
|
| 31 |
-
system_prompt = """You are an
|
| 32 |
-
|
| 33 |
-
1.
|
| 34 |
-
2.
|
| 35 |
-
3.
|
| 36 |
-
4.
|
| 37 |
-
|
| 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"
|
| 45 |
],
|
| 46 |
temperature=0.3,
|
| 47 |
-
max_tokens=
|
| 48 |
)
|
| 49 |
|
| 50 |
return response.choices[0].message.content
|
| 51 |
except Exception as e:
|
| 52 |
-
st.error(f"Error
|
| 53 |
return None
|
| 54 |
|
| 55 |
-
def
|
| 56 |
-
"""
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
| 60 |
|
| 61 |
# Streamlit UI
|
| 62 |
def main():
|
|
@@ -64,65 +73,76 @@ def main():
|
|
| 64 |
|
| 65 |
st.title("π Lecture Notes Generator")
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
st.
|
| 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
|
| 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 |
-
|
| 86 |
-
|
| 87 |
-
|
| 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 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|