Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,25 @@
|
|
| 1 |
-
# file: app.py
|
| 2 |
import os
|
| 3 |
from pydub import AudioSegment
|
| 4 |
from openai import OpenAI
|
| 5 |
import gradio as gr
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 8 |
-
MAX_SIZE = 25 * 1024 * 1024
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
def split_audio_if_needed(input_path):
|
| 11 |
size = os.path.getsize(input_path)
|
| 12 |
if size <= MAX_SIZE:
|
| 13 |
return [input_path]
|
| 14 |
-
|
| 15 |
audio = AudioSegment.from_file(input_path)
|
| 16 |
num_chunks = int(size / MAX_SIZE) + 1
|
| 17 |
chunk_length = len(audio) / num_chunks
|
| 18 |
-
|
| 19 |
chunk_files = []
|
| 20 |
for i in range(num_chunks):
|
| 21 |
start = int(i * chunk_length)
|
|
@@ -26,6 +30,9 @@ def split_audio_if_needed(input_path):
|
|
| 26 |
chunk_files.append(chunk_filename)
|
| 27 |
return chunk_files
|
| 28 |
|
|
|
|
|
|
|
|
|
|
| 29 |
def transcribe_and_summarize(file):
|
| 30 |
if not file:
|
| 31 |
return "請上傳音訊檔。", ""
|
|
@@ -52,15 +59,29 @@ def transcribe_and_summarize(file):
|
|
| 52 |
summary = response.choices[0].message.content.strip()
|
| 53 |
return full_text, summary
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
iface = gr.Interface(
|
| 56 |
-
fn=
|
| 57 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 58 |
outputs=[
|
| 59 |
gr.Textbox(label="完整轉錄文字", lines=10),
|
| 60 |
-
gr.Textbox(label="摘要結果", lines=10)
|
| 61 |
],
|
| 62 |
-
title="語音轉錄與摘要工具",
|
| 63 |
-
description="
|
| 64 |
)
|
| 65 |
|
| 66 |
iface.launch()
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from pydub import AudioSegment
|
| 3 |
from openai import OpenAI
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# ========================
|
| 7 |
+
# 🔐 使用者設定區
|
| 8 |
+
# ========================
|
| 9 |
+
PASSWORD = "ckw2025" # ← 改成你要的登入密碼
|
| 10 |
+
MAX_SIZE = 25 * 1024 * 1024 # 25 MB 限制
|
| 11 |
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
|
|
|
| 12 |
|
| 13 |
+
# ========================
|
| 14 |
+
# 🔊 音訊分割
|
| 15 |
+
# ========================
|
| 16 |
def split_audio_if_needed(input_path):
|
| 17 |
size = os.path.getsize(input_path)
|
| 18 |
if size <= MAX_SIZE:
|
| 19 |
return [input_path]
|
|
|
|
| 20 |
audio = AudioSegment.from_file(input_path)
|
| 21 |
num_chunks = int(size / MAX_SIZE) + 1
|
| 22 |
chunk_length = len(audio) / num_chunks
|
|
|
|
| 23 |
chunk_files = []
|
| 24 |
for i in range(num_chunks):
|
| 25 |
start = int(i * chunk_length)
|
|
|
|
| 30 |
chunk_files.append(chunk_filename)
|
| 31 |
return chunk_files
|
| 32 |
|
| 33 |
+
# ========================
|
| 34 |
+
# 🎧 主功能:轉錄 + 摘要
|
| 35 |
+
# ========================
|
| 36 |
def transcribe_and_summarize(file):
|
| 37 |
if not file:
|
| 38 |
return "請上傳音訊檔。", ""
|
|
|
|
| 59 |
summary = response.choices[0].message.content.strip()
|
| 60 |
return full_text, summary
|
| 61 |
|
| 62 |
+
# ========================
|
| 63 |
+
# 🔑 密碼驗證包裝
|
| 64 |
+
# ========================
|
| 65 |
+
def transcribe_with_password(password, file):
|
| 66 |
+
if password != PASSWORD:
|
| 67 |
+
return "❌ 密碼錯誤,請重新輸入。", ""
|
| 68 |
+
return transcribe_and_summarize(file)
|
| 69 |
+
|
| 70 |
+
# ========================
|
| 71 |
+
# 🌐 Gradio 介面
|
| 72 |
+
# ========================
|
| 73 |
iface = gr.Interface(
|
| 74 |
+
fn=transcribe_with_password,
|
| 75 |
+
inputs=[
|
| 76 |
+
gr.Textbox(label="輸入密碼", type="password", placeholder="請輸入授權密碼"),
|
| 77 |
+
gr.Audio(type="filepath", label="上傳音訊檔 (.m4a, .aac, .wav)")
|
| 78 |
+
],
|
| 79 |
outputs=[
|
| 80 |
gr.Textbox(label="完整轉錄文字", lines=10),
|
| 81 |
+
gr.Textbox(label="摘要結果", lines=10)
|
| 82 |
],
|
| 83 |
+
title="語音轉錄與摘要工具 (受密碼保護)",
|
| 84 |
+
description="上傳音訊後,自動轉錄文字與產生摘要。請先輸入授權密碼以使用本服務。",
|
| 85 |
)
|
| 86 |
|
| 87 |
iface.launch()
|