Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,15 @@ import torch
|
|
| 2 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 3 |
from datasets import load_dataset
|
| 4 |
import spacy
|
| 5 |
-
import gradio as gr
|
| 6 |
|
| 7 |
-
#
|
| 8 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 9 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 10 |
|
| 11 |
# Whisper 模型初始化(語音轉文字)
|
| 12 |
whisper_model_id = "openai/whisper-large-v3"
|
| 13 |
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 14 |
-
whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
| 15 |
-
)
|
| 16 |
whisper_model.to(device)
|
| 17 |
whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)
|
| 18 |
|
|
@@ -21,48 +19,60 @@ whisper_pipe = pipeline(
|
|
| 21 |
model=whisper_model,
|
| 22 |
tokenizer=whisper_processor.tokenizer,
|
| 23 |
feature_extractor=whisper_processor.feature_extractor,
|
| 24 |
-
device=device
|
| 25 |
-
)
|
| 26 |
-
|
| 27 |
-
# DeepSeek-V3 模型初始化(文本生成)
|
| 28 |
-
deepseek_pipe = None # 預設值,以防模型加載失敗
|
| 29 |
|
|
|
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
except Exception as e:
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# spaCy 初始化(文本分類與標籤)
|
| 36 |
nlp = spacy.load("en_core_web_sm")
|
| 37 |
|
| 38 |
def process_audio(audio_file):
|
| 39 |
|
| 40 |
-
# 語音轉文字
|
| 41 |
-
result = whisper_pipe(audio_file)["text"]
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
entities=[(ent.text, ent.label_) for ent in doc.ents]
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
|
|
|
| 56 |
|
|
|
|
| 57 |
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
| 66 |
|
| 67 |
with gr.Blocks() as app:
|
| 68 |
|
|
|
|
| 2 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 3 |
from datasets import load_dataset
|
| 4 |
import spacy
|
|
|
|
| 5 |
|
| 6 |
+
# 設置設備和環境變數(如有需要)
|
| 7 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 8 |
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 9 |
|
| 10 |
# Whisper 模型初始化(語音轉文字)
|
| 11 |
whisper_model_id = "openai/whisper-large-v3"
|
| 12 |
whisper_model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
| 13 |
+
whisper_model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True)
|
|
|
|
| 14 |
whisper_model.to(device)
|
| 15 |
whisper_processor = AutoProcessor.from_pretrained(whisper_model_id)
|
| 16 |
|
|
|
|
| 19 |
model=whisper_model,
|
| 20 |
tokenizer=whisper_processor.tokenizer,
|
| 21 |
feature_extractor=whisper_processor.feature_extractor,
|
| 22 |
+
device=device)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 安裝必要庫並下載 spaCy 英文小模型(如果尚未安裝)
|
| 25 |
try:
|
| 26 |
+
|
| 27 |
except Exception as e:
|
| 28 |
+
|
| 29 |
+
finally:
|
| 30 |
+
|
| 31 |
+
spacy.cli.download("en_core_web_sm")
|
| 32 |
|
|
|
|
| 33 |
nlp = spacy.load("en_core_web_sm")
|
| 34 |
|
| 35 |
def process_audio(audio_file):
|
| 36 |
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
|
| 39 |
+
# 語音轉文字
|
| 40 |
+
|
| 41 |
+
result= whisper_pipe(audio_file)["text"]
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# 使用其他文本生成模型替換,因為目前無法直接加載DeepSeek-V3
|
| 45 |
|
| 46 |
+
messages=[{"role": "user", "content": result}]
|
|
|
|
| 47 |
|
| 48 |
+
deepseek_response="" # 預設回應
|
| 49 |
+
|
| 50 |
+
try:
|
| 51 |
+
from transformers import pipeline
|
| 52 |
|
| 53 |
+
pipe=pipeline("text-generation",model="t5-base")
|
| 54 |
|
| 55 |
+
deepseek_response=pipe(messages)[0]["generated_text"]
|
| 56 |
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# 使用 spaCy 分析文本
|
| 60 |
+
|
| 61 |
+
doc=nlp(deepseek_response)
|
| 62 |
+
entities=[(ent.text, ent.label_) for ent in doc.ents]
|
| 63 |
+
|
| 64 |
+
return {
|
| 65 |
+
"Transcription (Whisper)": result,
|
| 66 |
+
"AI Response (T5)": deepseek_response,# 修改為 T5 回應以避免與原來不同步
|
| 67 |
+
"Extracted Entities (spaCy)": entities}
|
| 68 |
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return {
|
| 71 |
+
"Transcription (Whisper)": result,# 保留原始轉錄內容
|
| 72 |
+
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
|
| 76 |
|
| 77 |
with gr.Blocks() as app:
|
| 78 |
|