abdullahzunorain commited on
Commit
9991260
·
verified ·
1 Parent(s): 846fe39

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -2
app.py CHANGED
@@ -12,11 +12,15 @@ st.write("Upload your audio or video file, and this app will transcribe the audi
12
  GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
13
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
14
 
 
 
 
 
15
  # Upload the audio or video file
16
  uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
17
 
18
  # Function to extract audio from video
19
- def extract_audio(video_path, audio_path="temp_audio.wav"):
20
  """Extracts audio from video."""
21
  try:
22
  # Run ffmpeg command with stderr capture for better error handling
@@ -47,7 +51,7 @@ def summarize_text(text):
47
  def process_media(media_file):
48
  """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
49
  # Save the uploaded file to a temporary path
50
- temp_file_path = f"temp/{media_file.name}"
51
  with open(temp_file_path, "wb") as f:
52
  f.write(media_file.getbuffer())
53
 
@@ -78,3 +82,92 @@ if uploaded_file is not None and GROQ_API_KEY:
78
  process_media(uploaded_file)
79
  else:
80
  st.warning("Please upload a file and enter your Groq API key.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
13
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
14
 
15
+ # Create a temporary directory if it does not exist
16
+ temp_dir = "temp"
17
+ os.makedirs(temp_dir, exist_ok=True)
18
+
19
  # Upload the audio or video file
20
  uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
21
 
22
  # Function to extract audio from video
23
+ def extract_audio(video_path, audio_path="temp/temp_audio.wav"):
24
  """Extracts audio from video."""
25
  try:
26
  # Run ffmpeg command with stderr capture for better error handling
 
51
  def process_media(media_file):
52
  """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
53
  # Save the uploaded file to a temporary path
54
+ temp_file_path = os.path.join(temp_dir, media_file.name)
55
  with open(temp_file_path, "wb") as f:
56
  f.write(media_file.getbuffer())
57
 
 
82
  process_media(uploaded_file)
83
  else:
84
  st.warning("Please upload a file and enter your Groq API key.")
85
+
86
+
87
+
88
+
89
+
90
+
91
+
92
+
93
+
94
+ # import os
95
+ # import ffmpeg
96
+ # import whisper
97
+ # import streamlit as st
98
+ # from groq import Groq
99
+
100
+ # # Set the title and description of the app
101
+ # st.title("Audio/Video Transcription and Summarization")
102
+ # st.write("Upload your audio or video file, and this app will transcribe the audio and provide a summary of the transcription.")
103
+
104
+ # # Get the API key from user input (You may want to use Streamlit secrets management)
105
+ # GROQ_API_KEY = st.text_input("Enter your Groq API Key:")
106
+ # os.environ["GROQ_API_KEY"] = GROQ_API_KEY
107
+
108
+ # # Upload the audio or video file
109
+ # uploaded_file = st.file_uploader("Choose an audio or video file...", type=["mp4", "mov", "avi", "mkv", "wav", "mp3"])
110
+
111
+ # # Function to extract audio from video
112
+ # def extract_audio(video_path, audio_path="temp_audio.wav"):
113
+ # """Extracts audio from video."""
114
+ # try:
115
+ # # Run ffmpeg command with stderr capture for better error handling
116
+ # ffmpeg.input(video_path).output(audio_path).run(overwrite_output=True, capture_stdout=True, capture_stderr=True)
117
+ # except ffmpeg.Error as e:
118
+ # st.error("FFmpeg error encountered: " + e.stderr.decode())
119
+ # return audio_path
120
+
121
+ # # Function to transcribe audio to text using Whisper model
122
+ # def transcribe_audio(audio_path):
123
+ # """Transcribes audio to text using Whisper model."""
124
+ # model = whisper.load_model("base") # Load the Whisper model
125
+ # result = model.transcribe(audio_path)
126
+ # return result["text"]
127
+
128
+ # # Function to summarize text using Groq API
129
+ # def summarize_text(text):
130
+ # """Summarizes text using Groq API."""
131
+ # client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
132
+ # response = client.chat.completions.create(
133
+ # messages=[{"role": "user", "content": f"Summarize the following text: {text}"}],
134
+ # model="llama3-8b-8192"
135
+ # )
136
+ # summary = response.choices[0].message.content
137
+ # return summary
138
+
139
+ # # Complete function to process audio or video
140
+ # def process_media(media_file):
141
+ # """Processes audio or video: extracts audio, transcribes it, and summarizes the transcription."""
142
+ # # Save the uploaded file to a temporary path
143
+ # temp_file_path = f"temp/{media_file.name}"
144
+ # with open(temp_file_path, "wb") as f:
145
+ # f.write(media_file.getbuffer())
146
+
147
+ # # Determine if the file is a video or audio based on the file extension
148
+ # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
149
+ # # Step 1: Extract audio from video
150
+ # audio_path = extract_audio(temp_file_path)
151
+ # else:
152
+ # audio_path = temp_file_path # If it's already audio, use it as is
153
+
154
+ # # Step 2: Transcribe audio to text
155
+ # transcription = transcribe_audio(audio_path)
156
+ # st.write("### Transcription:")
157
+ # st.write(transcription)
158
+
159
+ # # Step 3: Summarize transcription
160
+ # summary = summarize_text(transcription)
161
+ # st.write("### Summary:")
162
+ # st.write(summary)
163
+
164
+ # # Clean up temporary files if needed
165
+ # os.remove(temp_file_path)
166
+ # if media_file.name.endswith(('.mp4', '.mov', '.avi', '.mkv')):
167
+ # os.remove(audio_path)
168
+
169
+ # # Run the app
170
+ # if uploaded_file is not None and GROQ_API_KEY:
171
+ # process_media(uploaded_file)
172
+ # else:
173
+ # st.warning("Please upload a file and enter your Groq API key.")