Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,7 +40,8 @@ def process_audio(audio_file):
|
|
| 40 |
|
| 41 |
try:
|
| 42 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
| 43 |
-
wav_path = audio_file.replace(".mp3", ".wav")
|
|
|
|
| 44 |
audio_segment.export(wav_path, format="wav")
|
| 45 |
|
| 46 |
except Exception as e:
|
|
@@ -48,37 +49,45 @@ def process_audio(audio_file):
|
|
| 48 |
|
| 49 |
# 語音轉文字
|
| 50 |
|
| 51 |
-
|
|
|
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
pipe=pipeline("text-generation",model="t5-base")
|
| 63 |
-
|
| 64 |
-
deepseek_response=pipe(messages)[0]["generated_text"]
|
| 65 |
-
|
| 66 |
-
# 使用 spaCy 分析文本
|
| 67 |
|
| 68 |
-
|
| 69 |
-
entities=[(ent.text, ent.label_) for ent in doc.ents] if doc is not None else []
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
"Extracted Entities (spaCy)": entities}
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
| 77 |
return {
|
| 78 |
-
"Transcription (Whister)": result
|
| 79 |
-
|
|
|
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
with gr.Blocks() as app:
|
| 84 |
|
|
@@ -96,10 +105,4 @@ with gr.Blocks() as app:
|
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
submit_button.click(fn=lambda x: process_audio(x), inputs=[audio_input], outputs=[output_text])
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
if __name__ == "__main__":
|
| 105 |
-
app.launch()
|
|
|
|
| 40 |
|
| 41 |
try:
|
| 42 |
audio_segment = AudioSegment.from_mp3(audio_file)
|
| 43 |
+
wav_path = "/tmp/" + audio_file.split("/")[-1].replace(".mp3", ".wav") # 將檔案存放於 /tmp 目錄
|
| 44 |
+
|
| 45 |
audio_segment.export(wav_path, format="wav")
|
| 46 |
|
| 47 |
except Exception as e:
|
|
|
|
| 49 |
|
| 50 |
# 語音轉文字
|
| 51 |
|
| 52 |
+
try:
|
| 53 |
+
result= whisper_pipe(wav_path)["text"]
|
| 54 |
|
| 55 |
+
# 使用 T5 作為替代模型
|
| 56 |
+
|
| 57 |
+
messages=[{"role": "user", "content": result}]
|
| 58 |
+
|
| 59 |
+
deepseek_response=""
|
| 60 |
+
|
| 61 |
+
try:
|
| 62 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
+
pipe=pipeline("text-generation",model="t5-base")
|
|
|
|
| 65 |
|
| 66 |
+
deepseek_response=pipe(messages)[0]["generated_text"]
|
| 67 |
+
|
| 68 |
+
# 使用 spaCy 分析文本
|
|
|
|
| 69 |
|
| 70 |
+
doc=nlp(deepseek_response) if nlp is not None else None
|
| 71 |
+
entities=[(ent.text, ent.label_) for ent in doc.ents] if doc is not None else []
|
| 72 |
+
|
| 73 |
return {
|
| 74 |
+
"Transcription (Whister)": result,
|
| 75 |
+
"AI Response (T5)": deepseek_response,# 修改為 T5 回應以避免與原來不同步
|
| 76 |
+
"Extracted Entities (spaCy)": entities}
|
| 77 |
|
| 78 |
+
except Exception as e:
|
| 79 |
+
return {
|
| 80 |
+
"Transcription (Whister)": result,# 保留原始轉錄內容
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
|
| 86 |
+
return {"Error": f"語音轉文字失敗:{e}"}
|
| 87 |
|
| 88 |
+
def clear_input():
|
| 89 |
+
|
| 90 |
+
return "", ""
|
| 91 |
|
| 92 |
with gr.Blocks() as app:
|
| 93 |
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
+
submit
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|