Update app.py
Browse files28 unmodified lines29303132323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514628 unmodified linesqwen_tokenizer = AutoTokenizer.from_pretrained(qwen_model_name, trust_remote_code=True)qwen_model = AutoModelForCausalLM.from_pretrained(qwen_model_name, trust_remote_code=True).to(device)
# ... (keep all the existing functions as they are)def download_audio_from_url(url): try: if "youtube.com" in url or "youtu.be" in url: print("Processing YouTube URL...") yt = YouTube(url) audio_stream = yt.streams.filter(only_audio=True).first() with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_file: audio_stream.download(output_path=temp_file.name) audio_bytes = open(temp_file.name, "rb").read() os.unlink(temp_file.name) elif "share" in url: print("Processing shareable link...") response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') video_tag = soup.find('video') if video_tag and 'src' in video_tag.attrs: video_url = video_tag['src'] print(f"Extracted video URL: {video_url}") else: raise ValueError("Direct video URL not found in the shareable link.") response = requests.get(video_url) audio_bytes = response.content else: print(f"Downloading video from URL: {url}") response = requests.get(url) audio_bytes = response.content print(f"Successfully downloaded {len(audio_bytes)} bytes of data") return audio_bytes except Exception as e: print(f"Error in download_audio_from_url: {str(e)}") raise
def transcribe_audio(audio_file): try: print("Loading audio file...") audio = AudioSegment.from_file(audio_file) audio = audio.set_channels(1).set_frame_rate(16000) audio_array = torch.tensor(audio.get_array_of_samples()).float() print(f"Audio duration: {len(audio) / 1000:.2f} seconds") print("Starting transcription...") input_features = whisper_processor(audio_array, sampling_rate=16000, return_tensors="pt").input_features.to(device) # Create attention mask attention_mask = torch.ones_like(input_features) # Generate with specific parameters predicted_ids = whisper_model.generate( input_features, attention_mask=attention_mask, language='en', task='translate' ) transcription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True) print(f"Transcription complete. Length: {len(transcription[0])} characters") if len(transcription[0]) < 10: raise ValueError(f"Transcription too short: {transcription[0]}") return transcription[0] except Exception as e: print(f"Error in transcribe_audio: {str(e)}") raise
def 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:
{transcription}""" inputs = qwen_tokenizer(prompt, return_tensors="pt").to(device) with torch.no_grad(): outputs = 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_text
def transcribe_video(url): try: print(f"Attempting to download audio from URL: {url}") audio_bytes = download_audio_from_url(url) print(f"Successfully downloaded {len(audio_bytes)} bytes of audio data") with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_audio: AudioSegment.from_file(io.BytesIO(audio_bytes)).export(temp_audio.name, format="wav") transcript = transcribe_audio(temp_audio.name) os.unlink(temp_audio.name) if len(transcript) < 10: raise ValueError("Transcription too short, possibly failed") print("Separating speakers...") separated_transcript = separate_speakers(transcript) return separated_transcript except Exception as e: error_message = f"An error occurred: {str(e)}" print(error_message) return error_message
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
app.py+112-1