Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,43 +9,44 @@ from openpyxl import Workbook
|
|
| 9 |
from openpyxl.styles import Font
|
| 10 |
from io import BytesIO
|
| 11 |
|
| 12 |
-
# ---------------------------
|
| 13 |
# PAGE CONFIG
|
| 14 |
-
# ---------------------------
|
| 15 |
st.set_page_config(
|
| 16 |
page_title="RecToText Pro",
|
| 17 |
layout="wide",
|
| 18 |
page_icon="🎤"
|
| 19 |
)
|
| 20 |
|
| 21 |
-
# ---------------------------
|
| 22 |
-
# SIDEBAR
|
| 23 |
-
# ---------------------------
|
| 24 |
st.sidebar.title("⚙️ Settings")
|
|
|
|
| 25 |
model_option = st.sidebar.selectbox(
|
| 26 |
"Select Whisper Model",
|
| 27 |
["base", "small"]
|
| 28 |
)
|
| 29 |
|
| 30 |
output_mode = st.sidebar.radio(
|
| 31 |
-
"Output
|
| 32 |
["Roman Urdu", "English"]
|
| 33 |
)
|
| 34 |
|
| 35 |
if st.sidebar.button("🧹 Clear Session"):
|
| 36 |
st.session_state.clear()
|
| 37 |
-
st.
|
| 38 |
|
| 39 |
-
# ---------------------------
|
| 40 |
# HEADER
|
| 41 |
-
# ---------------------------
|
| 42 |
st.markdown("<h1 style='text-align:center;'>🎤 RecToText Pro</h1>", unsafe_allow_html=True)
|
| 43 |
-
st.markdown("<p style='text-align:center;'>
|
| 44 |
st.divider()
|
| 45 |
|
| 46 |
-
# ---------------------------
|
| 47 |
# FUNCTIONS
|
| 48 |
-
# ---------------------------
|
| 49 |
|
| 50 |
@st.cache_resource
|
| 51 |
def load_model(model_size):
|
|
@@ -59,22 +60,19 @@ def clean_text(text):
|
|
| 59 |
return text
|
| 60 |
|
| 61 |
def convert_to_roman_urdu(text):
|
| 62 |
-
# Basic placeholder conversion logic
|
| 63 |
replacements = {
|
| 64 |
"ہے": "hai",
|
| 65 |
"میں": "main",
|
| 66 |
"اور": "aur",
|
| 67 |
"کیا": "kya",
|
| 68 |
-
"آپ": "aap"
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
for urdu, roman in replacements.items():
|
| 71 |
text = text.replace(urdu, roman)
|
| 72 |
return text
|
| 73 |
|
| 74 |
-
def process_audio(file_path, model):
|
| 75 |
-
result = model.transcribe(file_path)
|
| 76 |
-
return result
|
| 77 |
-
|
| 78 |
def create_excel(segments):
|
| 79 |
wb = Workbook()
|
| 80 |
ws = wb.active
|
|
@@ -97,12 +95,13 @@ def create_excel(segments):
|
|
| 97 |
excel_buffer.seek(0)
|
| 98 |
return excel_buffer
|
| 99 |
|
| 100 |
-
# ---------------------------
|
| 101 |
-
# FILE UPLOADER
|
| 102 |
-
# ---------------------------
|
| 103 |
uploaded_file = st.file_uploader(
|
| 104 |
-
"Upload Lecture Recording (.mp3, .wav, .m4a)",
|
| 105 |
-
type=["mp3", "wav", "m4a"]
|
|
|
|
| 106 |
)
|
| 107 |
|
| 108 |
if uploaded_file:
|
|
@@ -110,18 +109,19 @@ if uploaded_file:
|
|
| 110 |
st.audio(uploaded_file)
|
| 111 |
|
| 112 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 113 |
-
|
|
|
|
| 114 |
audio.export(tmp.name, format="wav")
|
| 115 |
temp_audio_path = tmp.name
|
| 116 |
|
| 117 |
-
st.info("Loading model...")
|
| 118 |
model = load_model(model_option)
|
| 119 |
|
| 120 |
progress = st.progress(0)
|
| 121 |
start_time = time.time()
|
| 122 |
|
| 123 |
-
with st.spinner("Transcribing..."):
|
| 124 |
-
result =
|
| 125 |
progress.progress(100)
|
| 126 |
|
| 127 |
end_time = time.time()
|
|
@@ -130,54 +130,52 @@ if uploaded_file:
|
|
| 130 |
|
| 131 |
detected_lang = result.get("language", "Unknown")
|
| 132 |
segments = result["segments"]
|
| 133 |
-
|
| 134 |
full_text = result["text"]
|
|
|
|
| 135 |
cleaned_text = clean_text(full_text)
|
| 136 |
|
| 137 |
if output_mode == "Roman Urdu":
|
| 138 |
cleaned_text = convert_to_roman_urdu(cleaned_text)
|
| 139 |
-
else:
|
| 140 |
-
cleaned_text = cleaned_text
|
| 141 |
|
| 142 |
word_count = len(cleaned_text.split())
|
| 143 |
processing_time = round(end_time - start_time, 2)
|
| 144 |
|
| 145 |
-
# ---------------------------
|
| 146 |
# DISPLAY RESULTS
|
| 147 |
-
# ---------------------------
|
| 148 |
col1, col2 = st.columns(2)
|
| 149 |
|
| 150 |
with col1:
|
| 151 |
st.subheader("📜 Raw Transcription")
|
| 152 |
-
st.text_area("", full_text, height=
|
| 153 |
|
| 154 |
with col2:
|
| 155 |
st.subheader("✨ Cleaned Output")
|
| 156 |
-
st.text_area("", cleaned_text, height=
|
| 157 |
|
| 158 |
st.divider()
|
| 159 |
|
| 160 |
st.write(f"**Detected Language:** {detected_lang}")
|
| 161 |
st.write(f"**Word Count:** {word_count}")
|
| 162 |
-
st.write(f"**Processing Time:** {processing_time}
|
| 163 |
|
| 164 |
-
# ---------------------------
|
| 165 |
# EXCEL DOWNLOAD
|
| 166 |
-
# ---------------------------
|
| 167 |
excel_file = create_excel(segments)
|
| 168 |
|
| 169 |
st.download_button(
|
| 170 |
label="📥 Download Excel File",
|
| 171 |
data=excel_file,
|
| 172 |
-
file_name="
|
| 173 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 174 |
)
|
| 175 |
|
| 176 |
-
# ---------------------------
|
| 177 |
# FOOTER
|
| 178 |
-
# ---------------------------
|
| 179 |
st.divider()
|
| 180 |
st.markdown(
|
| 181 |
-
"<p style='text-align:center; font-size:12px;'>Developed
|
| 182 |
unsafe_allow_html=True
|
| 183 |
)
|
|
|
|
| 9 |
from openpyxl.styles import Font
|
| 10 |
from io import BytesIO
|
| 11 |
|
| 12 |
+
# ---------------------------------------------------
|
| 13 |
# PAGE CONFIG
|
| 14 |
+
# ---------------------------------------------------
|
| 15 |
st.set_page_config(
|
| 16 |
page_title="RecToText Pro",
|
| 17 |
layout="wide",
|
| 18 |
page_icon="🎤"
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# ---------------------------------------------------
|
| 22 |
+
# SIDEBAR SETTINGS
|
| 23 |
+
# ---------------------------------------------------
|
| 24 |
st.sidebar.title("⚙️ Settings")
|
| 25 |
+
|
| 26 |
model_option = st.sidebar.selectbox(
|
| 27 |
"Select Whisper Model",
|
| 28 |
["base", "small"]
|
| 29 |
)
|
| 30 |
|
| 31 |
output_mode = st.sidebar.radio(
|
| 32 |
+
"Output Language Output",
|
| 33 |
["Roman Urdu", "English"]
|
| 34 |
)
|
| 35 |
|
| 36 |
if st.sidebar.button("🧹 Clear Session"):
|
| 37 |
st.session_state.clear()
|
| 38 |
+
st.rerun()
|
| 39 |
|
| 40 |
+
# ---------------------------------------------------
|
| 41 |
# HEADER
|
| 42 |
+
# ---------------------------------------------------
|
| 43 |
st.markdown("<h1 style='text-align:center;'>🎤 RecToText Pro</h1>", unsafe_allow_html=True)
|
| 44 |
+
st.markdown("<p style='text-align:center;'>AI-Powered Urdu + English Lecture Transcriber</p>", unsafe_allow_html=True)
|
| 45 |
st.divider()
|
| 46 |
|
| 47 |
+
# ---------------------------------------------------
|
| 48 |
# FUNCTIONS
|
| 49 |
+
# ---------------------------------------------------
|
| 50 |
|
| 51 |
@st.cache_resource
|
| 52 |
def load_model(model_size):
|
|
|
|
| 60 |
return text
|
| 61 |
|
| 62 |
def convert_to_roman_urdu(text):
|
|
|
|
| 63 |
replacements = {
|
| 64 |
"ہے": "hai",
|
| 65 |
"میں": "main",
|
| 66 |
"اور": "aur",
|
| 67 |
"کیا": "kya",
|
| 68 |
+
"آپ": "aap",
|
| 69 |
+
"کی": "ki",
|
| 70 |
+
"کا": "ka"
|
| 71 |
}
|
| 72 |
for urdu, roman in replacements.items():
|
| 73 |
text = text.replace(urdu, roman)
|
| 74 |
return text
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
def create_excel(segments):
|
| 77 |
wb = Workbook()
|
| 78 |
ws = wb.active
|
|
|
|
| 95 |
excel_buffer.seek(0)
|
| 96 |
return excel_buffer
|
| 97 |
|
| 98 |
+
# ---------------------------------------------------
|
| 99 |
+
# FILE UPLOADER (AAC SUPPORTED)
|
| 100 |
+
# ---------------------------------------------------
|
| 101 |
uploaded_file = st.file_uploader(
|
| 102 |
+
"Upload Lecture Recording (.mp3, .wav, .m4a, .aac)",
|
| 103 |
+
type=["mp3", "wav", "m4a", "aac"],
|
| 104 |
+
help="Supported formats: mp3, wav, m4a, aac"
|
| 105 |
)
|
| 106 |
|
| 107 |
if uploaded_file:
|
|
|
|
| 109 |
st.audio(uploaded_file)
|
| 110 |
|
| 111 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 112 |
+
file_extension = uploaded_file.name.split(".")[-1]
|
| 113 |
+
audio = AudioSegment.from_file(uploaded_file, format=file_extension)
|
| 114 |
audio.export(tmp.name, format="wav")
|
| 115 |
temp_audio_path = tmp.name
|
| 116 |
|
| 117 |
+
st.info("Loading Whisper model...")
|
| 118 |
model = load_model(model_option)
|
| 119 |
|
| 120 |
progress = st.progress(0)
|
| 121 |
start_time = time.time()
|
| 122 |
|
| 123 |
+
with st.spinner("Transcribing... Please wait."):
|
| 124 |
+
result = model.transcribe(temp_audio_path)
|
| 125 |
progress.progress(100)
|
| 126 |
|
| 127 |
end_time = time.time()
|
|
|
|
| 130 |
|
| 131 |
detected_lang = result.get("language", "Unknown")
|
| 132 |
segments = result["segments"]
|
|
|
|
| 133 |
full_text = result["text"]
|
| 134 |
+
|
| 135 |
cleaned_text = clean_text(full_text)
|
| 136 |
|
| 137 |
if output_mode == "Roman Urdu":
|
| 138 |
cleaned_text = convert_to_roman_urdu(cleaned_text)
|
|
|
|
|
|
|
| 139 |
|
| 140 |
word_count = len(cleaned_text.split())
|
| 141 |
processing_time = round(end_time - start_time, 2)
|
| 142 |
|
| 143 |
+
# ---------------------------------------------------
|
| 144 |
# DISPLAY RESULTS
|
| 145 |
+
# ---------------------------------------------------
|
| 146 |
col1, col2 = st.columns(2)
|
| 147 |
|
| 148 |
with col1:
|
| 149 |
st.subheader("📜 Raw Transcription")
|
| 150 |
+
st.text_area("", full_text, height=350)
|
| 151 |
|
| 152 |
with col2:
|
| 153 |
st.subheader("✨ Cleaned Output")
|
| 154 |
+
st.text_area("", cleaned_text, height=350)
|
| 155 |
|
| 156 |
st.divider()
|
| 157 |
|
| 158 |
st.write(f"**Detected Language:** {detected_lang}")
|
| 159 |
st.write(f"**Word Count:** {word_count}")
|
| 160 |
+
st.write(f"**Processing Time:** {processing_time} seconds")
|
| 161 |
|
| 162 |
+
# ---------------------------------------------------
|
| 163 |
# EXCEL DOWNLOAD
|
| 164 |
+
# ---------------------------------------------------
|
| 165 |
excel_file = create_excel(segments)
|
| 166 |
|
| 167 |
st.download_button(
|
| 168 |
label="📥 Download Excel File",
|
| 169 |
data=excel_file,
|
| 170 |
+
file_name="RecToText_Transcription.xlsx",
|
| 171 |
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
|
| 172 |
)
|
| 173 |
|
| 174 |
+
# ---------------------------------------------------
|
| 175 |
# FOOTER
|
| 176 |
+
# ---------------------------------------------------
|
| 177 |
st.divider()
|
| 178 |
st.markdown(
|
| 179 |
+
"<p style='text-align:center; font-size:12px;'>Developed using Whisper & Streamlit | RecToText Pro</p>",
|
| 180 |
unsafe_allow_html=True
|
| 181 |
)
|