Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,22 +13,28 @@ from io import BytesIO
|
|
| 13 |
|
| 14 |
st.set_page_config(page_title="RecToText Pro", layout="wide")
|
| 15 |
|
| 16 |
-
st.title("🎤 RecToText Pro –
|
| 17 |
-
st.caption("
|
| 18 |
-
|
| 19 |
-
# --------------------------------------------------
|
| 20 |
-
#
|
| 21 |
-
# --------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
@st.cache_resource
|
| 23 |
def load_model():
|
| 24 |
return WhisperModel("base", device="cpu", compute_type="int8")
|
| 25 |
|
| 26 |
model = load_model()
|
| 27 |
|
| 28 |
-
# --------------------------------------------------
|
| 29 |
-
# STRICT ROMAN URDU
|
| 30 |
-
# --------------------------------------------------
|
| 31 |
-
def
|
| 32 |
replacements = {
|
| 33 |
"ہے": "hai",
|
| 34 |
"میں": "main",
|
|
@@ -41,30 +47,28 @@ def transliterate_to_roman(text):
|
|
| 41 |
"پر": "par",
|
| 42 |
"نہیں": "nahin"
|
| 43 |
}
|
| 44 |
-
for
|
| 45 |
-
text = text.replace(
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
text = re.sub(r'[^\x00-\x7F]+', '', text)
|
| 49 |
-
return text
|
| 50 |
-
|
| 51 |
-
# -------------------------------------------------------
|
| 52 |
-
# CLEAN + STRUCTURE STORY
|
| 53 |
-
# -------------------------------------------------------
|
| 54 |
-
def clean_and_structure(text):
|
| 55 |
-
filler = ["um", "hmm", "acha", "matlab", "uh"]
|
| 56 |
-
pattern = r'\b(?:' + '|'.join(filler) + r')\b'
|
| 57 |
-
text = re.sub(pattern, "", text, flags=re.IGNORECASE)
|
| 58 |
-
text = re.sub(r'\s+', ' ', text).strip()
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
sentences = re.split(r'(?<=[.!?]) +', text)
|
| 61 |
|
| 62 |
paragraphs = []
|
| 63 |
temp = ""
|
| 64 |
|
| 65 |
for i, sentence in enumerate(sentences):
|
|
|
|
|
|
|
|
|
|
| 66 |
temp += sentence + " "
|
| 67 |
-
|
|
|
|
| 68 |
paragraphs.append(temp.strip())
|
| 69 |
temp = ""
|
| 70 |
|
|
@@ -73,27 +77,23 @@ def clean_and_structure(text):
|
|
| 73 |
|
| 74 |
return "\n\n".join(paragraphs)
|
| 75 |
|
| 76 |
-
# --------------------------------------------------
|
| 77 |
-
# AUDIO CHUNKING
|
| 78 |
-
# --------------------------------------------------
|
| 79 |
-
def chunk_audio(
|
| 80 |
-
audio = AudioSegment.from_wav(
|
| 81 |
-
chunk_length = 30 * 1000
|
| 82 |
chunks = []
|
| 83 |
-
|
| 84 |
-
for i in range(0, len(audio),
|
| 85 |
-
chunks.append(audio[i:i +
|
| 86 |
-
|
| 87 |
return chunks
|
| 88 |
|
| 89 |
-
# --------------------------------------------------
|
| 90 |
# EXPORT EXCEL
|
| 91 |
-
# --------------------------------------------------
|
| 92 |
def export_excel(text):
|
| 93 |
wb = Workbook()
|
| 94 |
ws = wb.active
|
| 95 |
-
ws.title = "Transcription"
|
| 96 |
-
|
| 97 |
ws.append(["Lecture Transcription"])
|
| 98 |
ws["A1"].font = Font(bold=True)
|
| 99 |
ws.append([text])
|
|
@@ -103,12 +103,12 @@ def export_excel(text):
|
|
| 103 |
buffer.seek(0)
|
| 104 |
return buffer
|
| 105 |
|
| 106 |
-
# --------------------------------------------------
|
| 107 |
# EXPORT WORD
|
| 108 |
-
# --------------------------------------------------
|
| 109 |
-
def export_word(
|
| 110 |
doc = Document()
|
| 111 |
-
doc.add_heading(
|
| 112 |
|
| 113 |
paragraphs = text.split("\n\n")
|
| 114 |
for para in paragraphs:
|
|
@@ -120,11 +120,18 @@ def export_word(title, text):
|
|
| 120 |
buffer.seek(0)
|
| 121 |
return buffer
|
| 122 |
|
| 123 |
-
# --------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# FILE UPLOADER
|
| 125 |
-
# --------------------------------------------------
|
| 126 |
uploaded = st.file_uploader(
|
| 127 |
-
"Upload Lecture
|
| 128 |
type=["mp3", "wav", "m4a", "aac"]
|
| 129 |
)
|
| 130 |
|
|
@@ -134,7 +141,6 @@ if uploaded:
|
|
| 134 |
try:
|
| 135 |
st.audio(uploaded)
|
| 136 |
|
| 137 |
-
# Convert to WAV
|
| 138 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 139 |
ext = uploaded.name.split(".")[-1]
|
| 140 |
audio = AudioSegment.from_file(uploaded, format=ext)
|
|
@@ -142,7 +148,6 @@ if uploaded:
|
|
| 142 |
temp_path = tmp.name
|
| 143 |
|
| 144 |
start_time = time.time()
|
| 145 |
-
|
| 146 |
chunks = chunk_audio(temp_path)
|
| 147 |
full_text = ""
|
| 148 |
|
|
@@ -158,47 +163,43 @@ if uploaded:
|
|
| 158 |
|
| 159 |
os.remove(temp_path)
|
| 160 |
|
|
|
|
| 161 |
if output_mode == "Roman Urdu":
|
| 162 |
-
full_text =
|
| 163 |
else:
|
| 164 |
full_text = re.sub(r'[^\x00-\x7F]+', '', full_text)
|
| 165 |
|
| 166 |
-
|
| 167 |
|
| 168 |
-
|
| 169 |
-
processing_time = round(time.time() - start_time, 2)
|
| 170 |
-
|
| 171 |
-
col1, col2 = st.columns(2)
|
| 172 |
-
|
| 173 |
-
with col1:
|
| 174 |
-
st.subheader("Raw Transcription")
|
| 175 |
-
st.text_area("", full_text, height=300)
|
| 176 |
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
st.text_area("", structured_text, height=300)
|
| 180 |
|
| 181 |
-
st.
|
|
|
|
| 182 |
|
| 183 |
st.write(f"Word Count: {word_count}")
|
| 184 |
st.write(f"Processing Time: {processing_time} sec")
|
| 185 |
|
| 186 |
-
excel_file = export_excel(
|
| 187 |
-
word_file = export_word(
|
| 188 |
|
| 189 |
-
|
| 190 |
|
| 191 |
-
with
|
| 192 |
-
st.download_button("Download Excel (.xlsx)", excel_file
|
|
|
|
| 193 |
|
| 194 |
-
with
|
| 195 |
-
st.download_button("Download Word (.docx)", word_file
|
|
|
|
| 196 |
|
| 197 |
-
st.success("
|
| 198 |
|
| 199 |
except Exception as e:
|
| 200 |
st.error("Processing Error")
|
| 201 |
st.exception(e)
|
| 202 |
|
| 203 |
st.markdown("---")
|
| 204 |
-
st.markdown("<center>
|
|
|
|
| 13 |
|
| 14 |
st.set_page_config(page_title="RecToText Pro", layout="wide")
|
| 15 |
|
| 16 |
+
st.title("🎤 RecToText Pro – AI Polished Edition")
|
| 17 |
+
st.caption("Professional Lecture Transcriber | Clean Story | Grammar Polished")
|
| 18 |
+
|
| 19 |
+
# --------------------------------------------------
|
| 20 |
+
# SESSION STATE
|
| 21 |
+
# --------------------------------------------------
|
| 22 |
+
if "processed_text" not in st.session_state:
|
| 23 |
+
st.session_state.processed_text = None
|
| 24 |
+
|
| 25 |
+
# --------------------------------------------------
|
| 26 |
+
# LOAD MODEL
|
| 27 |
+
# --------------------------------------------------
|
| 28 |
@st.cache_resource
|
| 29 |
def load_model():
|
| 30 |
return WhisperModel("base", device="cpu", compute_type="int8")
|
| 31 |
|
| 32 |
model = load_model()
|
| 33 |
|
| 34 |
+
# --------------------------------------------------
|
| 35 |
+
# STRICT ROMAN URDU
|
| 36 |
+
# --------------------------------------------------
|
| 37 |
+
def transliterate(text):
|
| 38 |
replacements = {
|
| 39 |
"ہے": "hai",
|
| 40 |
"میں": "main",
|
|
|
|
| 47 |
"پر": "par",
|
| 48 |
"نہیں": "nahin"
|
| 49 |
}
|
| 50 |
+
for k, v in replacements.items():
|
| 51 |
+
text = text.replace(k, v)
|
| 52 |
+
|
| 53 |
+
return re.sub(r'[^\x00-\x7F]+', '', text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
# --------------------------------------------------
|
| 56 |
+
# AI STYLE POLISHING (RULE BASED SAFE)
|
| 57 |
+
# --------------------------------------------------
|
| 58 |
+
def polish_text(text):
|
| 59 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 60 |
sentences = re.split(r'(?<=[.!?]) +', text)
|
| 61 |
|
| 62 |
paragraphs = []
|
| 63 |
temp = ""
|
| 64 |
|
| 65 |
for i, sentence in enumerate(sentences):
|
| 66 |
+
sentence = sentence.strip().capitalize()
|
| 67 |
+
if not sentence.endswith((".", "!", "?")):
|
| 68 |
+
sentence += "."
|
| 69 |
temp += sentence + " "
|
| 70 |
+
|
| 71 |
+
if (i + 1) % 4 == 0:
|
| 72 |
paragraphs.append(temp.strip())
|
| 73 |
temp = ""
|
| 74 |
|
|
|
|
| 77 |
|
| 78 |
return "\n\n".join(paragraphs)
|
| 79 |
|
| 80 |
+
# --------------------------------------------------
|
| 81 |
+
# AUDIO CHUNKING
|
| 82 |
+
# --------------------------------------------------
|
| 83 |
+
def chunk_audio(path):
|
| 84 |
+
audio = AudioSegment.from_wav(path)
|
|
|
|
| 85 |
chunks = []
|
| 86 |
+
chunk_len = 30 * 1000
|
| 87 |
+
for i in range(0, len(audio), chunk_len):
|
| 88 |
+
chunks.append(audio[i:i + chunk_len])
|
|
|
|
| 89 |
return chunks
|
| 90 |
|
| 91 |
+
# --------------------------------------------------
|
| 92 |
# EXPORT EXCEL
|
| 93 |
+
# --------------------------------------------------
|
| 94 |
def export_excel(text):
|
| 95 |
wb = Workbook()
|
| 96 |
ws = wb.active
|
|
|
|
|
|
|
| 97 |
ws.append(["Lecture Transcription"])
|
| 98 |
ws["A1"].font = Font(bold=True)
|
| 99 |
ws.append([text])
|
|
|
|
| 103 |
buffer.seek(0)
|
| 104 |
return buffer
|
| 105 |
|
| 106 |
+
# --------------------------------------------------
|
| 107 |
# EXPORT WORD
|
| 108 |
+
# --------------------------------------------------
|
| 109 |
+
def export_word(text):
|
| 110 |
doc = Document()
|
| 111 |
+
doc.add_heading("Lecture Transcription", level=1)
|
| 112 |
|
| 113 |
paragraphs = text.split("\n\n")
|
| 114 |
for para in paragraphs:
|
|
|
|
| 120 |
buffer.seek(0)
|
| 121 |
return buffer
|
| 122 |
|
| 123 |
+
# --------------------------------------------------
|
| 124 |
+
# CLEAR BUTTON
|
| 125 |
+
# --------------------------------------------------
|
| 126 |
+
if st.sidebar.button("🧹 Clear All"):
|
| 127 |
+
st.session_state.processed_text = None
|
| 128 |
+
st.rerun()
|
| 129 |
+
|
| 130 |
+
# --------------------------------------------------
|
| 131 |
# FILE UPLOADER
|
| 132 |
+
# --------------------------------------------------
|
| 133 |
uploaded = st.file_uploader(
|
| 134 |
+
"Upload Lecture (MP3, WAV, M4A, AAC) – Max 200MB",
|
| 135 |
type=["mp3", "wav", "m4a", "aac"]
|
| 136 |
)
|
| 137 |
|
|
|
|
| 141 |
try:
|
| 142 |
st.audio(uploaded)
|
| 143 |
|
|
|
|
| 144 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp:
|
| 145 |
ext = uploaded.name.split(".")[-1]
|
| 146 |
audio = AudioSegment.from_file(uploaded, format=ext)
|
|
|
|
| 148 |
temp_path = tmp.name
|
| 149 |
|
| 150 |
start_time = time.time()
|
|
|
|
| 151 |
chunks = chunk_audio(temp_path)
|
| 152 |
full_text = ""
|
| 153 |
|
|
|
|
| 163 |
|
| 164 |
os.remove(temp_path)
|
| 165 |
|
| 166 |
+
# Strict output control
|
| 167 |
if output_mode == "Roman Urdu":
|
| 168 |
+
full_text = transliterate(full_text)
|
| 169 |
else:
|
| 170 |
full_text = re.sub(r'[^\x00-\x7F]+', '', full_text)
|
| 171 |
|
| 172 |
+
polished = polish_text(full_text)
|
| 173 |
|
| 174 |
+
st.session_state.processed_text = polished
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
word_count = len(polished.split())
|
| 177 |
+
processing_time = round(time.time() - start_time, 2)
|
|
|
|
| 178 |
|
| 179 |
+
st.subheader("✨ Clean AI Polished Story")
|
| 180 |
+
st.text_area("", polished, height=350)
|
| 181 |
|
| 182 |
st.write(f"Word Count: {word_count}")
|
| 183 |
st.write(f"Processing Time: {processing_time} sec")
|
| 184 |
|
| 185 |
+
excel_file = export_excel(polished)
|
| 186 |
+
word_file = export_word(polished)
|
| 187 |
|
| 188 |
+
col1, col2 = st.columns(2)
|
| 189 |
|
| 190 |
+
with col1:
|
| 191 |
+
if st.download_button("Download Excel (.xlsx)", excel_file):
|
| 192 |
+
st.session_state.processed_text = None
|
| 193 |
|
| 194 |
+
with col2:
|
| 195 |
+
if st.download_button("Download Word (.docx)", word_file):
|
| 196 |
+
st.session_state.processed_text = None
|
| 197 |
|
| 198 |
+
st.success("Story Generated Successfully.")
|
| 199 |
|
| 200 |
except Exception as e:
|
| 201 |
st.error("Processing Error")
|
| 202 |
st.exception(e)
|
| 203 |
|
| 204 |
st.markdown("---")
|
| 205 |
+
st.markdown("<center>RecToText Pro – AI Polished Edition</center>", unsafe_allow_html=True)
|