MiakOnline commited on
Commit
b98298a
·
verified ·
1 Parent(s): e74bd99

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -27
app.py CHANGED
@@ -7,6 +7,9 @@ import re
7
  from pydub import AudioSegment
8
  from openpyxl import Workbook
9
  from openpyxl.styles import Font
 
 
 
10
  from io import BytesIO
11
 
12
  # ---------------------------------------------------
@@ -29,7 +32,7 @@ model_option = st.sidebar.selectbox(
29
  )
30
 
31
  output_mode = st.sidebar.radio(
32
- "Output Language Output",
33
  ["Roman Urdu", "English"]
34
  )
35
 
@@ -41,7 +44,7 @@ if st.sidebar.button("🧹 Clear Session"):
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
  # ---------------------------------------------------
@@ -57,7 +60,22 @@ def clean_text(text):
57
  pattern = r'\b(?:' + '|'.join(filler_words) + r')\b'
58
  text = re.sub(pattern, '', text, flags=re.IGNORECASE)
59
  text = re.sub(r'\s+', ' ', text).strip()
60
- return text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
 
62
  def convert_to_roman_urdu(text):
63
  replacements = {
@@ -95,13 +113,31 @@ def create_excel(segments):
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:
@@ -117,15 +153,12 @@ if uploaded_file:
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()
128
-
129
  os.remove(temp_audio_path)
130
 
131
  detected_lang = result.get("language", "Unknown")
@@ -140,9 +173,6 @@ if uploaded_file:
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:
@@ -150,7 +180,7 @@ if uploaded_file:
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()
@@ -159,23 +189,32 @@ if uploaded_file:
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
  )
 
7
  from pydub import AudioSegment
8
  from openpyxl import Workbook
9
  from openpyxl.styles import Font
10
+ from docx import Document
11
+ from docx.shared import Pt
12
+ from docx.enum.text import WD_ALIGN_PARAGRAPH
13
  from io import BytesIO
14
 
15
  # ---------------------------------------------------
 
32
  )
33
 
34
  output_mode = st.sidebar.radio(
35
+ "Output Format",
36
  ["Roman Urdu", "English"]
37
  )
38
 
 
44
  # HEADER
45
  # ---------------------------------------------------
46
  st.markdown("<h1 style='text-align:center;'>🎤 RecToText Pro</h1>", unsafe_allow_html=True)
47
+ st.markdown("<p style='text-align:center;'>AI Lecture Transcriber with Excel & Word Export</p>", unsafe_allow_html=True)
48
  st.divider()
49
 
50
  # ---------------------------------------------------
 
60
  pattern = r'\b(?:' + '|'.join(filler_words) + r')\b'
61
  text = re.sub(pattern, '', text, flags=re.IGNORECASE)
62
  text = re.sub(r'\s+', ' ', text).strip()
63
+
64
+ # Better paragraph formatting
65
+ sentences = re.split(r'(?<=[.!?]) +', text)
66
+ paragraphs = []
67
+ temp_para = ""
68
+
69
+ for i, sentence in enumerate(sentences):
70
+ temp_para += sentence + " "
71
+ if (i + 1) % 5 == 0:
72
+ paragraphs.append(temp_para.strip())
73
+ temp_para = ""
74
+
75
+ if temp_para:
76
+ paragraphs.append(temp_para.strip())
77
+
78
+ return "\n\n".join(paragraphs)
79
 
80
  def convert_to_roman_urdu(text):
81
  replacements = {
 
113
  excel_buffer.seek(0)
114
  return excel_buffer
115
 
116
+ def create_word_document(cleaned_text):
117
+ doc = Document()
118
+
119
+ title = doc.add_heading("Lecture Transcription", level=1)
120
+ title.alignment = WD_ALIGN_PARAGRAPH.CENTER
121
+
122
+ doc.add_paragraph("")
123
+
124
+ paragraphs = cleaned_text.split("\n\n")
125
+
126
+ for para in paragraphs:
127
+ p = doc.add_paragraph(para)
128
+ p.paragraph_format.space_after = Pt(12)
129
+
130
+ word_buffer = BytesIO()
131
+ doc.save(word_buffer)
132
+ word_buffer.seek(0)
133
+ return word_buffer
134
+
135
  # ---------------------------------------------------
136
+ # FILE UPLOADER
137
  # ---------------------------------------------------
138
  uploaded_file = st.file_uploader(
139
  "Upload Lecture Recording (.mp3, .wav, .m4a, .aac)",
140
+ type=["mp3", "wav", "m4a", "aac"]
 
141
  )
142
 
143
  if uploaded_file:
 
153
  st.info("Loading Whisper model...")
154
  model = load_model(model_option)
155
 
 
156
  start_time = time.time()
157
 
158
  with st.spinner("Transcribing... Please wait."):
159
  result = model.transcribe(temp_audio_path)
 
160
 
161
  end_time = time.time()
 
162
  os.remove(temp_audio_path)
163
 
164
  detected_lang = result.get("language", "Unknown")
 
173
  word_count = len(cleaned_text.split())
174
  processing_time = round(end_time - start_time, 2)
175
 
 
 
 
176
  col1, col2 = st.columns(2)
177
 
178
  with col1:
 
180
  st.text_area("", full_text, height=350)
181
 
182
  with col2:
183
+ st.subheader("✨ Clean Story Format")
184
  st.text_area("", cleaned_text, height=350)
185
 
186
  st.divider()
 
189
  st.write(f"**Word Count:** {word_count}")
190
  st.write(f"**Processing Time:** {processing_time} seconds")
191
 
192
+ # ----------------------------
193
+ # EXPORT FILES
194
+ # ----------------------------
195
  excel_file = create_excel(segments)
196
+ word_file = create_word_document(cleaned_text)
197
+
198
+ colA, colB = st.columns(2)
199
+
200
+ with colA:
201
+ st.download_button(
202
+ label="📥 Download Excel (.xlsx)",
203
+ data=excel_file,
204
+ file_name="RecToText_Transcription.xlsx",
205
+ mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
206
+ )
207
+
208
+ with colB:
209
+ st.download_button(
210
+ label="📄 Download Word (.docx)",
211
+ data=word_file,
212
+ file_name="RecToText_Lecture.docx",
213
+ mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
214
+ )
215
 
 
 
 
 
 
 
 
 
 
 
216
  st.divider()
217
  st.markdown(
218
+ "<p style='text-align:center; font-size:12px;'>RecToText Pro | Whisper + Streamlit Powered</p>",
219
  unsafe_allow_html=True
220
  )