Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,71 @@
|
|
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"
|
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}")
|
|
|
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}")
|