Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import torch
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
from datasets import load_dataset
|
| 4 |
import spacy
|
| 5 |
import gradio as gr
|
|
@@ -24,8 +25,11 @@ whisper_pipe = pipeline(
|
|
| 24 |
device=device,
|
| 25 |
)
|
| 26 |
|
| 27 |
-
# DeepSeek 模型初始化(文本生成)
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
# spaCy 初始化(文本分類與標籤)
|
| 31 |
nlp = spacy.load("en_core_web_sm")
|
|
@@ -35,38 +39,60 @@ def process_audio(audio_file):
|
|
| 35 |
# 語音轉文字
|
| 36 |
result = whisper_pipe(audio_file)["text"]
|
| 37 |
|
| 38 |
-
# 使用 DeepSeek
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 45 |
-
|
| 46 |
-
return result, deepseek_response, entities
|
| 47 |
|
| 48 |
# Gradio 界面設計
|
| 49 |
def interface(audio_file):
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
# Gradio 應用程序
|
| 58 |
with gr.Blocks() as app:
|
| 59 |
-
gr.Markdown("# AI 客服自動化系統")
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
# 啟動應用程序(本地測試時使用)
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# 部署到 Hugging Face Spaces 時,將 `app.launch()` 替換為 `app`
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor
|
| 4 |
from datasets import load_dataset
|
| 5 |
import spacy
|
| 6 |
import gradio as gr
|
|
|
|
| 25 |
device=device,
|
| 26 |
)
|
| 27 |
|
| 28 |
+
# DeepSeek-V3 模型初始化(文本生成)
|
| 29 |
+
try:
|
| 30 |
+
deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V3", trust_remote_code=True)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"加載模型時出現錯誤:{e}")
|
| 33 |
|
| 34 |
# spaCy 初始化(文本分類與標籤)
|
| 35 |
nlp = spacy.load("en_core_web_sm")
|
|
|
|
| 39 |
# 語音轉文字
|
| 40 |
result = whisper_pipe(audio_file)["text"]
|
| 41 |
|
| 42 |
+
# 使用 DeepSeek 生成回應(如果成功加載模型)
|
| 43 |
+
try:
|
| 44 |
+
messages = [{"role": "user", "content": result}]
|
| 45 |
+
deepseek_response = deepseek_pipe(messages)[0]["generated_text"]
|
| 46 |
+
|
| 47 |
+
# 使用 spaCy 分析文本
|
| 48 |
+
doc = nlp(deepseek_response)
|
| 49 |
+
entities = [(ent.text, ent.label_) for ent in doc.ents]
|
| 50 |
+
|
| 51 |
+
return result, deepseek_response, entities
|
| 52 |
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return result, f"錯誤:無法生成回應 - {e}", []
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
# Gradio 界面設計
|
| 57 |
def interface(audio_file):
|
| 58 |
+
transcription_result_list # 將結果包裝為字典以適應 Gradio 輸出格式
|
| 59 |
+
|
| 60 |
+
try:
|
| 61 |
+
transcription_result_list # 這裡需要修改以適應新的返回格式
|
| 62 |
+
|
| 63 |
+
transcription_result_dict # 這裡需要修改以適應新的返回格式
|
| 64 |
+
|
| 65 |
+
return {
|
| 66 |
+
"Transcription (Whisper)": transcription_result_dict["Transcription (Whisper)"],
|
| 67 |
+
"AI Response (DeepSeek)": transcription_result_dict["AI Response (DeepSeek)"],
|
| 68 |
+
"Extracted Entities (spaCy)": transcription_result_dict["Extracted Entities (spaCy)"],
|
| 69 |
+
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
|
| 81 |
|
|
|
|
| 82 |
with gr.Blocks() as app:
|
|
|
|
| 83 |
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
#
|
| 89 |
+
with gr.Row():
|
| 90 |
+
audio_input=gr.Audio(source="microphone", type="filepath", label="上傳語音")
|
| 91 |
+
output_text=gr.JSON(label="結果")
|
| 92 |
+
|
| 93 |
+
submit_button=gr.Button("提交")
|
| 94 |
+
submit_button.click(fn=lambda x: process_audio(x), inputs=[audio_input], outputs=[output_text])
|
| 95 |
+
|
| 96 |
|
|
|
|
| 97 |
if __name__ == "__main__":
|
| 98 |
+
app.launch()
|
|
|
|
|
|