如何将Gradio块用作可以在本地函数中使用的变量?

解释

我试图使用Gradio块作为输入来创建变量。这些变量随后被发送到一个函数中,用于格式化字符串。

问题

Gradio块创建的变量无法被后续函数接受。详细信息如下。

代码

Gradio块

这是Gradio的前端代码,旨在生成我想要的变量:

with gr.Blocks() as main:    with gr.Tab("The Workout Plan"):        with gr.Column():            with gr.Row():                age = gr.Number(label="Age"), #1 年龄                weight = gr.Number(label="Weight (lbs)"), #2 体重                 sex = gr.Dropdown( #3 性别                label="Biological Sex",                choices=["Male", "Female"]),            with gr.Column():                goal = gr.Dropdown( #4 目标                    label="What is your primary goal?",                    choices=["Hypertrophy (muscle growth)", "Power Lifting (strength increase)", "Flexibility and Mobility"]),                location = gr.Dropdown( #5 地点                    label="Where do you work out?",                    choices=["At a gym", "At home"]                ),                style = gr.Dropdown( #6 锻炼风格                    label="What type of training do you prefer?",                    choices=["Full body", "Upper-lower split", "Push-pull split", "Body-part split", "Compound exercises", "Olympic lifts"]                ),                days = gr.Slider(1, 7, value=3, step=1, label="Days per Week"), #7                workout_time = gr.Dropdown( #8                    label="How much time per workout?",                    choices=["30 minutes", "45 minutes", "60 minutes", "75 minutes", "90 minutes", "120 minutes"]                ),                warm_up = gr.Checkbox(label="Do you want a warm-up included?"), #9                stretching = gr.Checkbox(label="Do you want a stretching session after?") #10            submit_btn = gr.Button(value="Create my plan")        with gr.Column():            plan = gr.Textbox("The final plan")     with gr.Tab("Explanation"):        gr.Markdown("this is where the explination will go")

Gradio块 → 函数代码

这是我试图在其中使用这些变量的函数:

def generate_workout(age, weight, sex, goal, location, style, days, workout_time, warm_up, stretching): # 所有10个“变量”    age = int(age)        if warm_up:        warm_up =  "that includes a warmup"    else:        warm_up = ""    if stretching:        stretching = "than includes stretching"    else:        stretching = ""        prompt = f"Create a {goal} {style} workout plan for a {age}-year-old {sex} weighing {weight} lbs using {location} equipment, working out {days} days a week, where each workout is less than {workout_time}, {warm_up}, {stretching}"        return prompt

Gradio提交按钮代码

这是“submit_btn”的代码,旨在创建提示:

submit_btn.click(    generate_workout,         inputs=[            age,            weight,            sex,            goal,            location,            style,            days,            workout_time,            warm_up,            stretching                        ],            outputs=[                        plan                ]    )

错误

line 78, in <module>    submit_btn.click(  File "../lib/python3.10/site-packages/gradio/events.py", line 145, in click    dep = self.set_event_trigger(  File "../lib/python3.10/site-packages/gradio/blocks.py", line 227, in set_event_trigger    "inputs": [block._id for block in inputs],  File "../lib/python3.10/site-packages/gradio/blocks.py", line 227, in <listcomp>    "inputs": [block._id for block in inputs],AttributeError: 'tuple' object has no attribute '_id'

我期望发生的情况

我期望变量被保存,看起来它们确实被保存了,然后能够在其他函数中使用它们,并随后与Gradio按钮或事件链接。

我不确定这是Gradio的固有缺陷,还是我遗漏了Gradio Blocks文档中的某些内容。

提前感谢,
Noam


回答:

你的代码问题在于组件定义后的逗号,例如:

age = gr.Number(label="Age"), #1 年龄

由于这些是普通的Python变量声明,不应该有逗号,删除所有不必要的逗号后,你的代码就能正常工作:

age = gr.Number(label="Age") #1 年龄

原因是逗号会自动创建一个元组:

a = 3,print(type(a))>>> <class 'tuple'>

与之相反的是

a = 3print(type(a))>>> <class 'int'>

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注