Amarsaish commited on
Commit
6e5faa2
·
verified ·
1 Parent(s): 8cf577a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -19
app.py CHANGED
@@ -1,12 +1,80 @@
1
- # import os
2
- # import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  # from pydub import AudioSegment
4
  # from groq import Groq
5
-
6
- # # Set ffmpeg path
7
- # ffmpeg_path = r"ffmpeg.exe"
8
- # os.environ["PATH"] += os.pathsep + os.path.dirname(ffmpeg_path)
9
- # AudioSegment.converter = ffmpeg_path
10
 
11
  # # Groq API configuration
12
  # groq_api_key = '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' # Replace with your actual API key
@@ -70,16 +138,10 @@
70
  # except Exception as e:
71
  # st.error(f"Error during transcription: {e}")
72
 
73
-
74
- from pydub import AudioSegment
75
- from groq import Groq
76
  import os
77
  import streamlit as st
78
-
79
- # Groq API configuration
80
- groq_api_key = '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' # Replace with your actual API key
81
- client = Groq(api_key=groq_api_key)
82
- model = 'whisper-large-v3'
83
 
84
  # Function to ensure the file is in a suitable format
85
  def ensure_suitable_format(file_path):
@@ -98,7 +160,9 @@ def convert_audio_to_wav(input_path, output_path):
98
  return output_path
99
 
100
  # Function to transcribe audio using Groq
101
- def audio_to_text(filepath):
 
 
102
  with open(filepath, "rb") as file:
103
  translation = client.audio.translations.create(
104
  file=(filepath, file.read()),
@@ -110,10 +174,13 @@ def audio_to_text(filepath):
110
  st.title("Audio-to-Text Transcription")
111
  st.write("Upload an audio file to get the transcribed text.")
112
 
 
 
 
113
  # File upload
114
  uploaded_file = st.file_uploader("Upload your audio file", type=["flac", "mp3", "mp4", "mpeg", "mpga", "m4a", "ogg", "opus", "wav", "webm"])
115
 
116
- if uploaded_file:
117
  # Save the uploaded file locally
118
  file_path = os.path.join("uploaded_audio", uploaded_file.name)
119
  os.makedirs("uploaded_audio", exist_ok=True)
@@ -132,9 +199,10 @@ if uploaded_file:
132
  # Transcribe audio
133
  st.write("Processing transcription...")
134
  try:
135
- transcription = audio_to_text(converted_audio)
136
  st.success("Transcription complete!")
137
  st.text_area("Transcribed Text", transcription, height=200)
138
  except Exception as e:
139
  st.error(f"Error during transcription: {e}")
140
-
 
 
1
+ # # import os
2
+ # # import streamlit as st
3
+ # # from pydub import AudioSegment
4
+ # # from groq import Groq
5
+
6
+ # # # Set ffmpeg path
7
+ # # ffmpeg_path = r"ffmpeg.exe"
8
+ # # os.environ["PATH"] += os.pathsep + os.path.dirname(ffmpeg_path)
9
+ # # AudioSegment.converter = ffmpeg_path
10
+
11
+ # # # Groq API configuration
12
+ # # groq_api_key = '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' # Replace with your actual API key
13
+ # # client = Groq(api_key=groq_api_key)
14
+ # # model = 'whisper-large-v3'
15
+
16
+ # # # Function to ensure the file is in a suitable format
17
+ # # def ensure_suitable_format(file_path):
18
+ # # allowed_formats = ["flac", "mp3", "mp4", "mpeg", "mpga", "m4a", "ogg", "opus", "wav", "webm"]
19
+ # # file_extension = file_path.split('.')[-1].lower()
20
+ # # if file_extension not in allowed_formats:
21
+ # # new_file_path = f"{os.path.splitext(file_path)[0]}.wav"
22
+ # # os.rename(file_path, new_file_path)
23
+ # # return new_file_path
24
+ # # return file_path
25
+
26
+ # # # Function to convert audio to WAV
27
+ # # def convert_audio_to_wav(input_path, output_path):
28
+ # # audio = AudioSegment.from_file(input_path)
29
+ # # audio.export(output_path, format="wav")
30
+ # # return output_path
31
+
32
+ # # # Function to transcribe audio using Groq
33
+ # # def audio_to_text(filepath):
34
+ # # with open(filepath, "rb") as file:
35
+ # # translation = client.audio.translations.create(
36
+ # # file=(filepath, file.read()),
37
+ # # model=model,
38
+ # # )
39
+ # # return translation.text
40
+
41
+ # # # Streamlit App UI
42
+ # # st.title("Audio-to-Text Transcription")
43
+ # # st.write("Upload an audio file to get the transcribed text.")
44
+
45
+ # # # File upload
46
+ # # uploaded_file = st.file_uploader("Upload your audio file", type=["flac", "mp3", "mp4", "mpeg", "mpga", "m4a", "ogg", "opus", "wav", "webm"])
47
+
48
+ # # if uploaded_file:
49
+ # # # Save the uploaded file locally
50
+ # # file_path = os.path.join("uploaded_audio", uploaded_file.name)
51
+ # # os.makedirs("uploaded_audio", exist_ok=True)
52
+ # # with open(file_path, "wb") as f:
53
+ # # f.write(uploaded_file.getbuffer())
54
+
55
+ # # st.write(f"File uploaded: {uploaded_file.name}")
56
+
57
+ # # # Ensure file format is suitable
58
+ # # suitable_audio_path = ensure_suitable_format(file_path)
59
+
60
+ # # # Convert audio to WAV
61
+ # # wav_path = f"{os.path.splitext(suitable_audio_path)[0]}.wav"
62
+ # # converted_audio = convert_audio_to_wav(suitable_audio_path, wav_path)
63
+
64
+ # # # Transcribe audio
65
+ # # st.write("Processing transcription...")
66
+ # # try:
67
+ # # transcription = audio_to_text(converted_audio)
68
+ # # st.success("Transcription complete!")
69
+ # # st.text_area("Transcribed Text", transcription, height=200)
70
+ # # except Exception as e:
71
+ # # st.error(f"Error during transcription: {e}")
72
+
73
+
74
  # from pydub import AudioSegment
75
  # from groq import Groq
76
+ # import os
77
+ # import streamlit as st
 
 
 
78
 
79
  # # Groq API configuration
80
  # groq_api_key = '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' # Replace with your actual API key
 
138
  # except Exception as e:
139
  # st.error(f"Error during transcription: {e}")
140
 
 
 
 
141
  import os
142
  import streamlit as st
143
+ from pydub import AudioSegment
144
+ from groq import Groq
 
 
 
145
 
146
  # Function to ensure the file is in a suitable format
147
  def ensure_suitable_format(file_path):
 
160
  return output_path
161
 
162
  # Function to transcribe audio using Groq
163
+ def audio_to_text(filepath, groq_api_key):
164
+ client = Groq(api_key=groq_api_key)
165
+ model = 'whisper-large-v3'
166
  with open(filepath, "rb") as file:
167
  translation = client.audio.translations.create(
168
  file=(filepath, file.read()),
 
174
  st.title("Audio-to-Text Transcription")
175
  st.write("Upload an audio file to get the transcribed text.")
176
 
177
+ # Input for API key
178
+ groq_api_key = st.text_input("Enter your Groq API Key", type="password")
179
+
180
  # File upload
181
  uploaded_file = st.file_uploader("Upload your audio file", type=["flac", "mp3", "mp4", "mpeg", "mpga", "m4a", "ogg", "opus", "wav", "webm"])
182
 
183
+ if groq_api_key and uploaded_file:
184
  # Save the uploaded file locally
185
  file_path = os.path.join("uploaded_audio", uploaded_file.name)
186
  os.makedirs("uploaded_audio", exist_ok=True)
 
199
  # Transcribe audio
200
  st.write("Processing transcription...")
201
  try:
202
+ transcription = audio_to_text(converted_audio, groq_api_key)
203
  st.success("Transcription complete!")
204
  st.text_area("Transcribed Text", transcription, height=200)
205
  except Exception as e:
206
  st.error(f"Error during transcription: {e}")
207
+ elif not groq_api_key:
208
+ st.warning("Please enter your Groq API Key to proceed.")