import os import openai import streamlit as st import io from pydub import AudioSegment from youtube_transcript_api import YouTubeTranscriptApi import PyPDF2 from pptx import Presentation # Set OpenAI API key openai.api_key = os.getenv('OPENAI_API_KEY') # Function to transcribe audio using OpenAI Whisper def transcribe_audio(audio_file): try: # Load audio file audio = AudioSegment.from_file(audio_file) # Convert to WAV buffer = io.BytesIO() audio.export(buffer, format="wav") buffer.seek(0) # Set name attribute buffer.name = "audio.wav" # Transcribe audio response = openai.Audio.transcribe( "whisper-1", file=buffer, response_format="verbose_json" ) return response except Exception as e: st.error(f"Error during transcription: {str(e)}") return None # Function to extract text from PDF and split it into chunks def extract_text_from_pdf(pdf_file): reader = PyPDF2.PdfReader(io.BytesIO(pdf_file.read())) text = "" for page in reader.pages: text += page.extract_text() + "\n" # Split text into chunks of approximately 1500 words each words = text.split() chunks = [" ".join(words[i:i + 1500]) for i in range(0, len(words), 1500)] return chunks # Function to get YouTube transcript def get_youtube_transcript(url): try: # Extract video ID from URL if "watch?v=" in url: video_id = url.split("watch?v=")[1].split("&")[0] elif "youtu.be/" in url: video_id = url.split("youtu.be/")[1].split("?")[0] else: st.error("Invalid YouTube URL.") return None # Fetch transcript transcript_list = YouTubeTranscriptApi.list_transcripts(video_id) # Choose a transcript transcript = transcript_list.find_transcript(['en']) # Fetch the actual transcript data transcript_data = transcript.fetch() # Combine transcript texts transcription_text = "\n".join([entry['text'] for entry in transcript_data]) return transcription_text except Exception as e: st.error(f"Error fetching YouTube transcript: {str(e)}") return None # Function to generate notes for each chunk def generate_notes(text): prompt = f"Create comprehensive notes in bullet points from the following text:\n\n{text}" response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=[{'role': 'user', 'content': prompt}], max_tokens=1000, ) return response.choices[0].message.content.strip() # Function to generate additional sections def generate_section(title, text): prompt = f"Generate a section titled '{title}' with 3-6 sentences based on the following text:\n\n{text}" response = openai.ChatCompletion.create( model='gpt-3.5-turbo', messages=[{'role': 'user', 'content': prompt}], max_tokens=500, ) return response.choices[0].message.content.strip() # Function to create PowerPoint presentation def create_presentation(summary, key_concepts, key_takeaways, case_studies, glossary, faqs): prs = Presentation() # Add title slide slide = prs.slides.add_slide(prs.slide_layouts[0]) title = slide.shapes.title subtitle = slide.placeholders[1] title.text = "Lecture Notes" subtitle.text = "Generated by AI Notes Generation Bot" # Add slides for each section def add_slide(title, content): slide = prs.slides.add_slide(prs.slide_layouts[1]) slide.shapes.title.text = title textbox = slide.placeholders[1] textbox.text = content add_slide("Summary", summary) add_slide("Key Concepts", key_concepts) add_slide("Key Takeaways", key_takeaways) add_slide("Case Studies/Examples", case_studies) add_slide("Glossary", glossary) add_slide("FAQs", faqs) # Save the presentation to a BytesIO object ppt_buffer = io.BytesIO() prs.save(ppt_buffer) ppt_buffer.seek(0) return ppt_buffer # Main app def main(): st.set_page_config(layout="wide") # Add custom CSS for gradient background st.markdown(""" """, unsafe_allow_html=True) st.markdown("
{summary}
" + "{key_concepts}
" + "{key_takeaways}
" + "{case_studies}
" "{glossary}
" "{faqs}
" "