如何将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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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