Update app.py
Browse files12345613 unmodified lines20212223242526272836 unmodified lines6566676869707172731 unmodified line7576777879805 unmodified lines868788899091924 unmodified lines97989910010110210335 unmodified lines139140141142143144145import ioimport torchfrom transformers import WhisperProcessor, WhisperForConditionalGenerationimport requestsfrom bs4 import BeautifulSoupimport tempfile13 unmodified linesprint(f"Using device: {device}")# Load the Whisper model and processormodel_name = "openai/whisper-small"processor = WhisperProcessor.from_pretrained(model_name)model = WhisperForConditionalGeneration.from_pretrained(model_name).to(device)def download_audio_from_url(url):try:36 unmodified linesaudio_array = audio.get_array_of_samples()print("Starting transcription...")input_features = processor(audio_array, sampling_rate=16000, return_tensors="pt").input_features.to(device)predicted_ids = model.generate(input_features)transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)print(f"Transcription complete. Length: {len(transcription[0])} characters")return transcription[0]1 unmodified lineprint(f"Error in transcribe_audio: {str(e)}")raisedef transcribe_video(url):try:print(f"Attempting to download audio from URL: {url}")5 unmodified linestranscript = transcribe_audio(temp_audio.name)os.unlink(temp_audio.name)return transcriptexcept Exception as e:error_message = f"An error occurred: {str(e)}"print(error_message)4 unmodified linesapp.layout = dbc.Container([dbc.Row([dbc.Col([html.H1("Video Transcription", className="text-center mb-4"),dbc.Card([dbc.CardBody([dbc.Input(id="video-url", type="text", placeholder="Enter video URL"),35 unmodified linesdownload_data = dict(content=transcript, filename="transcript.txt")return dbc.Card([dbc.CardBody([html.H5("Transcription Result"),html.Pre(transcript, style={"white-space": "pre-wrap", "word-wrap": "break-word"}),dbc.Button("Download Transcript", id="btn-download", color="secondary", className="mt-3")])12345613 unmodified lines202122232425262728293031323336 unmodified lines7071727374757677781 unmodified line80818283848586878889909192939495969798991001011021031041051061075 unmodified lines1131141151161171181191201211221234 unmodified lines12812913013113213313435 unmodified lines170171172173174175176import ioimport torchfrom transformers import WhisperProcessor, WhisperForConditionalGeneration, AutoTokenizer, AutoModelForCausalLMimport requestsfrom bs4 import BeautifulSoupimport tempfile13 unmodified linesprint(f"Using device: {device}")# Load the Whisper model and processorwhisper_model_name = "openai/whisper-small"whisper_processor = WhisperProcessor.from_pretrained(whisper_model_name)whisper_model = WhisperForConditionalGeneration.from_pretrained(whisper_model_name).to(device)# Load the Qwen model and tokenizerqwen_model_name = "Qwen/Qwen2.5-3B-Instruct"qwen_tokenizer = AutoTokenizer.from_pretrained(qwen_model_name)qwen_model = AutoModelForCausalLM.from_pretrained(qwen_model_name).to(device)def download_audio_from_url(url):try:36 unmodified linesaudio_array = audio.get_array_of_samples()print("Starting transcription...")input_features = whisper_processor(audio_array, sampling_rate=16000, return_tensors="pt").input_features.to(device)predicted_ids = whisper_model.generate(input_features)transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)print(f"Transcription complete. Length: {len(transcription[0])} characters")return transcription[0]1 unmodified lineprint(f"Error in transcribe_audio: {str(e)}")raisedef separate_speakers(transcription):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:{transcription}"""inputs = qwen_tokenizer(prompt, return_tensors="pt").to(device)outputs = qwen_model.generate(**inputs, max_new_tokens=1000)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()return processed_textdef transcribe_video(url):try:print(f"Attempting to download audio from URL: {url}")5 unmodified linestranscript = transcribe_audio(temp_audio.name)os.unlink(temp_audio.name)print("Separating speakers...")separated_transcript = separate_speakers(transcript)return separated_transcriptexcept Exception as e:error_message = f"An error occurred: {str(e)}"print(error_message)4 unmodified linesapp.layout = dbc.Container([dbc.Row([dbc.Col([html.H1("Video Transcription with Speaker Separation", className="text-center mb-4"),dbc.Card([dbc.CardBody([dbc.Input(id="video-url", type="text", placeholder="Enter video URL"),35 unmodified linesdownload_data = dict(content=transcript, filename="transcript.txt")return dbc.Card([dbc.CardBody([html.H5("Transcription Result with Speaker Separation"),html.Pre(transcript, style={"white-space": "pre-wrap", "word-wrap": "break-word"}),dbc.Button("Download Transcript", id="btn-download", color="secondary", className="mt-3")])
app.py+41-10