Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,62 @@
|
|
1 |
import gradio as gr
|
2 |
import openai
|
3 |
-
import tempfile
|
4 |
import os
|
5 |
-
import random
|
6 |
|
7 |
-
|
8 |
-
|
|
|
9 |
"""
|
10 |
|
11 |
-
def transcribe_audio(
|
12 |
-
if api_key is None:
|
13 |
-
return "请输入openai官方apikey"
|
14 |
if audio is None:
|
15 |
-
return "
|
|
|
|
|
|
|
16 |
|
17 |
client = openai.OpenAI(
|
18 |
-
api_key
|
|
|
19 |
)
|
20 |
try:
|
21 |
-
|
22 |
with open(audio, "rb") as audio_file:
|
23 |
result = client.audio.transcriptions.create(
|
24 |
-
model
|
25 |
-
file
|
26 |
-
prompt
|
27 |
)
|
28 |
-
return result.text
|
29 |
except Exception as e:
|
30 |
-
return f'转录音频时出现错误: {str(e)}'
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
)
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import openai
|
|
|
3 |
import os
|
|
|
4 |
|
5 |
+
# 配置基本参数
|
6 |
+
PROMPT = """首先,需要澄清的是,我方对"偏爱"的定义是:**教师基于主观喜好或对"优秀"的片面理解,给予特定学生与其学业表现或课堂行为无关的特殊待遇或关注,而忽视其他学生正当的学习需求和情感体验,从而损害教育公平的行为。**对方辩友试图将"偏爱"的对象限定为客观标准下的"优秀学生",这是偷换概念的逻辑错误。
|
7 |
+
即使是看似客观的评价标准,例如"成绩优秀"、"品行突出",在实际操作中也充满了主观性。每个老师对"优秀"的理解不同,对学生行为的评价标准也不同,最终就会导致"偏爱"的对象并非真正意义上的"优秀",而是取决于教师个人的喜好。试问,一个在数学方面有天赋但语文成绩相对较弱的学生,难道就不值得被老师关注和鼓励吗?
|
8 |
"""
|
9 |
|
10 |
+
def transcribe_audio(audio, api_key_state):
|
|
|
|
|
11 |
if audio is None:
|
12 |
+
return "没有检测到音频,请重新录音或者上传音频文件。", api_key_state
|
13 |
+
|
14 |
+
if not api_key_state:
|
15 |
+
return "请输入有效的API密钥。", api_key_state
|
16 |
|
17 |
client = openai.OpenAI(
|
18 |
+
api_key=api_key_state,
|
19 |
+
base_url=BASE_URL,
|
20 |
)
|
21 |
try:
|
22 |
+
# 使用 OpenAI Whisper API 进行转录
|
23 |
with open(audio, "rb") as audio_file:
|
24 |
result = client.audio.transcriptions.create(
|
25 |
+
model="whisper-1",
|
26 |
+
file=audio_file,
|
27 |
+
prompt=PROMPT,
|
28 |
)
|
29 |
+
return result.text, api_key_state
|
30 |
except Exception as e:
|
31 |
+
return f'转录音频时出现错误: {str(e)}', api_key_state
|
32 |
+
|
33 |
+
def set_api_key(api_key, api_key_state):
|
34 |
+
return api_key, api_key # 更新显示的 API 密钥和存储的状态
|
35 |
+
|
36 |
+
def clear_inputs(audio, api_key, api_key_state):
|
37 |
+
return None, api_key, api_key_state # 只清除音频,保留 API 密钥
|
38 |
+
|
39 |
+
with gr.Blocks(theme="default") as iface:
|
40 |
+
gr.Markdown("# 💬 Agent4DB专用语音转文字工具")
|
41 |
+
gr.Markdown("🤟 使用 OpenAI Whisper API 将语音转录为文字")
|
42 |
+
|
43 |
+
api_key_state = gr.State("")
|
44 |
+
|
45 |
+
with gr.Row():
|
46 |
+
audio_input = gr.Audio(sources=["microphone", "upload"], type="filepath", label="录音或上传音频")
|
47 |
+
api_key_input = gr.Textbox(type="password", label="API密钥", placeholder="请输入您的API密钥")
|
48 |
+
|
49 |
+
transcribe_button = gr.Button("转录")
|
50 |
+
clear_button = gr.ClearButton()
|
51 |
+
output = gr.Textbox(label="转录文本", placeholder="转录的文字将显示在这里...", lines=10, interactive=True)
|
52 |
+
|
53 |
+
# 设置 API 密钥的回调
|
54 |
+
api_key_input.change(set_api_key, [api_key_input, api_key_state], [api_key_input, api_key_state])
|
55 |
+
|
56 |
+
# 转录按钮的回调
|
57 |
+
transcribe_button.click(transcribe_audio, inputs=[audio_input, api_key_state], outputs=[output, api_key_state])
|
58 |
+
|
59 |
+
# 清除按钮的回调
|
60 |
+
clear_button.click(clear_inputs, inputs=[audio_input, api_key_input, api_key_state], outputs=[audio_input, api_key_input, api_key_state])
|
61 |
+
|
62 |
+
iface.launch(share=True)
|