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