import gdown import os import re from docx import Document from datetime import datetime def format_timestamp(seconds): if seconds is None: return "00:00" td = int(seconds) minutes = td // 60 secs = td % 60 return f"{minutes:02d}:{secs:02d}" def download_from_gdrive(url): try: if not os.path.exists("temp_downloads"): os.makedirs("temp_downloads") output_path = f"temp_downloads/drive_audio_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp3" return gdown.download(url, output_path, quiet=False, fuzzy=True) except Exception as e: print(f"Error: {e}") return None def clean_text(text): """ Uses Regex to find and remove repeating phrases like 'कर दो कर दो कर दो'. """ # Removes repetitions of 3 or more identical words text = re.sub(r'(\b\w+\b)( \1){2,}', r'\1', text) return text def save_as_docx(transcripts): doc = Document() doc.add_heading('Hindi Transcription Report (Cleaned)', 0) for entry in transcripts: doc.add_heading(f"File: {entry['filename']}", level=1) content = entry['text'] if isinstance(content, list): last_text = "" for chunk in content: text = clean_text(chunk['text']) # Skip if it's a hallucination (very high repetition in small chunk) if text == last_text or len(text) < 2: continue ts = chunk.get('timestamp', (0, 0)) p = doc.add_paragraph() p.add_run(f"[{format_timestamp(ts[0])}] ").bold = True p.add_run(text) last_text = text else: doc.add_paragraph(clean_text(content)) doc.add_page_break() report_name = f"Transcripts_Cleaned_{datetime.now().strftime('%Y%m%d_%H%M%S')}.docx" doc.save(report_name) return report_name