abhishekjoel commited on
Commit
bdb27e7
·
verified ·
1 Parent(s): 5352661

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -68
app.py CHANGED
@@ -8,89 +8,97 @@ import io
8
  import yt_dlp
9
  import PyPDF2
10
 
11
- # Define your OpenAI API key
12
- openai.api_key = "YOUR_OPENAI_API_KEY"
13
 
14
  # Function to convert audio file to text using OpenAI's Whisper
15
-
16
  def transcribe_audio(audio_file):
17
- # Load the audio file
18
- audio = AudioSegment.from_file(audio_file)
19
- # Export as WAV, which Whisper accepts
20
- buffer = io.BytesIO()
21
- audio.export(buffer, format="wav")
22
- buffer.seek(0)
23
-
24
- response = openai.Audio.transcribe(
25
- "whisper-1",
26
- file=buffer,
27
- model='whisper',
28
- response_format='verbose_json'
29
- )
30
- return response
 
 
 
 
31
 
32
  # Function to download audio from YouTube URL
33
-
34
  def download_youtube_audio(url):
35
- ydl_opts = {
36
- 'format': 'bestaudio/best',
37
- 'outtmpl': 'downloaded_audio.%(ext)s',
38
- 'postprocessors': [{
39
- 'key': 'FFmpegExtractAudio',
40
- 'preferredcodec': 'mp3',
41
- 'preferredquality': '192',
42
- }],
43
- }
44
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
45
- ydl.download([url])
46
- return 'downloaded_audio.mp3'
 
 
 
 
47
 
48
  # Function to extract text from PDF
49
-
50
  def extract_text_from_pdf(pdf_file):
51
- pdf_reader = PyPDF2.PdfFileReader(pdf_file)
52
- text = ""
53
- for page_num in range(pdf_reader.numPages):
54
- text += pdf_reader.getPage(page_num).extract_text() + "\n"
55
- return text
 
 
 
 
56
 
57
  # Function to generate summarised lecture notes using GPT-3.5
58
-
59
  def generate_summary(transcription):
60
- transcription_text = "\n".join([f"{segment['start']:.2f}-{segment['end']:.2f}: {segment['text']}" for segment in transcription['segments']])
61
- prompt = f"""
62
- You are an intelligent assistant that will summarize the transcription below.
63
- The transcription text is:
64
- {transcription_text}
65
-
66
- Summarize the content into 1000 tokens or less, focusing on the key topics and main points.
67
- """
68
- response = openai.ChatCompletion.create(
69
- model="gpt-3.5-turbo",
70
- messages=[
71
- {"role": "system", "content": "You are an expert summarizer."},
72
- {"role": "user", "content": prompt}
73
- ]
74
- )
75
- summary = response['choices'][0]['message']['content']
76
- return summary
 
 
 
 
77
 
78
  # Define the main function to handle transcription and summary generation
79
-
80
  def process_lecture(input_type, audio_input, pdf_input, youtube_input, lesson_plan):
81
  transcription = ""
82
  try:
83
- if input_type == "Audio File":
84
- if audio_input is not None:
85
- transcription = transcribe_audio(audio_input)
86
- elif input_type == "YouTube URL":
87
- if youtube_input:
88
- audio_path = download_youtube_audio(youtube_input)
89
- with open(audio_path, "rb") as f:
90
- transcription = transcribe_audio(f)
91
- elif input_type == "PDF Document":
92
- if pdf_input is not None:
93
- transcription = extract_text_from_pdf(pdf_input)
94
  except Exception as e:
95
  return f"Error during processing: {str(e)}", "No summary available."
96
 
@@ -100,7 +108,7 @@ def process_lecture(input_type, audio_input, pdf_input, youtube_input, lesson_pl
100
  summary = generate_summary(transcription)
101
  return transcription_text, summary
102
  except Exception as e:
103
- return "Transcription generated, but error during summary generation: {str(e)}", "No summary available."
104
  else:
105
  return "No transcription available.", "No summary available."
106
 
@@ -129,4 +137,5 @@ with gr.Blocks() as demo:
129
  submit_btn.click(fn=process_lecture, inputs=[input_type, audio_input, pdf_input, youtube_input, lesson_plan_input], outputs=[transcription_output, summary_output])
130
 
131
  # Launch the interface
132
- demo.launch(share=True)
 
 
8
  import yt_dlp
9
  import PyPDF2
10
 
11
+ # Define your OpenAI API key using environment variable (recommended for Hugging Face Spaces)
12
+ openai.api_key = os.getenv("OPENAI_API_KEY")
13
 
14
  # Function to convert audio file to text using OpenAI's Whisper
 
15
  def transcribe_audio(audio_file):
16
+ try:
17
+ # Load the audio file
18
+ audio = AudioSegment.from_file(audio_file)
19
+ # Export as WAV, which Whisper accepts
20
+ buffer = io.BytesIO()
21
+ audio.export(buffer, format="wav")
22
+ buffer.seek(0)
23
+
24
+ response = openai.Audio.transcribe(
25
+ "whisper-1",
26
+ file=buffer,
27
+ model='whisper',
28
+ response_format='verbose_json'
29
+ )
30
+ return response
31
+ except Exception as e:
32
+ print(f"Error in transcribe_audio: {str(e)}")
33
+ raise
34
 
35
  # Function to download audio from YouTube URL
 
36
  def download_youtube_audio(url):
37
+ try:
38
+ ydl_opts = {
39
+ 'format': 'bestaudio/best',
40
+ 'outtmpl': 'downloaded_audio.%(ext)s',
41
+ 'postprocessors': [{
42
+ 'key': 'FFmpegExtractAudio',
43
+ 'preferredcodec': 'mp3',
44
+ 'preferredquality': '192',
45
+ }],
46
+ }
47
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
48
+ ydl.download([url])
49
+ return 'downloaded_audio.mp3'
50
+ except Exception as e:
51
+ print(f"Error in download_youtube_audio: {str(e)}")
52
+ raise
53
 
54
  # Function to extract text from PDF
 
55
  def extract_text_from_pdf(pdf_file):
56
+ try:
57
+ pdf_reader = PyPDF2.PdfFileReader(pdf_file)
58
+ text = ""
59
+ for page_num in range(pdf_reader.numPages):
60
+ text += pdf_reader.getPage(page_num).extract_text() + "\n"
61
+ return text
62
+ except Exception as e:
63
+ print(f"Error in extract_text_from_pdf: {str(e)}")
64
+ raise
65
 
66
  # Function to generate summarised lecture notes using GPT-3.5
 
67
  def generate_summary(transcription):
68
+ try:
69
+ transcription_text = "\n".join([f"{segment['start']:.2f}-{segment['end']:.2f}: {segment['text']}" for segment in transcription['segments']])
70
+ prompt = f"""
71
+ You are an intelligent assistant that will summarize the transcription below.
72
+ The transcription text is:
73
+ {transcription_text}
74
+
75
+ Summarize the content into 1000 tokens or less, focusing on the key topics and main points.
76
+ """
77
+ response = openai.ChatCompletion.create(
78
+ model="gpt-3.5-turbo",
79
+ messages=[
80
+ {"role": "system", "content": "You are an expert summarizer."},
81
+ {"role": "user", "content": prompt}
82
+ ]
83
+ )
84
+ summary = response['choices'][0]['message']['content']
85
+ return summary
86
+ except Exception as e:
87
+ print(f"Error in generate_summary: {str(e)}")
88
+ raise
89
 
90
  # Define the main function to handle transcription and summary generation
 
91
  def process_lecture(input_type, audio_input, pdf_input, youtube_input, lesson_plan):
92
  transcription = ""
93
  try:
94
+ if input_type == "Audio File" and audio_input is not None:
95
+ transcription = transcribe_audio(audio_input)
96
+ elif input_type == "YouTube URL" and youtube_input:
97
+ audio_path = download_youtube_audio(youtube_input)
98
+ with open(audio_path, "rb") as f:
99
+ transcription = transcribe_audio(f)
100
+ elif input_type == "PDF Document" and pdf_input is not None:
101
+ transcription = extract_text_from_pdf(pdf_input)
 
 
 
102
  except Exception as e:
103
  return f"Error during processing: {str(e)}", "No summary available."
104
 
 
108
  summary = generate_summary(transcription)
109
  return transcription_text, summary
110
  except Exception as e:
111
+ return f"Transcription generated, but error during summary generation: {str(e)}", "No summary available."
112
  else:
113
  return "No transcription available.", "No summary available."
114
 
 
137
  submit_btn.click(fn=process_lecture, inputs=[input_type, audio_input, pdf_input, youtube_input, lesson_plan_input], outputs=[transcription_output, summary_output])
138
 
139
  # Launch the interface
140
+ if __name__ == "__main__":
141
+ demo.launch(share=True)