Marathon23 commited on
Commit
cfbec94
·
verified ·
1 Parent(s): 7b70f57

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
app.py CHANGED
@@ -30,15 +30,17 @@ pipe = pipeline(
30
  def translate_text(input_text, target_language):
31
  prompt = f"請將以下文字翻譯成{target_language}:\n\n{input_text}"
32
 
33
- response = openai.ChatCompletion.create(
34
- model="gpt-4o",
35
- messages=[
36
- {"role": "user", "content": prompt}
37
- ]
38
- )
39
-
40
- translated_text = response['choices'][0]['message']['content'].strip()
41
- return translated_text
 
 
42
 
43
  @spaces.GPU
44
  def transcribe(inputs, task, translate_option, target_language):
@@ -115,13 +117,15 @@ def yt_transcribe(yt_url, task, translate_option, target_language, max_filesize=
115
 
116
  demo = gr.Blocks(theme=gr.themes.Ocean())
117
 
 
 
118
  mf_transcribe = gr.Interface(
119
  fn=transcribe,
120
  inputs=[
121
  gr.Audio(sources="microphone", type="filepath"),
122
  gr.Radio(["transcribe", "translate"], label="任務", value="transcribe"),
123
  gr.Radio(["是", "否"], label="是否翻譯轉錄結果", value="否"),
124
- gr.Dropdown(["英文", "日文", "法文", "德文", "西班牙文"], label="目標語言", value="英文")
125
  ],
126
  outputs="text",
127
  title="清華大學多模態課程&廖老師嫡傳弟子-第二組 「語音轉文字」model",
@@ -138,10 +142,10 @@ file_transcribe = gr.Interface(
138
  gr.Audio(sources="upload", type="filepath", label="音訊檔案"),
139
  gr.Radio(["transcribe", "translate"], label="任務", value="transcribe"),
140
  gr.Radio(["是", "否"], label="是否翻譯轉錄結果", value="否"),
141
- gr.Dropdown(["英文", "日文", "法文", "德文", "西班牙文"], label="目標語言", value="英文")
142
  ],
143
  outputs="text",
144
- title="Whisper Large V3: 轉錄音訊",
145
  description=(
146
  "只需點擊一下按鈕,即可轉錄長篇的麥克風或音訊輸入!演示使用了"
147
  f"檢查點 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 和 🤗 Transformers 來轉錄任意長度的音訊文件。"
@@ -155,10 +159,10 @@ yt_transcribe = gr.Interface(
155
  gr.Textbox(lines=1, placeholder="在此處貼上 YouTube 視頻的 URL", label="YouTube URL"),
156
  gr.Radio(["transcribe", "translate"], label="任務", value="transcribe"),
157
  gr.Radio(["是", "否"], label="是否翻譯轉錄結果", value="否"),
158
- gr.Dropdown(["英文", "日文", "法文", "德文", "西班牙文"], label="目標語言", value="英文")
159
  ],
160
  outputs=["html", "text"],
161
- title="Whisper Large V3: 轉錄 YouTube",
162
  description=(
163
  "只需點擊一下按鈕,即可轉錄長篇的 YouTube 視頻!演示使用了"
164
  f"檢查點 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 和 🤗 Transformers 來轉錄任意長度的視頻文件。"
 
30
  def translate_text(input_text, target_language):
31
  prompt = f"請將以下文字翻譯成{target_language}:\n\n{input_text}"
32
 
33
+ try:
34
+ response = openai.ChatCompletion.create(
35
+ model="gpt-3.5-turbo", # 更改模型名稱
36
+ messages=[
37
+ {"role": "user", "content": prompt}
38
+ ]
39
+ )
40
+ translated_text = response['choices'][0]['message']['content'].strip()
41
+ return translated_text
42
+ except Exception as e:
43
+ raise gr.Error(f"翻譯過程中出現錯誤:{str(e)}")
44
 
45
  @spaces.GPU
46
  def transcribe(inputs, task, translate_option, target_language):
 
117
 
118
  demo = gr.Blocks(theme=gr.themes.Ocean())
119
 
120
+ language_options = ["英文", "日文", "法文", "德文", "西班牙文", "繁體中文", "簡體中文", "越南文", "泰文"]
121
+
122
  mf_transcribe = gr.Interface(
123
  fn=transcribe,
124
  inputs=[
125
  gr.Audio(sources="microphone", type="filepath"),
126
  gr.Radio(["transcribe", "translate"], label="任務", value="transcribe"),
127
  gr.Radio(["是", "否"], label="是否翻譯轉錄結果", value="否"),
128
+ gr.Dropdown(language_options, label="目標語言", value="英文")
129
  ],
130
  outputs="text",
131
  title="清華大學多模態課程&廖老師嫡傳弟子-第二組 「語音轉文字」model",
 
142
  gr.Audio(sources="upload", type="filepath", label="音訊檔案"),
143
  gr.Radio(["transcribe", "translate"], label="任務", value="transcribe"),
144
  gr.Radio(["是", "否"], label="是否翻譯轉錄結果", value="否"),
145
+ gr.Dropdown(language_options, label="目標語言", value="英文")
146
  ],
147
  outputs="text",
148
+ title="清華大學多模態課程&廖老師嫡傳弟子-第二組 「語音轉文字」model:上傳音檔",
149
  description=(
150
  "只需點擊一下按鈕,即可轉錄長篇的麥克風或音訊輸入!演示使用了"
151
  f"檢查點 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 和 🤗 Transformers 來轉錄任意長度的音訊文件。"
 
159
  gr.Textbox(lines=1, placeholder="在此處貼上 YouTube 視頻的 URL", label="YouTube URL"),
160
  gr.Radio(["transcribe", "translate"], label="任務", value="transcribe"),
161
  gr.Radio(["是", "否"], label="是否翻譯轉錄結果", value="否"),
162
+ gr.Dropdown(language_options, label="目標語言", value="英文")
163
  ],
164
  outputs=["html", "text"],
165
+ title="清華大學多模態課程&廖老師嫡傳弟子-第二組 「語音轉文字」model: 轉錄 YouTube",
166
  description=(
167
  "只需點擊一下按鈕,即可轉錄長篇的 YouTube 視頻!演示使用了"
168
  f"檢查點 [{MODEL_NAME}](https://huggingface.co/{MODEL_NAME}) 和 🤗 Transformers 來轉錄任意長度的視頻文件。"