Dibiddo commited on
Commit
4a67e11
·
verified ·
1 Parent(s): 5a096f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -44
app.py CHANGED
@@ -10,29 +10,19 @@ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
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,44 +30,55 @@ 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
 
@@ -93,7 +94,6 @@ with gr.Blocks() as app:
93
 
94
 
95
 
96
-
97
 
98
 
99
 
 
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
 
19
+ whisper_pipe = pipeline(
20
+ "automatic-speech-recognition",
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 # 預設值,以防模型加載失敗
 
30
  try:
31
  deepseek_pipe = pipeline("text-generation", model="deepseek-ai/DeepSeek-V3", trust_remote_code=True)
32
  except Exception as e:
33
+ print(f"加載模型時出現錯誤:{e}")
34
 
35
  # spaCy 初始化(文本分類與標籤)
36
  nlp = spacy.load("en_core_web_sm")
37
 
38
  def process_audio(audio_file):
39
 
40
+ result = whisper_pipe(audio_file)["text"]
41
+
 
 
42
 
43
 
44
 
45
  # 使用 DeepSeek 生成回應(如果成功加載模型)
46
+
47
+
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
 
50
+
51
 
52
 
53
 
 
54
 
55
 
56
+ # 修正後:將這個判斷放在函數內部,並且去掉多餘的縮進
57
+
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
+ else:
75
+ return {
76
+ "Transcription (Whisper)": result,
77
+ # 可以添加預設回應或錯誤信息
78
+
79
+ }
80
+
81
+
82
 
83
  with gr.Blocks() as app:
84
 
 
94
 
95
 
96
 
 
97
 
98
 
99