Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from datasets import load_dataset
|
| 5 |
import spacy
|
| 6 |
import gradio as gr
|
|
@@ -11,19 +10,29 @@ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
| 11 |
|
| 12 |
# Whisper 模型初始化(語音轉文字)
|
| 13 |
whisper_model_id = "openai/whisper-large-v3"
|
| 14 |
-
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 15 |
-
whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 16 |
-
)
|
| 17 |
-
whisper_model.to(device)
|
| 18 |
-
whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
device
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# DeepSeek-V3 模型初始化(文本生成)
|
| 29 |
deepseek_pipe = None # 預設值,以防模型加載失敗
|
|
@@ -31,56 +40,64 @@ deepseek_pipe = None # 預設值,以防模型加載失敗
|
|
| 31 |
try:
|
| 32 |
deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V3", trust_remote_code=True)
|
| 33 |
except Exception as e:
|
| 34 |
-
print(f"
|
| 35 |
|
| 36 |
# spaCy 初始化(文本分類與標籤)
|
| 37 |
nlp = spacy.load("en_core_web_sm")
|
| 38 |
|
| 39 |
def process_audio(audio_file):
|
| 40 |
-
# 語音轉文字
|
| 41 |
-
result = whisper_pipe(audio_file)["text"]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
if deepseek_pipe is not None:
|
| 45 |
-
messages = [{"role": "user", "content": result}]
|
| 46 |
-
deepseek_response = deepseek_pipe(messages)[0]["generated_text"]
|
| 47 |
|
| 48 |
-
|
| 49 |
-
doc = nlp(deepseek_response)
|
| 50 |
-
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 51 |
|
| 52 |
-
return {
|
| 53 |
-
"Transcription (Whisper)": result,
|
| 54 |
-
"AI Response (DeepSeek)": deepseek_response,
|
| 55 |
-
"Extracted Entities (spaCy)": entities,
|
| 56 |
|
| 57 |
-
}
|
| 58 |
-
|
| 59 |
|
| 60 |
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
-
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
with gr.Blocks() as app:
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
with gr.Row():
|
| 72 |
audio_input=gr.Audio(source="microphone", type="filepath", label="上傳語音")
|
| 73 |
output_text=gr.JSON(label="結果")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
|
| 82 |
|
| 83 |
|
|
|
|
| 84 |
submit_button.click(fn=lambda x: process_audio(x), inputs=[audio_input], outputs=[output_text])
|
| 85 |
|
| 86 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import torch
|
| 2 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
|
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
import spacy
|
| 5 |
import gradio as gr
|
|
|
|
| 10 |
|
| 11 |
# Whisper 模型初始化(語音轉文字)
|
| 12 |
whisper_model_id = "openai/whisper-large-v3"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
try:
|
| 15 |
+
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 16 |
+
whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True,
|
| 17 |
+
revision="main", # 嘗試指定修訂版以解決兼容性問題
|
| 18 |
+
)
|
| 19 |
+
whisper_model.to(device)
|
| 20 |
+
|
| 21 |
+
# 加載處理器時也指定修訂版(如果需要)
|
| 22 |
+
whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"加載Whisper模型或處理器時出現錯誤:{e}")
|
| 25 |
+
else:
|
| 26 |
+
# 成功加載後建立pipeline進行語音轉文字工作
|
| 27 |
+
|
| 28 |
+
whisper_pipe = pipeline(
|
| 29 |
+
"automatic-speech-recognition",
|
| 30 |
+
model=whisper_model,
|
| 31 |
+
tokenizer=whisper_processor.tokenizer,
|
| 32 |
+
feature_extractor=whisper_processor.feature_extractor,
|
| 33 |
+
device=device,
|
| 34 |
+
|
| 35 |
+
)
|
| 36 |
|
| 37 |
# DeepSeek-V3 模型初始化(文本生成)
|
| 38 |
deepseek_pipe = None # 預設值,以防模型加載失敗
|
|
|
|
| 40 |
try:
|
| 41 |
deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V3", trust_remote_code=True)
|
| 42 |
except Exception as e:
|
| 43 |
+
print(f"加載DeepSeek-V3模型時出現錯誤:{e}")
|
| 44 |
|
| 45 |
# spaCy 初始化(文本分類與標籤)
|
| 46 |
nlp = spacy.load("en_core_web_sm")
|
| 47 |
|
| 48 |
def process_audio(audio_file):
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
try:
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
result = whisper_pipe(audio_file)["text"]
|
|
|
|
|
|
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
|
|
|
|
|
|
| 55 |
|
| 56 |
|
| 57 |
+
# 使用 DeepSeek 生成回應(如果成功加載模型)
|
| 58 |
+
if deepseek_pipe is not None:
|
| 59 |
+
messages=[{"role": "user", "content": result}]
|
| 60 |
+
deepseek_response=deepseek_pipe(messages)[0]["generated_text"]
|
| 61 |
+
|
| 62 |
+
doc=nlp(deepseek_response)
|
| 63 |
+
entities=[(ent.text, ent.label_) for ent in doc.ents]
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
"Transcription (Whisper)": result,
|
| 67 |
+
"AI Response (DeepSeek)": deepseek_response,
|
| 68 |
+
"Extracted Entities (spaCy)": entities
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
}
|
| 73 |
+
|
| 74 |
+
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
+
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
with gr.Blocks() as app:
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
|
| 86 |
|
| 87 |
with gr.Row():
|
| 88 |
audio_input=gr.Audio(source="microphone", type="filepath", label="上傳語音")
|
| 89 |
output_text=gr.JSON(label="結果")
|
| 90 |
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
+
submit_button=gr.Button("提交")
|
| 101 |
submit_button.click(fn=lambda x: process_audio(x), inputs=[audio_input], outputs=[output_text])
|
| 102 |
|
| 103 |
if __name__ == "__main__":
|