Update app.py
Browse files100 unmodified lines1011021031041051061071081091101111121135 unmodified lines11912012112212312412512612712813 unmodified lines14214314414514614714814915015115215348 unmodified lines202203204205206207208209210211212213214215216217218219220221222223100 unmodified linesraisedef separate_speakers(transcription):print("Starting speaker separation...")prompt = f"""Analyze the following transcribed text and separate it into different speakers. Identify potential speaker changes based on context, content shifts, or dialogue patterns. Format the output as follows:1. Label speakers as "Speaker 1", "Speaker 2", etc.2. Start each speaker's text on a new line beginning with their label.3. Separate different speakers' contributions with a blank line.4. If the same speaker continues, do not insert a blank line or repeat the speaker label.Now, please process the following transcribed text:5 unmodified linesoutputs = qwen_model.generate(**inputs, max_new_tokens=4000)result = qwen_tokenizer.decode(outputs[0], skip_special_tokens=True)# Extract the processed text (remove the instruction part)processed_text = result.split("Now, please process the following transcribed text:")[-1].strip()print("Speaker separation complete.")return processed_textdef transcribe_video(url):13 unmodified lineslogger.info("Separating speakers...")try:separated_transcript = separate_speakers(transcript)logger.info(f"Speaker separation complete. Result length: {len(separated_transcript)} characters")if len(separated_transcript) < 10:logger.warning("Speaker separation result too short, using original transcript")return transcriptreturn separated_transcriptexcept Exception as e:logger.error(f"Error during speaker separation: {str(e)}")logger.info("Returning original transcript without speaker separation")48 unmodified lineselse:return transcript, {'display': 'none'}if transcript and not transcript.startswith("An error occurred"):return dbc.Card([dbc.CardBody([html.H5("Transcription Result with Speaker Separation"),html.Pre(transcript, style={"white-space": "pre-wrap", "word-wrap": "break-word"})])]), {'display': 'block'}else:return transcript, {'display': 'none'}@app.callback(Output("download-transcript", "data"),Input("download-button", "n_clicks"),State("transcription-output", "children"),prevent_initial_call=True)def download_transcript(n_clicks, transcription_output):if not transcription_output:raise PreventUpdate100 unmodified lines1011021031041051061071081091101111121131145 unmodified lines12012112212312412512612712812913 unmodified lines14314414514614714814915015115215315448 unmodified lines203204205206207208209210211212213214215100 unmodified linesraisedef separate_speakers(transcription):logger.info("Starting speaker separation...")prompt = f"""Analyze the following transcribed text and separate it into different speakers. Identify potential speaker changes based on context, content shifts, or dialogue patterns. Format the output as follows:1. Label speakers as "Speaker 1", "Speaker 2", etc.2. Start each speaker's text on a new line beginning with their label.3. Separate different speakers' contributions with a blank line.4. If the same speaker continues, do not insert a blank line or repeat the speaker label.5. Do not include any additional explanations or metadata.Now, please process the following transcribed text:5 unmodified linesoutputs = qwen_model.generate(**inputs, max_new_tokens=4000)result = qwen_tokenizer.decode(outputs[0], skip_special_tokens=True)# Extract only the processed text (remove the instruction part)processed_text = result.split("Now, please process the following transcribed text:")[-1].strip()logger.info("Speaker separation complete.")return processed_textdef transcribe_video(url):13 unmodified lineslogger.info("Separating speakers...")try:diarized_transcript = separate_speakers(transcript)logger.info(f"Speaker separation complete. Result length: {len(diarized_transcript)} characters")if len(diarized_transcript) < 10:logger.warning("Speaker separation result too short, using original transcript")return transcriptreturn diarized_transcriptexcept Exception as e:logger.error(f"Error during speaker separation: {str(e)}")logger.info("Returning original transcript without speaker separation")48 unmodified lineselse:return transcript, {'display': 'none'}@app.callback(Output("download-transcript", "data"),Input("download-button", "n_clicks"),State("transcription-output", "children"),prevent_initial_call=True)def download_transcript(n_clicks, transcription_output):if not transcription_output:raise PreventUpdate
app.py+9-17