Update app.py
Browse files26 unmodified lines2728293031323352 unmodified lines8687888990913 unmodified lines95969798991001014 unmodified lines1061071081091101111123 unmodified lines1161171181191201211226 unmodified lines12913013113213313426 unmodified lines# Load the Qwen model and tokenizerqwen_model_name = "Qwen/Qwen2.5-3B-Instruct"qwen_tokenizer = AutoTokenizer.from_pretrained(qwen_model_name, trust_remote_code=True)qwen_model = AutoModelForCausalLM.from_pretrained(qwen_model_name, trust_remote_code=True, torch_dtype=torch.float16).to(device)def download_audio_from_url(url):try:52 unmodified linestranscription = whisper_processor.batch_decode(predicted_ids, skip_special_tokens=True)print(f"Transcription complete. Length: {len(transcription[0])} characters")return transcription[0]except Exception as e:print(f"Error in transcribe_audio: {str(e)}")3 unmodified linesprint("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. You will have to use dialog context to asume which speaker is saying their dialog as that isn't in the text.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.4 unmodified lines"""inputs = qwen_tokenizer(prompt, return_tensors="pt").to(device)inputs = {k: v.to(torch.float16) for k, v in inputs.items()} # Convert inputs to float16with torch.no_grad():outputs = qwen_model.generate(**inputs, max_new_tokens=4000)result = qwen_tokenizer.decode(outputs[0], skip_special_tokens=True)3 unmodified linesprint("Speaker separation complete.")return processed_textdef transcribe_video(url):try:print(f"Attempting to download audio from URL: {url}")6 unmodified linesos.unlink(temp_audio.name)print("Separating speakers...")separated_transcript = separate_speakers(transcript)26 unmodified lines2728293031323352 unmodified lines86878889909192933 unmodified lines9798991001011021034 unmodified lines1081091101111121133 unmodified lines1171181191201211221236 unmodified lines13013113213313413513613713826 unmodified lines# Load the Qwen model and tokenizerqwen_model_name = "Qwen/Qwen2.5-3B-Instruct"qwen_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)def download_audio_from_url(url):try:52 unmodified linestranscription = 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)}")3 unmodified linesprint("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.4 unmodified lines"""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)3 unmodified linesprint("Speaker separation complete.")return processed_textdef transcribe_video(url):try:print(f"Attempting to download audio from URL: {url}")6 unmodified linesos.unlink(temp_audio.name)if len(transcript) < 10:raise ValueError("Transcription too short, possibly failed")print("Separating speakers...")separated_transcript = separate_speakers(transcript)
app.py+8-4