Upload transcript_refiner.py with huggingface_hub
Browse files- transcript_refiner.py +144 -0
transcript_refiner.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
+
from typing import Optional, Dict, Any
|
| 3 |
+
import streamlit as st
|
| 4 |
+
|
| 5 |
+
# 定義可用的 OpenAI 模型
|
| 6 |
+
OPENAI_MODELS = {
|
| 7 |
+
"gpt-4o": "GPT-4o",
|
| 8 |
+
"gpt-4o-mini": "GPT-4o-mini",
|
| 9 |
+
"o1-mini": "o1-mini",
|
| 10 |
+
"o3-mini": "o3-mini"
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
def refine_transcript(
|
| 14 |
+
raw_text: str,
|
| 15 |
+
api_key: str,
|
| 16 |
+
model: str = "o3-mini",
|
| 17 |
+
temperature: float = 0.5,
|
| 18 |
+
context: Optional[str] = None
|
| 19 |
+
) -> Optional[Dict[str, Any]]:
|
| 20 |
+
"""
|
| 21 |
+
使用 OpenAI 優化轉錄文字
|
| 22 |
+
|
| 23 |
+
Args:
|
| 24 |
+
raw_text: 原始文字
|
| 25 |
+
api_key: OpenAI API 金鑰
|
| 26 |
+
model: 使用的模型名稱
|
| 27 |
+
temperature: 創意程度 (0.0-1.0)
|
| 28 |
+
context: 背景資訊
|
| 29 |
+
"""
|
| 30 |
+
client = OpenAI(api_key=api_key)
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
# 準備 API 參數
|
| 34 |
+
system_prompt = (
|
| 35 |
+
"你是一個專業的文字編輯,負責將文字轉換成正確的繁體中文並修正語法錯誤。"
|
| 36 |
+
"請保持原意,但確保輸出是優美的繁體中文。"
|
| 37 |
+
)
|
| 38 |
+
if context:
|
| 39 |
+
system_prompt += f"\n\n背景資訊:{context}"
|
| 40 |
+
|
| 41 |
+
params = {
|
| 42 |
+
"model": model,
|
| 43 |
+
"messages": [
|
| 44 |
+
{
|
| 45 |
+
"role": "system",
|
| 46 |
+
"content": system_prompt
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"role": "user",
|
| 50 |
+
"content": f"請將以下文字轉換成繁體中文,並修正語法和標點符號:\n\n{raw_text}"
|
| 51 |
+
}
|
| 52 |
+
]
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
# 只有 gpt-4o 和 gpt-4o-mini 支援 temperature
|
| 56 |
+
if model.startswith("gpt-4"):
|
| 57 |
+
params["temperature"] = temperature
|
| 58 |
+
|
| 59 |
+
# 第一步:修正並轉換為繁體中文
|
| 60 |
+
correction_response = client.chat.completions.create(**params)
|
| 61 |
+
|
| 62 |
+
corrected_text = correction_response.choices[0].message.content
|
| 63 |
+
|
| 64 |
+
# 第二步:結構化整理(使用相同的參數設定)
|
| 65 |
+
params["messages"] = [
|
| 66 |
+
{
|
| 67 |
+
"role": "system",
|
| 68 |
+
"content": (
|
| 69 |
+
"你是一個專業的文字編輯,負責整理和結構化文字內容。"
|
| 70 |
+
"請以繁體中文輸出,並確保格式清晰易讀。"
|
| 71 |
+
)
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"role": "user",
|
| 75 |
+
"content": (
|
| 76 |
+
"請幫我整理以下文字,並提供:\n"
|
| 77 |
+
"1. 重點摘要\n"
|
| 78 |
+
"2. 關鍵字列表\n"
|
| 79 |
+
"3. 主要論點或重要資訊\n\n"
|
| 80 |
+
f"{corrected_text}"
|
| 81 |
+
)
|
| 82 |
+
}
|
| 83 |
+
]
|
| 84 |
+
|
| 85 |
+
summary_response = client.chat.completions.create(**params)
|
| 86 |
+
summary_text = summary_response.choices[0].message.content
|
| 87 |
+
|
| 88 |
+
# 計算總 token 使用量
|
| 89 |
+
total_input_tokens = (
|
| 90 |
+
correction_response.usage.prompt_tokens +
|
| 91 |
+
summary_response.usage.prompt_tokens
|
| 92 |
+
)
|
| 93 |
+
total_output_tokens = (
|
| 94 |
+
correction_response.usage.completion_tokens +
|
| 95 |
+
summary_response.usage.completion_tokens
|
| 96 |
+
)
|
| 97 |
+
|
| 98 |
+
return {
|
| 99 |
+
"corrected": corrected_text,
|
| 100 |
+
"summary": summary_text,
|
| 101 |
+
"usage": {
|
| 102 |
+
"total_input_tokens": total_input_tokens,
|
| 103 |
+
"total_output_tokens": total_output_tokens,
|
| 104 |
+
"model": model
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
except Exception as e:
|
| 109 |
+
print(f"文字優化失敗:{str(e)}")
|
| 110 |
+
return None
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
def convert_to_traditional_chinese(
|
| 114 |
+
text: str,
|
| 115 |
+
api_key: str,
|
| 116 |
+
model: str = "o3-mini"
|
| 117 |
+
) -> str:
|
| 118 |
+
"""將文字轉換為繁體中文"""
|
| 119 |
+
client = OpenAI(api_key=api_key)
|
| 120 |
+
|
| 121 |
+
response = client.chat.completions.create(
|
| 122 |
+
model=model,
|
| 123 |
+
temperature=0.1, # 使用較低的溫度以確保準確轉換
|
| 124 |
+
messages=[
|
| 125 |
+
{
|
| 126 |
+
"role": "system",
|
| 127 |
+
"content": "你是一個專業的繁簡轉換工具,請將輸入文字轉換成繁體中文,保持原意不變。"
|
| 128 |
+
},
|
| 129 |
+
{
|
| 130 |
+
"role": "user",
|
| 131 |
+
"content": text
|
| 132 |
+
}
|
| 133 |
+
]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
return response.choices[0].message.content
|
| 137 |
+
|
| 138 |
+
# Example usage with elevenlabs_stt:
|
| 139 |
+
# raw_transcript = transcribe_audio(...)['text']
|
| 140 |
+
# refined = refine_transcript(
|
| 141 |
+
# raw_text=raw_transcript,
|
| 142 |
+
# api_key="OPENAI_API_KEY",
|
| 143 |
+
# temperature=0.5
|
| 144 |
+
# )
|