MiakOnline commited on
Commit
644ed40
·
verified ·
1 Parent(s): c2dad5a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +250 -0
app.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import whisper
3
+ import tempfile
4
+ import os
5
+ import time
6
+ import re
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
+ from collections import Counter
15
+
16
+ # ---------------------------------------------------
17
+ # PAGE CONFIG
18
+ # ---------------------------------------------------
19
+ st.set_page_config(
20
+ page_title="RecToText Pro - AI Edition",
21
+ layout="wide",
22
+ page_icon="🎤"
23
+ )
24
+
25
+ # ---------------------------------------------------
26
+ # SIDEBAR
27
+ # ---------------------------------------------------
28
+ st.sidebar.title("⚙️ Settings")
29
+
30
+ model_option = st.sidebar.selectbox(
31
+ "Select Whisper Model",
32
+ ["base", "small"]
33
+ )
34
+
35
+ output_mode = st.sidebar.radio(
36
+ "Output Format",
37
+ ["Roman Urdu", "English"]
38
+ )
39
+
40
+ if st.sidebar.button("🧹 Clear Session"):
41
+ st.session_state.clear()
42
+ st.rerun()
43
+
44
+ # ---------------------------------------------------
45
+ # HEADER
46
+ # ---------------------------------------------------
47
+ st.markdown("<h1 style='text-align:center;'>🎤 RecToText Pro - AI Enhanced</h1>", unsafe_allow_html=True)
48
+ st.markdown("<p style='text-align:center;'>Auto Title | AI Summary | Smart Formatting</p>", unsafe_allow_html=True)
49
+ st.divider()
50
+
51
+ # ---------------------------------------------------
52
+ # FUNCTIONS
53
+ # ---------------------------------------------------
54
+
55
+ @st.cache_resource
56
+ def load_model(model_size):
57
+ return whisper.load_model(model_size)
58
+
59
+ def clean_text(text):
60
+ filler_words = ["um", "hmm", "acha", "matlab", "uh", "huh"]
61
+ pattern = r'\b(?:' + '|'.join(filler_words) + r')\b'
62
+ text = re.sub(pattern, '', text, flags=re.IGNORECASE)
63
+ text = re.sub(r'\s+', ' ', text).strip()
64
+ return text
65
+
66
+ def convert_to_roman_urdu(text):
67
+ replacements = {
68
+ "ہے": "hai",
69
+ "میں": "main",
70
+ "اور": "aur",
71
+ "کیا": "kya",
72
+ "آپ": "aap",
73
+ "کی": "ki",
74
+ "کا": "ka"
75
+ }
76
+ for urdu, roman in replacements.items():
77
+ text = text.replace(urdu, roman)
78
+ return text
79
+
80
+ # -----------------------------
81
+ # AI Title Detection
82
+ # -----------------------------
83
+ def generate_title(text):
84
+ words = re.findall(r'\b[a-zA-Z]{4,}\b', text.lower())
85
+ common_words = Counter(words).most_common(5)
86
+ keywords = [word.capitalize() for word, _ in common_words[:3]]
87
+ if keywords:
88
+ return "Lecture on " + " ".join(keywords)
89
+ return "Lecture Transcription"
90
+
91
+ # -----------------------------
92
+ # AI Summary Generator
93
+ # -----------------------------
94
+ def generate_summary(text):
95
+ sentences = re.split(r'(?<=[.!?]) +', text)
96
+ summary = " ".join(sentences[:5])
97
+ return summary
98
+
99
+ # -----------------------------
100
+ # Smart Formatting
101
+ # -----------------------------
102
+ def smart_format(text):
103
+ sentences = re.split(r'(?<=[.!?]) +', text)
104
+ formatted = ""
105
+ for i, sentence in enumerate(sentences):
106
+ if len(sentence.split()) < 8:
107
+ formatted += f"\n\n{sentence.upper()}\n"
108
+ else:
109
+ formatted += sentence + " "
110
+ return formatted.strip()
111
+
112
+ # -----------------------------
113
+ # Excel Export
114
+ # -----------------------------
115
+ def create_excel(segments):
116
+ wb = Workbook()
117
+ ws = wb.active
118
+ ws.title = "Transcription"
119
+
120
+ headers = ["Timestamp", "Transcribed Text", "Cleaned Output"]
121
+ ws.append(headers)
122
+
123
+ for col in range(1, 4):
124
+ ws.cell(row=1, column=col).font = Font(bold=True)
125
+
126
+ for seg in segments:
127
+ timestamp = f"{round(seg['start'],2)} - {round(seg['end'],2)}"
128
+ raw_text = seg["text"]
129
+ cleaned = clean_text(raw_text)
130
+ ws.append([timestamp, raw_text, cleaned])
131
+
132
+ buffer = BytesIO()
133
+ wb.save(buffer)
134
+ buffer.seek(0)
135
+ return buffer
136
+
137
+ # -----------------------------
138
+ # Word Export
139
+ # -----------------------------
140
+ def create_word_document(title, summary, formatted_text):
141
+ doc = Document()
142
+
143
+ # Title
144
+ doc.add_heading(title, level=1).alignment = WD_ALIGN_PARAGRAPH.CENTER
145
+
146
+ doc.add_page_break()
147
+
148
+ # Summary Page
149
+ doc.add_heading("Executive Summary", level=2)
150
+ doc.add_paragraph(summary)
151
+
152
+ doc.add_page_break()
153
+
154
+ # Main Content
155
+ doc.add_heading("Full Lecture Content", level=2)
156
+
157
+ paragraphs = formatted_text.split("\n\n")
158
+ for para in paragraphs:
159
+ doc.add_paragraph(para).paragraph_format.space_after = Pt(12)
160
+
161
+ buffer = BytesIO()
162
+ doc.save(buffer)
163
+ buffer.seek(0)
164
+ return buffer
165
+
166
+ # ---------------------------------------------------
167
+ # FILE UPLOADER
168
+ # ---------------------------------------------------
169
+ uploaded_file = st.file_uploader(
170
+ "Upload Lecture Recording (.mp3, .wav, .m4a, .aac)",
171
+ type=["mp3", "wav", "m4a", "aac"]
172
+ )
173
+
174
+ if uploaded_file:
175
+
176
+ st.audio(uploaded_file)
177
+
178
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
179
+ ext = uploaded_file.name.split(".")[-1]
180
+ audio = AudioSegment.from_file(uploaded_file, format=ext)
181
+ audio.export(tmp.name, format="wav")
182
+ temp_audio_path = tmp.name
183
+
184
+ st.info("Loading Whisper model...")
185
+ model = load_model(model_option)
186
+
187
+ start_time = time.time()
188
+ with st.spinner("Transcribing..."):
189
+ result = model.transcribe(temp_audio_path)
190
+ end_time = time.time()
191
+
192
+ os.remove(temp_audio_path)
193
+
194
+ full_text = result["text"]
195
+ segments = result["segments"]
196
+ detected_lang = result.get("language", "Unknown")
197
+
198
+ cleaned_text = clean_text(full_text)
199
+
200
+ if output_mode == "Roman Urdu":
201
+ cleaned_text = convert_to_roman_urdu(cleaned_text)
202
+
203
+ title = generate_title(cleaned_text)
204
+ summary = generate_summary(cleaned_text)
205
+ formatted_text = smart_format(cleaned_text)
206
+
207
+ word_count = len(cleaned_text.split())
208
+ processing_time = round(end_time - start_time, 2)
209
+
210
+ col1, col2 = st.columns(2)
211
+
212
+ with col1:
213
+ st.subheader("📜 Raw Transcription")
214
+ st.text_area("", full_text, height=350)
215
+
216
+ with col2:
217
+ st.subheader("✨ AI Formatted Version")
218
+ st.text_area("", formatted_text, height=350)
219
+
220
+ st.divider()
221
+
222
+ st.write(f"**Auto Detected Title:** {title}")
223
+ st.write(f"**Detected Language:** {detected_lang}")
224
+ st.write(f"**Word Count:** {word_count}")
225
+ st.write(f"**Processing Time:** {processing_time} sec")
226
+
227
+ excel_file = create_excel(segments)
228
+ word_file = create_word_document(title, summary, formatted_text)
229
+
230
+ colA, colB = st.columns(2)
231
+
232
+ with colA:
233
+ st.download_button(
234
+ "📥 Download Excel (.xlsx)",
235
+ data=excel_file,
236
+ file_name="RecToText_Transcription.xlsx"
237
+ )
238
+
239
+ with colB:
240
+ st.download_button(
241
+ "📄 Download Word (.docx)",
242
+ data=word_file,
243
+ file_name="RecToText_AI_Lecture.docx"
244
+ )
245
+
246
+ st.divider()
247
+ st.markdown(
248
+ "<p style='text-align:center;font-size:12px;'>RecToText Pro AI Edition | Auto Title | Smart Summary | AI Formatting</p>",
249
+ unsafe_allow_html=True
250
+ )