Spaces:
Sleeping
Sleeping
Update app.py
#1
by
mahmad92
- opened
app.py
CHANGED
@@ -1,123 +1,123 @@
|
|
1 |
-
# 21F-9195 Muhammad Ahmad
|
2 |
-
import streamlit as st
|
3 |
-
import whisper
|
4 |
-
import tempfile
|
5 |
-
import os
|
6 |
-
import shutil
|
7 |
-
import requests
|
8 |
-
import zipfile
|
9 |
-
|
10 |
-
# Function to download and setup FFmpeg automatically
|
11 |
-
def setup_ffmpeg():
|
12 |
-
ffmpeg_dir = os.path.join(os.getcwd(), "ffmpeg_bin")
|
13 |
-
ffmpeg_path = os.path.join(ffmpeg_dir, "ffmpeg.exe")
|
14 |
-
|
15 |
-
if not os.path.exists(ffmpeg_path):
|
16 |
-
st.write("Downloading FFmpeg... β³")
|
17 |
-
url = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
|
18 |
-
zip_path = "ffmpeg.zip"
|
19 |
-
|
20 |
-
with open(zip_path, "wb") as f:
|
21 |
-
f.write(requests.get(url, stream=True).content)
|
22 |
-
|
23 |
-
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
24 |
-
zip_ref.extractall("ffmpeg_temp")
|
25 |
-
|
26 |
-
extracted_dir = [d for d in os.listdir("ffmpeg_temp") if os.path.isdir(os.path.join("ffmpeg_temp", d))][0]
|
27 |
-
shutil.move(os.path.join("ffmpeg_temp", extracted_dir, "bin"), ffmpeg_dir)
|
28 |
-
|
29 |
-
os.remove(zip_path)
|
30 |
-
shutil.rmtree("ffmpeg_temp")
|
31 |
-
st.write("FFmpeg setup complete β
")
|
32 |
-
|
33 |
-
return ffmpeg_dir
|
34 |
-
|
35 |
-
ffmpeg_dir = setup_ffmpeg()
|
36 |
-
os.environ["PATH"] += os.pathsep + ffmpeg_dir
|
37 |
-
|
38 |
-
@st.cache_resource
|
39 |
-
def load_model():
|
40 |
-
return whisper.load_model("small")
|
41 |
-
|
42 |
-
model = load_model()
|
43 |
-
|
44 |
-
# Custom CSS for styling
|
45 |
-
st.markdown(
|
46 |
-
"""
|
47 |
-
<style>
|
48 |
-
body {
|
49 |
-
background-color: #1e1e1e;
|
50 |
-
color: white;
|
51 |
-
font-family: 'Arial', sans-serif;
|
52 |
-
}
|
53 |
-
.stButton>button {
|
54 |
-
background-color: #4CAF50;
|
55 |
-
color: white;
|
56 |
-
border: none;
|
57 |
-
border-radius: 8px;
|
58 |
-
padding: 10px 20px;
|
59 |
-
font-size: 16px;
|
60 |
-
margin: 10px 0;
|
61 |
-
}
|
62 |
-
.stButton>button:hover {
|
63 |
-
background-color: #45a049;
|
64 |
-
}
|
65 |
-
.uploaded-file-details {
|
66 |
-
margin-top: 15px;
|
67 |
-
font-size: 14px;
|
68 |
-
color: #ddd;
|
69 |
-
}
|
70 |
-
</style>
|
71 |
-
""",
|
72 |
-
unsafe_allow_html=True
|
73 |
-
)
|
74 |
-
|
75 |
-
# Streamlit App Layout
|
76 |
-
st.title("π€
|
77 |
-
st.markdown(
|
78 |
-
"""
|
79 |
-
Welcome to the **Audio-to-Text Converter**!
|
80 |
-
Effortlessly convert your audio files into text and subtitles.
|
81 |
-
|
82 |
-
πΉ **Features**:
|
83 |
-
- Generate a **Text File** and **Subtitle (SRT) File**.
|
84 |
-
- Supports multiple audio formats: `mp3`, `mp4`, `m4a`, `wav`.
|
85 |
-
"""
|
86 |
-
)
|
87 |
-
def format_timestamp(seconds):
|
88 |
-
"""Format a timestamp in seconds to the SRT format: HH:MM:SS,ms."""
|
89 |
-
milliseconds = int((seconds % 1) * 1000)
|
90 |
-
seconds = int(seconds)
|
91 |
-
minutes, seconds = divmod(seconds, 60)
|
92 |
-
hours, minutes = divmod(minutes, 60)
|
93 |
-
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
|
94 |
-
|
95 |
-
# File Upload Section
|
96 |
-
st.header("π€ Upload Your Audio File")
|
97 |
-
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "mp4", "m4a", "wav"])
|
98 |
-
|
99 |
-
if uploaded_file:
|
100 |
-
st.audio(uploaded_file, format="audio/mp3")
|
101 |
-
file_size = uploaded_file.size / (1024 * 1024)
|
102 |
-
st.markdown(
|
103 |
-
f"""
|
104 |
-
<div class="uploaded-file-details">
|
105 |
-
<strong>Filename:</strong> {uploaded_file.name} <br>
|
106 |
-
<strong>File Size:</strong> {file_size:.2f} MB
|
107 |
-
</div>
|
108 |
-
""",
|
109 |
-
unsafe_allow_html=True,
|
110 |
-
)
|
111 |
-
|
112 |
-
if st.button("Convert Audio to Text"):
|
113 |
-
with st.spinner("Transcription in progress... This might take a while β³"):
|
114 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
115 |
-
temp_audio.write(uploaded_file.read())
|
116 |
-
temp_audio_path = temp_audio.name
|
117 |
-
|
118 |
-
result = model.transcribe(temp_audio_path)
|
119 |
-
os.remove(temp_audio_path)
|
120 |
-
|
121 |
-
st.success("Transcription Complete! π")
|
122 |
-
st.subheader("Transcription:")
|
123 |
-
st.write(result["text"])
|
|
|
1 |
+
# 21F-9195 Muhammad Ahmad
|
2 |
+
import streamlit as st
|
3 |
+
import whisper
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
import shutil
|
7 |
+
import requests
|
8 |
+
import zipfile
|
9 |
+
|
10 |
+
# Function to download and setup FFmpeg automatically
|
11 |
+
def setup_ffmpeg():
|
12 |
+
ffmpeg_dir = os.path.join(os.getcwd(), "ffmpeg_bin")
|
13 |
+
ffmpeg_path = os.path.join(ffmpeg_dir, "ffmpeg.exe")
|
14 |
+
|
15 |
+
if not os.path.exists(ffmpeg_path):
|
16 |
+
st.write("Downloading FFmpeg... β³")
|
17 |
+
url = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"
|
18 |
+
zip_path = "ffmpeg.zip"
|
19 |
+
|
20 |
+
with open(zip_path, "wb") as f:
|
21 |
+
f.write(requests.get(url, stream=True).content)
|
22 |
+
|
23 |
+
with zipfile.ZipFile(zip_path, "r") as zip_ref:
|
24 |
+
zip_ref.extractall("ffmpeg_temp")
|
25 |
+
|
26 |
+
extracted_dir = [d for d in os.listdir("ffmpeg_temp") if os.path.isdir(os.path.join("ffmpeg_temp", d))][0]
|
27 |
+
shutil.move(os.path.join("ffmpeg_temp", extracted_dir, "bin"), ffmpeg_dir)
|
28 |
+
|
29 |
+
os.remove(zip_path)
|
30 |
+
shutil.rmtree("ffmpeg_temp")
|
31 |
+
st.write("FFmpeg setup complete β
")
|
32 |
+
|
33 |
+
return ffmpeg_dir
|
34 |
+
|
35 |
+
ffmpeg_dir = setup_ffmpeg()
|
36 |
+
os.environ["PATH"] += os.pathsep + ffmpeg_dir
|
37 |
+
|
38 |
+
@st.cache_resource
|
39 |
+
def load_model():
|
40 |
+
return whisper.load_model("small")
|
41 |
+
|
42 |
+
model = load_model()
|
43 |
+
|
44 |
+
# Custom CSS for styling
|
45 |
+
st.markdown(
|
46 |
+
"""
|
47 |
+
<style>
|
48 |
+
body {
|
49 |
+
background-color: #1e1e1e;
|
50 |
+
color: white;
|
51 |
+
font-family: 'Arial', sans-serif;
|
52 |
+
}
|
53 |
+
.stButton>button {
|
54 |
+
background-color: #4CAF50;
|
55 |
+
color: white;
|
56 |
+
border: none;
|
57 |
+
border-radius: 8px;
|
58 |
+
padding: 10px 20px;
|
59 |
+
font-size: 16px;
|
60 |
+
margin: 10px 0;
|
61 |
+
}
|
62 |
+
.stButton>button:hover {
|
63 |
+
background-color: #45a049;
|
64 |
+
}
|
65 |
+
.uploaded-file-details {
|
66 |
+
margin-top: 15px;
|
67 |
+
font-size: 14px;
|
68 |
+
color: #ddd;
|
69 |
+
}
|
70 |
+
</style>
|
71 |
+
""",
|
72 |
+
unsafe_allow_html=True
|
73 |
+
)
|
74 |
+
|
75 |
+
# Streamlit App Layout
|
76 |
+
st.title("π€ EchoTextAI")
|
77 |
+
st.markdown(
|
78 |
+
"""
|
79 |
+
Welcome to the **Audio-to-Text Converter**!
|
80 |
+
Effortlessly convert your audio files into text and subtitles.
|
81 |
+
|
82 |
+
πΉ **Features**:
|
83 |
+
- Generate a **Text File** and **Subtitle (SRT) File**.
|
84 |
+
- Supports multiple audio formats: `mp3`, `mp4`, `m4a`, `wav`.
|
85 |
+
"""
|
86 |
+
)
|
87 |
+
def format_timestamp(seconds):
|
88 |
+
"""Format a timestamp in seconds to the SRT format: HH:MM:SS,ms."""
|
89 |
+
milliseconds = int((seconds % 1) * 1000)
|
90 |
+
seconds = int(seconds)
|
91 |
+
minutes, seconds = divmod(seconds, 60)
|
92 |
+
hours, minutes = divmod(minutes, 60)
|
93 |
+
return f"{hours:02}:{minutes:02}:{seconds:02},{milliseconds:03}"
|
94 |
+
|
95 |
+
# File Upload Section
|
96 |
+
st.header("π€ Upload Your Audio File")
|
97 |
+
uploaded_file = st.file_uploader("Choose an audio file", type=["mp3", "mp4", "m4a", "wav"])
|
98 |
+
|
99 |
+
if uploaded_file:
|
100 |
+
st.audio(uploaded_file, format="audio/mp3")
|
101 |
+
file_size = uploaded_file.size / (1024 * 1024)
|
102 |
+
st.markdown(
|
103 |
+
f"""
|
104 |
+
<div class="uploaded-file-details">
|
105 |
+
<strong>Filename:</strong> {uploaded_file.name} <br>
|
106 |
+
<strong>File Size:</strong> {file_size:.2f} MB
|
107 |
+
</div>
|
108 |
+
""",
|
109 |
+
unsafe_allow_html=True,
|
110 |
+
)
|
111 |
+
|
112 |
+
if st.button("Convert Audio to Text"):
|
113 |
+
with st.spinner("Transcription in progress... This might take a while β³"):
|
114 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_audio:
|
115 |
+
temp_audio.write(uploaded_file.read())
|
116 |
+
temp_audio_path = temp_audio.name
|
117 |
+
|
118 |
+
result = model.transcribe(temp_audio_path)
|
119 |
+
os.remove(temp_audio_path)
|
120 |
+
|
121 |
+
st.success("Transcription Complete! π")
|
122 |
+
st.subheader("Transcription:")
|
123 |
+
st.write(result["text"])
|