abdullahzunorain commited on
Commit
88eaaac
·
verified ·
1 Parent(s): 3c5be40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -27
app.py CHANGED
@@ -1,26 +1,31 @@
1
- # # Set your Groq API key here or use environment variable
2
- # GROQ_API_TOKEN = os.getenv("groq_api")
3
- # client = Groq(api_key=GROQ_API_TOKEN)
4
-
5
  import os
6
  import ffmpeg
7
  import whisper
8
  import streamlit as st
9
  from groq import Groq
10
 
11
- # Set the title and description of the app
12
- st.title("Audio/Video Transcription and Summarization")
13
- st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
 
 
 
 
 
 
 
 
14
 
15
  # Retrieve the API key from environment variables or Streamlit secrets
16
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or st.secrets["GROQ_API_KEY"]
17
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
18
 
19
- # Create a temporary directory if it does not exist
20
  temp_dir = "temp"
21
  os.makedirs(temp_dir, exist_ok=True)
22
 
23
- # Upload the audio or video file
 
24
  uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
25
 
26
  # Function to extract audio from video
@@ -30,13 +35,13 @@ def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
30
  # Run ffmpeg command with stderr capture for better error handling
31
  ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
32
  except ffmpeg.Error as e:
33
- st.error("FFmpeg error encountered: " + e.stderr.decode())
34
  return audio_path
35
 
36
- # Function to transcribe audio to text using Whisper model
37
  def transcribe_audio(audio_path):
38
  """Transcribes audio to text using Whisper model."""
39
- model = whisper.load_model("base") # Load the Whisper model
40
  result = model.transcribe(audio_path)
41
  return result["text"]
42
 
@@ -51,46 +56,153 @@ def summarize_text(text):
51
  summary = response.choices[0].message.content
52
  return summary
53
 
54
- # Complete function to process audio or video
55
  def process_media(media_file):
56
- """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
57
  # Save the uploaded file to a temporary path
58
  temp_file_path = os.path.join(temp_dir, media_file.name)
59
  with open(temp_file_path, "wb") as f:
60
  f.write(media_file.getbuffer())
61
 
62
- # Determine if the file is a video or audio based on the file extension
63
  if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
64
- # Step 1: Extract audio from video
65
  audio_path = extract_audio(temp_file_path)
66
  else:
67
- audio_path = temp_file_path # If it's already audio, use it as is
68
 
69
- # Step 2: Transcribe audio to text
70
- transcription = transcribe_audio(audio_path)
 
 
71
  st.write("### Transcription:")
72
  st.write(transcription)
73
 
74
- # Step 3: Summarize transcription
75
- summary = summarize_text(transcription)
76
- st.write("### Summary:")
77
- st.write(summary)
78
-
79
- # Clean up temporary files if needed
 
 
 
80
  os.remove(temp_file_path)
81
  if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
82
  os.remove(audio_path)
83
 
84
- # Run the app
85
  if uploaded_file is not None:
 
86
  process_media(uploaded_file)
87
  else:
88
- st.warning("Please upload a file.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
 
 
 
 
 
90
 
91
 
92
 
93
 
 
94
 
95
 
96
 
 
 
 
 
 
1
  import os
2
  import ffmpeg
3
  import whisper
4
  import streamlit as st
5
  from groq import Groq
6
 
7
+ # Set the app title and description with styling
8
+ st.set_page_config(page_title="Audio/Video Transcription & Summarization", page_icon="🎙️")
9
+ st.title("🎙️ Audio/Video Transcription & Summarization")
10
+ st.write("Easily upload an audio or video file to get a transcription and a quick summary.")
11
+
12
+ # Add a sidebar for settings and instructions
13
+ with st.sidebar:
14
+ st.header("Settings")
15
+ st.write("Configure app preferences here.")
16
+ enable_summary = st.checkbox("Enable Summarization", value=True)
17
+ st.info("Note: Summarization uses the Groq API.")
18
 
19
  # Retrieve the API key from environment variables or Streamlit secrets
20
  GROQ_API_KEY = os.getenv("GROQ_API_KEY") or st.secrets["GROQ_API_KEY"]
21
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
22
 
23
+ # Create a temporary directory
24
  temp_dir = "temp"
25
  os.makedirs(temp_dir, exist_ok=True)
26
 
27
+ # Display file uploader with improved layout and style
28
+ st.subheader("Upload Audio/Video File")
29
  uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
30
 
31
  # Function to extract audio from video
 
35
  # Run ffmpeg command with stderr capture for better error handling
36
  ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
37
  except ffmpeg.Error as e:
38
+ st.error("Error processing file with FFmpeg: " + e.stderr.decode())
39
  return audio_path
40
 
41
+ # Function to transcribe audio using Whisper model
42
  def transcribe_audio(audio_path):
43
  """Transcribes audio to text using Whisper model."""
44
+ model = whisper.load_model("base")
45
  result = model.transcribe(audio_path)
46
  return result["text"]
47
 
 
56
  summary = response.choices[0].message.content
57
  return summary
58
 
59
+ # Main processing function with progress indicators
60
  def process_media(media_file):
61
+ """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription if enabled."""
62
  # Save the uploaded file to a temporary path
63
  temp_file_path = os.path.join(temp_dir, media_file.name)
64
  with open(temp_file_path, "wb") as f:
65
  f.write(media_file.getbuffer())
66
 
67
+ # Determine if the file is a video or audio
68
  if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
69
+ st.info("Extracting audio from video...")
70
  audio_path = extract_audio(temp_file_path)
71
  else:
72
+ audio_path = temp_file_path # If already audio, use it as is
73
 
74
+ # Transcribe audio to text with progress spinner
75
+ with st.spinner("Transcribing audio..."):
76
+ transcription = transcribe_audio(audio_path)
77
+ st.success("Transcription completed!")
78
  st.write("### Transcription:")
79
  st.write(transcription)
80
 
81
+ # Summarize transcription if enabled
82
+ if enable_summary:
83
+ with st.spinner("Generating summary..."):
84
+ summary = summarize_text(transcription)
85
+ st.success("Summary generated!")
86
+ st.write("### Summary:")
87
+ st.write(summary)
88
+
89
+ # Cleanup temporary files
90
  os.remove(temp_file_path)
91
  if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
92
  os.remove(audio_path)
93
 
94
+ # Run the app and handle file upload state
95
  if uploaded_file is not None:
96
+ st.info("Processing your file...")
97
  process_media(uploaded_file)
98
  else:
99
+ st.warning("Please upload an audio or video file to begin.")
100
+
101
+
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+
112
+
113
+ # # # Set your Groq API key here or use environment variable
114
+ # # GROQ_API_TOKEN = os.getenv("groq_api")
115
+ # # client = Groq(api_key=GROQ_API_TOKEN)
116
+
117
+ # import os
118
+ # import ffmpeg
119
+ # import whisper
120
+ # import streamlit as st
121
+ # from groq import Groq
122
+
123
+ # # Set the title and description of the app
124
+ # st.title("Audio/Video Transcription and Summarization")
125
+ # st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
126
+
127
+ # # Retrieve the API key from environment variables or Streamlit secrets
128
+ # GROQ_API_KEY = os.getenv("GROQ_API_KEY") or st.secrets["GROQ_API_KEY"]
129
+ # os.environ["GROQ_API_KEY"] = GROQ_API_KEY
130
+
131
+ # # Create a temporary directory if it does not exist
132
+ # temp_dir = "temp"
133
+ # os.makedirs(temp_dir, exist_ok=True)
134
+
135
+ # # Upload the audio or video file
136
+ # uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
137
+
138
+ # # Function to extract audio from video
139
+ # def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
140
+ # """Extracts audio from video."""
141
+ # try:
142
+ # # Run ffmpeg command with stderr capture for better error handling
143
+ # ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
144
+ # except ffmpeg.Error as e:
145
+ # st.error("FFmpeg error encountered: " + e.stderr.decode())
146
+ # return audio_path
147
+
148
+ # # Function to transcribe audio to text using Whisper model
149
+ # def transcribe_audio(audio_path):
150
+ # """Transcribes audio to text using Whisper model."""
151
+ # model = whisper.load_model("base") # Load the Whisper model
152
+ # result = model.transcribe(audio_path)
153
+ # return result["text"]
154
+
155
+ # # Function to summarize text using Groq API
156
+ # def summarize_text(text):
157
+ # """Summarizes text using Groq API."""
158
+ # client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
159
+ # response = client.chat.completions.create(
160
+ # messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
161
+ # model="llama3-8b-8192"
162
+ # )
163
+ # summary = response.choices[0].message.content
164
+ # return summary
165
+
166
+ # # Complete function to process audio or video
167
+ # def process_media(media_file):
168
+ # """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
169
+ # # Save the uploaded file to a temporary path
170
+ # temp_file_path = os.path.join(temp_dir, media_file.name)
171
+ # with open(temp_file_path, "wb") as f:
172
+ # f.write(media_file.getbuffer())
173
+
174
+ # # Determine if the file is a video or audio based on the file extension
175
+ # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
176
+ # # Step 1: Extract audio from video
177
+ # audio_path = extract_audio(temp_file_path)
178
+ # else:
179
+ # audio_path = temp_file_path # If it's already audio, use it as is
180
+
181
+ # # Step 2: Transcribe audio to text
182
+ # transcription = transcribe_audio(audio_path)
183
+ # st.write("### Transcription:")
184
+ # st.write(transcription)
185
+
186
+ # # Step 3: Summarize transcription
187
+ # summary = summarize_text(transcription)
188
+ # st.write("### Summary:")
189
+ # st.write(summary)
190
+
191
+ # # Clean up temporary files if needed
192
+ # os.remove(temp_file_path)
193
+ # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
194
+ # os.remove(audio_path)
195
 
196
+ # # Run the app
197
+ # if uploaded_file is not None:
198
+ # process_media(uploaded_file)
199
+ # else:
200
+ # st.warning("Please upload a file.")
201
 
202
 
203
 
204
 
205
+ # ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
206
 
207
 
208