abdullahzunorain commited on
Commit
922156f
ยท
verified ยท
1 Parent(s): 88eaaac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -212
app.py CHANGED
@@ -1,138 +1,31 @@
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
32
- def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
33
- """Extracts audio from video."""
34
- try:
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
-
48
- # Function to summarize text using Groq API
49
- def summarize_text(text):
50
- """Summarizes text using Groq API."""
51
- client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
52
- response = client.chat.completions.create(
53
- messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
54
- model="llama3-8b-8192"
55
- )
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
@@ -142,13 +35,13 @@ else:
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
 
@@ -163,131 +56,212 @@ else:
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
 
209
 
210
- # import os
211
- # import ffmpeg
212
- # import whisper
213
- # import streamlit as st
214
- # from groq import Groq
215
 
216
- # # Set the title and description of the app
217
- # st.title("Audio/Video Transcription and Summarization")
218
- # st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
219
 
220
- # # Get the API key from user input (You may want to use Streamlit secrets management)
221
- # GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
222
- # os.environ["GROQ_API_KEY"] = GROQ_API_KEY
223
 
224
- # # Create a temporary directory if it does not exist
225
- # temp_dir = "temp"
226
- # os.makedirs(temp_dir, exist_ok=True)
 
 
227
 
228
- # # Upload the audio or video file
229
- # uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
230
 
231
- # # Function to extract audio from video
232
- # def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
233
- # """Extracts audio from video."""
234
- # try:
235
- # # Run ffmpeg command with stderr capture for better error handling
236
- # ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
237
- # except ffmpeg.Error as e:
238
- # st.error("FFmpeg error encountered: " + e.stderr.decode())
239
- # return audio_path
240
 
241
- # # Function to transcribe audio to text using Whisper model
242
- # def transcribe_audio(audio_path):
243
- # """Transcribes audio to text using Whisper model."""
244
- # model = whisper.load_model("base") # Load the Whisper model
245
- # result = model.transcribe(audio_path)
246
- # return result["text"]
247
 
248
- # # Function to summarize text using Groq API
249
- # def summarize_text(text):
250
- # """Summarizes text using Groq API."""
251
- # client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
252
- # response = client.chat.completions.create(
253
- # messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
254
- # model="llama3-8b-8192"
255
- # )
256
- # summary = response.choices[0].message.content
257
- # return summary
258
 
259
- # # Complete function to process audio or video
260
- # def process_media(media_file):
261
- # """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
262
- # # Save the uploaded file to a temporary path
263
- # temp_file_path = os.path.join(temp_dir, media_file.name)
264
- # with open(temp_file_path, "wb") as f:
265
- # f.write(media_file.getbuffer())
266
 
267
- # # Determine if the file is a video or audio based on the file extension
268
- # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
269
- # # Step 1: Extract audio from video
270
- # audio_path = extract_audio(temp_file_path)
271
- # else:
272
- # audio_path = temp_file_path # If it's already audio, use it as is
273
 
274
- # # Step 2: Transcribe audio to text
275
- # transcription = transcribe_audio(audio_path)
276
- # st.write("### Transcription:")
277
- # st.write(transcription)
278
-
279
- # # Step 3: Summarize transcription
280
- # summary = summarize_text(transcription)
281
- # st.write("### Summary:")
282
- # st.write(summary)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
283
 
284
- # # Clean up temporary files if needed
285
- # os.remove(temp_file_path)
286
- # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
287
- # os.remove(audio_path)
 
 
 
288
 
289
- # # Run the app
290
- # if uploaded_file is not None and GROQ_API_KEY:
291
- # process_media(uploaded_file)
292
- # else:
293
- # st.warning("Please upload a file and enter your Groq API key.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
112
+ import ffmpeg
113
+ import whisper
114
+ import streamlit as st
115
+ from groq import Groq
116
 
117
+ # Custom CSS for styling
118
+ st.markdown("""
119
+ <style>
120
+ /* Background gradient and color settings */
121
+ .stApp {
122
+ background-image: linear-gradient(to right, #2e2e2e, #454545);
123
+ color: white;
124
+ font-family: Arial, sans-serif;
125
+ }
126
+ /* Container box styling */
127
+ .container {
128
+ background-color: #333;
129
+ padding: 2rem;
130
+ border-radius: 10px;
131
+ box-shadow: 0px 4px 12px rgba(0, 0, 0, 0.4);
132
+ }
133
+ /* Header styling */
134
+ .header {
135
+ color: #ffdd40;
136
+ text-align: center;
137
+ font-size: 2.5rem;
138
+ margin-bottom: 1.5rem;
139
+ }
140
+ /* Subheader styling */
141
+ .subheader {
142
+ color: #f0f0f0;
143
+ text-align: center;
144
+ font-size: 1.2rem;
145
+ margin-bottom: 2rem;
146
+ }
147
+ /* Button styling */
148
+ .stButton>button {
149
+ background-color: #ffdd40;
150
+ color: #333;
151
+ border-radius: 5px;
152
+ padding: 0.6rem 1.5rem;
153
+ font-size: 1rem;
154
+ font-weight: bold;
155
+ }
156
+ .stButton>button:hover {
157
+ background-color: #ffcc00;
158
+ color: #222;
159
+ }
160
+ /* Footer styling */
161
+ .footer {
162
+ text-align: center;
163
+ color: #cfcfcf;
164
+ font-size: 0.9rem;
165
+ margin-top: 2rem;
166
+ }
167
+ </style>
168
+ """, unsafe_allow_html=True)
169
+
170
+ # App title and description with styling
171
+ st.markdown("<div class='header'>๐ŸŽ™๏ธ Audio/Video Transcription & Summarization</div>", unsafe_allow_html=True)
172
+ st.markdown("<div class='subheader'>Upload an audio or video file to get a transcription and a concise summary.</div>", unsafe_allow_html=True)
173
+
174
+ # Sidebar for settings and instructions
175
+ with st.sidebar:
176
+ st.header("Settings")
177
+ st.write("Customize your preferences:")
178
+ enable_summary = st.checkbox("Enable Summarization", value=True)
179
+ st.info("Note: Summarization uses the Groq API.")
180
 
181
+ # Retrieve the API key from environment variables or Streamlit secrets
182
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY") or st.secrets["GROQ_API_KEY"]
183
+ os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
 
 
 
 
 
184
 
185
+ # Create a temporary directory
186
+ temp_dir = "temp"
187
+ os.makedirs(temp_dir, exist_ok=True)
 
 
 
188
 
189
+ # Enhanced file upload area
190
+ st.markdown("<div class='container'>", unsafe_allow_html=True)
191
+ uploaded_file = st.file_uploader(
192
+ label="Select an audio or video file",
193
+ type=["mp4", "mov", "avi", "mkv", "wav", "mp3"],
194
+ help="Supported formats: mp4, mov, avi, mkv, wav, mp3"
195
+ )
 
 
 
196
 
197
+ # Function to extract audio from video
198
+ def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
199
+ try:
200
+ ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
201
+ except ffmpeg.Error as e:
202
+ st.error("Error processing file with FFmpeg: " + e.stderr.decode())
203
+ return audio_path
204
 
205
+ # Function to transcribe audio using Whisper model
206
+ def transcribe_audio(audio_path):
207
+ model = whisper.load_model("base")
208
+ result = model.transcribe(audio_path)
209
+ return result["text"]
 
210
 
211
+ # Function to summarize text using Groq API
212
+ def summarize_text(text):
213
+ client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
214
+ response = client.chat.completions.create(
215
+ messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
216
+ model="llama3-8b-8192"
217
+ )
218
+ summary = response.choices[0].message.content
219
+ return summary
220
+
221
+ # Main processing function with progress indicators
222
+ def process_media(media_file):
223
+ temp_file_path = os.path.join(temp_dir, media_file.name)
224
+ with open(temp_file_path, "wb") as f:
225
+ f.write(media_file.getbuffer())
226
+
227
+ # Extract audio if the file is a video
228
+ if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
229
+ st.info("Extracting audio from video...")
230
+ audio_path = extract_audio(temp_file_path)
231
+ else:
232
+ audio_path = temp_file_path
233
+
234
+ # Transcribe audio
235
+ with st.spinner("Transcribing audio..."):
236
+ transcription = transcribe_audio(audio_path)
237
+ st.success("Transcription completed!")
238
+ st.write("### Transcription:")
239
+ st.write(transcription)
240
 
241
+ # Summarize transcription if enabled
242
+ if enable_summary:
243
+ with st.spinner("Generating summary..."):
244
+ summary = summarize_text(transcription)
245
+ st.success("Summary generated!")
246
+ st.write("### Summary:")
247
+ st.write(summary)
248
 
249
+ # Cleanup
250
+ os.remove(temp_file_path)
251
+ if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
252
+ os.remove(audio_path)
253
+
254
+ if uploaded_file:
255
+ st.info("Processing your file, please wait...")
256
+ process_media(uploaded_file)
257
+ else:
258
+ st.warning("Please upload an audio or video file to begin.")
259
+
260
+ # Footer with branding
261
+ st.markdown("""
262
+ <div class="footer">
263
+ &copy; 2024 TranscribePro. Developed by Abdullah Zunorain.
264
+ </div>
265
+ """, unsafe_allow_html=True)
266
+
267
+ st.markdown("</div>", unsafe_allow_html=True)