Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234564 unmodified lines111213141516171819202122232425262728291 unmodified line3132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import torchfrom transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipelinefrom transformers import pipelinefrom datasets import load_datasetimport spacyimport gradio as gr4 unmodified lines# Whisper 模型初始化(語音轉文字)whisper_model_id = "openai/whisper-large-v3"whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)whisper_model.to(device)whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)whisper_pipe = pipeline("automatic-speech-recognition",model=whisper_model,tokenizer=whisper_processor.tokenizer,feature_extractor=whisper_processor.feature_extractor,device=device,)# DeepSeek-V3 模型初始化(文本生成)deepseek_pipe = None # 預設值,以防模型加載失敗1 unmodified linetry:deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V3", trust_remote_code=True)except Exception as e:print(f"加載模型時出現錯誤:{e}")# spaCy 初始化(文本分類與標籤)nlp = spacy.load("en_core_web_sm")def process_audio(audio_file):# 語音轉文字result = whisper_pipe(audio_file)["text"]# 使用 DeepSeek 生成回應(如果成功加載模型)if deepseek_pipe is not None:messages = [{"role": "user", "content": result}]deepseek_response = deepseek_pipe(messages)[0]["generated_text"]# 使用 spaCy 分析文本doc = nlp(deepseek_response)entities = [(ent.text, ent.label_) for ent in doc.ents]return {"Transcription (Whisper)": result,"AI Response (DeepSeek)": deepseek_response,"Extracted Entities (spaCy)": entities,}with gr.Blocks() as app:with gr.Row():audio_input=gr.Audio(source="microphone", type="filepath", label="上傳語音")output_text=gr.JSON(label="結果")submit_button=gr.Button("提交") # 修正了這一行submit_button.click(fn=lambda x: process_audio(x), inputs=[audio_input], outputs=[output_text])if __name__ == "__main__":123454 unmodified lines10111213141516171819202122232425262728293031323334353637381 unmodified line404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103import torchfrom transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipelinefrom datasets import load_datasetimport spacyimport gradio as gr4 unmodified lines# Whisper 模型初始化(語音轉文字)whisper_model_id = "openai/whisper-large-v3"try:whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True,revision="main", # 嘗試指定修訂版以解決兼容性問題)whisper_model.to(device)# 加載處理器時也指定修訂版(如果需要)whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)except Exception as e:print(f"加載Whisper模型或處理器時出現錯誤:{e}")else:# 成功加載後建立pipeline進行語音轉文字工作whisper_pipe = pipeline("automatic-speech-recognition",model=whisper_model,tokenizer=whisper_processor.tokenizer,feature_extractor=whisper_processor.feature_extractor,device=device,)# DeepSeek-V3 模型初始化(文本生成)deepseek_pipe = None # 預設值,以防模型加載失敗1 unmodified linetry:deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V3", trust_remote_code=True)except Exception as e:print(f"加載DeepSeek-V3模型時出現錯誤:{e}")# spaCy 初始化(文本分類與標籤)nlp = spacy.load("en_core_web_sm")def process_audio(audio_file):try:result = whisper_pipe(audio_file)["text"]# 使用 DeepSeek 生成回應(如果成功加載模型)if deepseek_pipe is not None:messages=[{"role": "user", "content": result}]deepseek_response=deepseek_pipe(messages)[0]["generated_text"]doc=nlp(deepseek_response)entities=[(ent.text, ent.label_) for ent in doc.ents]return {"Transcription (Whisper)": result,"AI Response (DeepSeek)": deepseek_response,"Extracted Entities (spaCy)": entities}with gr.Blocks() as app:with gr.Row():audio_input=gr.Audio(source="microphone", type="filepath", label="上傳語音")output_text=gr.JSON(label="結果")submit_button=gr.Button("提交")submit_button.click(fn=lambda x: process_audio(x), inputs=[audio_input], outputs=[output_text])if __name__ == "__main__":
app.py+54-37