Choose a language to see the code snippets for interacting with the API. 选择一种语言以查看与 API…
- User
Choose a language to see the code snippets for interacting with the API. 选择一种语言以查看与 API 交互的代码片段。
- Install the python client (docs) if you don't already have it installed.
- 如果您还没有安装,请安装 python 客户端 (文档)。
copy $ pip install gradio_client 2. Find the API endpoint below corresponding to your desired function in the app. Copy the code snippet, replacing the placeholder values with your own input data. If this is a private Space, you may need to pass your Hugging Face token as well (read more). Or use the API Recorder
to automatically generate your API requests. 2. 在下面找到对应您所需功能的 API 端点 在应用中的函数。复制代码片段,替换掉 占位符值用您自己的输入数据替换。 如果这是一个私人空间,您可能需要通过您的 拥抱脸 token 同样适用(阅读更多)。 或者使用 API 录制器
自动生成您的 API 请求。
api_name: /add_text copy from gradio_client import Client
client = Client("Qwen/Qwen2-Audio-Instruct-Demo") result = client.predict( chatbot=[], input={"files":[],"text":""}, api_name="/add_text" ) print(result) Accepts 2 parameters: chatbot Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None] Default: []
The input value that is provided in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输入值,该值在“Qwen2-Audio-7B-Instruct”Modelscopechatbot 组件中提供。
input Dict(files: List[filepath | str], text: str) Default: {"files":[],"text":""}
The input value that is provided in the "parameter_6" Modelscopemultimodalinput component. parameter_6 模型域多模态输入组件中提供的输入值。
Returns tuple of 2 elements [0] Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None]
The output value that appears in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输出值出现在“Qwen2-Audio-7B-Instruct”模型 Scopechatbot 组件中。
[1] Dict(files: List[filepath | str], text: str)
The output value that appears in the "value_6" Modelscopemultimodalinput component. 输出值出现在“value_6”Modelscopemultimodalinput 组件中。
api_name: /predict copy from gradio_client import Client
client = Client("Qwen/Qwen2-Audio-Instruct-Demo") result = client.predict( chatbot=[], api_name="/predict" ) print(result) Accepts 1 parameter: chatbot Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None] Default: []
The input value that is provided in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输入值,该值在“Qwen2-Audio-7B-Instruct”Modelscopechatbot 组件中提供。
Returns 1 element Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None]
The output value that appears in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输出值出现在“Qwen2-Audio-7B-Instruct”模型 Scopechatbot 组件中。
api_name: /reset_state copy from gradio_client import Client
client = Client("Qwen/Qwen2-Audio-Instruct-Demo") result = client.predict( api_name="/reset_state" ) print(result) Accepts 0 parameters: Returns 1 element Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None]
The output value that appears in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输出值出现在“Qwen2-Audio-7B-Instruct”模型 Scopechatbot 组件中。
api_name: /regenerate copy from gradio_client import Client
client = Client("Qwen/Qwen2-Audio-Instruct-Demo") result = client.predict( chatbot=[], api_name="/regenerate" ) print(result) Accepts 1 parameter: chatbot Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None] Default: []
The input value that is provided in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输入值,该值在“Qwen2-Audio-7B-Instruct”Modelscopechatbot 组件中提供。
Returns 1 element Tuple[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None, Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | List[Dict(id: str | None, elem_id: str | None, elem_classes: str | List[str] | None, name: str | None, text: str | None, flushing: bool | None, avatar: str | filepath | None, files: List[Dict(file: filepath, alt_text: str | None) | Dict() | filepath | str | str] | None) | Dict(files: List[filepath | str], text: str) | Dict() | str | None] | None]
The output value that appears in the "Qwen2-Audio-7B-Instruct" Modelscopechatbot component. 输出值出现在“Qwen2-Audio-7B-Instruct”模型 Scopechatbot 组件中。
给我实现一个程序,用于提升普通话水平,大概就是用户点击录音,然后将录音和对应的提示词请求这个接口,最后将接口返回结果展示给用户
- Assistant
好的,我可以帮助您实现一个用于提升普通话水平的程序。该程序的主要功能是:
- 用户点击按钮开始录音。
- 用户输入对应的提示词。
- 将录音和提示词发送到指定的接口(API)。
- 接收接口返回的结果并展示给用户。
下面我将为您提供一个基于 Python 的示例程序,使用
gradio库创建用户界面,并使用gradio_client库与 API 进行交互。步骤 1:安装所需的库
首先,确保您已经安装了
gradio和gradio_client库。如果尚未安装,请运行以下命令:pip install gradio gradio_client步骤 2:编写程序
以下是示例代码,请将其保存为
app.py:import gradio as gr from gradio_client import Client def send_to_api(recording, prompt_text): client = Client("Qwen/Qwen2-Audio-Instruct-Demo") # 将录音文件和提示文本发送到API的 /add_text 端点 result_add_text = client.predict( chatbot=[], # 初始化聊天记录 input={"files": [recording], "text": prompt_text}, api_name="/add_text" ) # 接着调用 /predict 端点以获取模型的响应 result_predict = client.predict( chatbot=result_add_text[0], # 使用上一步的聊天记录 api_name="/predict" ) # 返回模型的回复 response = result_predict[0][-1]['text'] # 获取最新的回复文本 return response with gr.Blocks() as demo: gr.Markdown("# 普通话水平提升助手") with gr.Row(): with gr.Column(): audio_input = gr.Audio(source="microphone", type="filepath", label="点击录音") prompt_input = gr.Textbox(label="请输入提示词") submit_btn = gr.Button("提交") with gr.Column(): output_text = gr.Textbox(label="接口返回结果") submit_btn.click(fn=send_to_api, inputs=[audio_input, prompt_input], outputs=output_text) demo.launch()步骤 3:代码说明
导入库
import gradio as gr from gradio_client import Clientgradio:用于创建简洁的 Web 界面,方便用户交互。gradio_client:用于与远程的 Gradio 接口进行交互。
定义与 API 交互的函数
def send_to_api(recording, prompt_text): client = Client("Qwen/Qwen2-Audio-Instruct-Demo") # 将录音文件和提示文本发送到API的 /add_text 端点 result_add_text = client.predict( chatbot=[], # 初始化聊天记录 input={"files": [recording], "text": prompt_text}, api_name="/add_text" ) # 接着调用 /predict 端点以获取模型的响应 result_predict = client.predict( chatbot=result_add_text[0], # 使用上一步的聊天记录 api_name="/predict" ) # 返回模型的回复 response = result_predict[0][-1]['text'] # 获取最新的回复文本 return response- 初始化客户端:连接到指定的 API 接口。
- 使用
/add_text端点发送录音文件和提示词。 - 使用
/predict端点获取模型的回复。 - 提取并返回模型的回复文本。
创建 Gradio 界面
with gr.Blocks() as demo: gr.Markdown("# 普通话水平提升助手") with gr.Row(): with gr.Column(): audio_input = gr.Audio(source="microphone", type="filepath", label="点击录音") prompt_input = gr.Textbox(label="请输入提示词") submit_btn = gr.Button("提交") with gr.Column(): output_text = gr.Textbox(label="接口返回结果") submit_btn.click(fn=send_to_api, inputs=[audio_input, prompt_input], outputs=output_text) demo.launch()- 使用
gr.Blocks()创建一个复杂的布局。 gr.Audio:用于录制音频。source="microphone"表示从麦克风录制。type="filepath"表示返回录制的文件路径。
gr.Textbox:用于输入提示词和显示结果。gr.Button:提交按钮。submit_btn.click():当点击按钮时,调用send_to_api函数,并将输入和输出组件对应起来。demo.launch():启动 Gradio 应用。
步骤 4:运行程序
在终端中运行以下命令启动程序:
python app.py程序启动后,终端会显示一个本地链接,例如:
Running on local URL: http://127.0.0.1:7860/在浏览器中打开该链接,您将看到一个简单的界面:
- 点击“点击录音”开始录音,完成后停止录音。
- 在“请输入提示词”中输入您想练习的句子或短语。
- 点击“提交”按钮。
- 接口返回的结果将显示在“接口返回结果”文本框中。
注意事项
-
API 权限:如果
Qwen/Qwen2-Audio-Instruct-Demo是一个私有空间,您需要提供 Hugging Face 的访问令牌。在创建Client时,可以传入hf_token参数:client = Client("Qwen/Qwen2-Audio-Instruct-Demo", hf_token="your_hf_token")请将
"your_hf_token"替换为您的实际令牌。 -
录音权限:浏览器可能会请求访问麦克风的权限,请确保允许。
-
错误处理:此示例代码未包含错误处理,建议在实际使用中添加相应的异常捕获和处理。
-
接口响应格式:根据实际接口返回的数据格式,可能需要调整代码中解析响应的部分。
扩展功能
- 结果评估:您可以根据接口返回的结果,对用户的发音或语法进行评分或提供改进建议。
- 历史记录:保存用户的练习历史,方便跟踪进度。
- 多语言支持:如果需要,还可以扩展支持其他语言的练习。
总结
通过上述步骤,您将拥有一个简单的 Web 程序,帮助用户通过录音和练习提示词来提升普通话水平。程序利用了
gradio库提供友好的用户界面,并且通过gradio_client与远程接口进行交互,获取分析结果并展示给用户。希望这对您有所帮助!