Spaces:
Build error
Build error
Update app.py
Browse files7 unmodified lines891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495963 unmodified lines10010110210310410510622 unmodified lines1291301311327 unmodified linesimport yt_dlpimport PyPDF2# Define your OpenAI API keyopenai.api_key = "YOUR_OPENAI_API_KEY"# Function to convert audio file to text using OpenAI's Whisperdef transcribe_audio(audio_file):# Load the audio fileaudio = AudioSegment.from_file(audio_file)# Export as WAV, which Whisper acceptsbuffer = io.BytesIO()audio.export(buffer, format="wav")buffer.seek(0)response = openai.Audio.transcribe("whisper-1",file=buffer,model='whisper',response_format='verbose_json')return response# Function to download audio from YouTube URLdef download_youtube_audio(url):ydl_opts = {'format': 'bestaudio/best','outtmpl': 'downloaded_audio.%(ext)s','postprocessors': [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '192',}],}with yt_dlp.YoutubeDL(ydl_opts) as ydl:ydl.download([url])return 'downloaded_audio.mp3'# Function to extract text from PDFdef extract_text_from_pdf(pdf_file):pdf_reader = PyPDF2.PdfFileReader(pdf_file)text = ""for page_num in range(pdf_reader.numPages):text += pdf_reader.getPage(page_num).extract_text() + "\n"return text# Function to generate summarised lecture notes using GPT-3.5def generate_summary(transcription):transcription_text = "\n".join([f"{segment['start']:.2f}-{segment['end']:.2f}: {segment['text']}" for segment in transcription['segments']])prompt = f"""You are an intelligent assistant that will summarize the transcription below.The transcription text is:{transcription_text}Summarize the content into 1000 tokens or less, focusing on the key topics and main points."""response = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role": "system", "content": "You are an expert summarizer."},{"role": "user", "content": prompt}])summary = response['choices'][0]['message']['content']return summary# Define the main function to handle transcription and summary generationdef process_lecture(input_type, audio_input, pdf_input, youtube_input, lesson_plan):transcription = ""try:if input_type == "Audio File":if audio_input is not None:transcription = transcribe_audio(audio_input)elif input_type == "YouTube URL":if youtube_input:audio_path = download_youtube_audio(youtube_input)with open(audio_path, "rb") as f:transcription = transcribe_audio(f)elif input_type == "PDF Document":if pdf_input is not None:transcription = extract_text_from_pdf(pdf_input)except Exception as e:return f"Error during processing: {str(e)}", "No summary available."3 unmodified linessummary = generate_summary(transcription)return transcription_text, summaryexcept Exception as e:return "Transcription generated, but error during summary generation: {str(e)}", "No summary available."else:return "No transcription available.", "No summary available."22 unmodified linessubmit_btn.click(fn=process_lecture, inputs=[input_type, audio_input, pdf_input, youtube_input, lesson_plan_input], outputs=[transcription_output, summary_output])# Launch the interfacedemo.launch(share=True)7 unmodified lines891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031043 unmodified lines10810911011111211311422 unmodified lines1371381391401417 unmodified linesimport yt_dlpimport PyPDF2# Define your OpenAI API key using environment variable (recommended for Hugging Face Spaces)openai.api_key = os.getenv("OPENAI_API_KEY")# Function to convert audio file to text using OpenAI's Whisperdef transcribe_audio(audio_file):try:# Load the audio fileaudio = AudioSegment.from_file(audio_file)# Export as WAV, which Whisper acceptsbuffer = io.BytesIO()audio.export(buffer, format="wav")buffer.seek(0)response = openai.Audio.transcribe("whisper-1",file=buffer,model='whisper',response_format='verbose_json')return responseexcept Exception as e:print(f"Error in transcribe_audio: {str(e)}")raise# Function to download audio from YouTube URLdef download_youtube_audio(url):try:ydl_opts = {'format': 'bestaudio/best','outtmpl': 'downloaded_audio.%(ext)s','postprocessors': [{'key': 'FFmpegExtractAudio','preferredcodec': 'mp3','preferredquality': '192',}],}with yt_dlp.YoutubeDL(ydl_opts) as ydl:ydl.download([url])return 'downloaded_audio.mp3'except Exception as e:print(f"Error in download_youtube_audio: {str(e)}")raise# Function to extract text from PDFdef extract_text_from_pdf(pdf_file):try:pdf_reader = PyPDF2.PdfFileReader(pdf_file)text = ""for page_num in range(pdf_reader.numPages):text += pdf_reader.getPage(page_num).extract_text() + "\n"return textexcept Exception as e:print(f"Error in extract_text_from_pdf: {str(e)}")raise# Function to generate summarised lecture notes using GPT-3.5def generate_summary(transcription):try:transcription_text = "\n".join([f"{segment['start']:.2f}-{segment['end']:.2f}: {segment['text']}" for segment in transcription['segments']])prompt = f"""You are an intelligent assistant that will summarize the transcription below.The transcription text is:{transcription_text}Summarize the content into 1000 tokens or less, focusing on the key topics and main points."""response = openai.ChatCompletion.create(model="gpt-3.5-turbo",messages=[{"role": "system", "content": "You are an expert summarizer."},{"role": "user", "content": prompt}])summary = response['choices'][0]['message']['content']return summaryexcept Exception as e:print(f"Error in generate_summary: {str(e)}")raise# Define the main function to handle transcription and summary generationdef process_lecture(input_type, audio_input, pdf_input, youtube_input, lesson_plan):transcription = ""try:if input_type == "Audio File" and audio_input is not None:transcription = transcribe_audio(audio_input)elif input_type == "YouTube URL" and youtube_input:audio_path = download_youtube_audio(youtube_input)with open(audio_path, "rb") as f:transcription = transcribe_audio(f)elif input_type == "PDF Document" and pdf_input is not None:transcription = extract_text_from_pdf(pdf_input)except Exception as e:return f"Error during processing: {str(e)}", "No summary available."3 unmodified linessummary = generate_summary(transcription)return transcription_text, summaryexcept Exception as e:return f"Transcription generated, but error during summary generation: {str(e)}", "No summary available."else:return "No transcription available.", "No summary available."22 unmodified linessubmit_btn.click(fn=process_lecture, inputs=[input_type, audio_input, pdf_input, youtube_input, lesson_plan_input], outputs=[transcription_output, summary_output])# Launch the interfaceif __name__ == "__main__":demo.launch(share=True)
app.py+77-68