Noa*_*gel 0 block openai-api gradio
我正在尝试使用 Gradio 块作为输入来创建变量。然后,这些变量被发送到一个函数,用于格式化字符串。
\n后面的函数不接受 Gradio 块创建的变量。详细信息如下。
\n这是 Gradio“前端”Gradio 代码,旨在生成我想要的变量:
\nwith gr.Blocks() as main:\n with gr.Tab("The Workout Plan"):\n with gr.Column():\n with gr.Row():\n age = gr.Number(label="Age"), #1 Age\n weight = gr.Number(label="Weight (lbs)"), #2 weight \n sex = gr.Dropdown( #3 sex\n label="Biological Sex",\n choices=["Male", "Female"]),\n with gr.Column():\n goal = gr.Dropdown( #4 goal\n label="What is your primary goal?",\n choices=["Hypertrophy (muscle growth)", "Power Lifting (strength increase)", "Flexibility and Mobility"]),\n location = gr.Dropdown( #5 location\n label="Where do you work out?",\n choices=["At a gym", "At home"]\n ),\n style = gr.Dropdown( #6 workout style\n label="What type of training do you prefer?",\n choices=["Full body", "Upper-lower split", "Push-pull split", "Body-part split", "Compound exercises", "Olympic lifts"]\n ),\n days = gr.Slider(1, 7, value=3, step=1, label="Days per Week"), #7\n workout_time = gr.Dropdown( #8\n label="How much time per workout?",\n choices=["30 minutes", "45 minutes", "60 minutes", "75 minutes", "90 minutes", "120 minutes"]\n ),\n warm_up = gr.Checkbox(label="Do you want a warm-up included?"), #9\n stretching = gr.Checkbox(label="Do you want a stretching session after?") #10\n submit_btn = gr.Button(value="Create my plan")\n with gr.Column():\n plan = gr.Textbox("The final plan") \n with gr.Tab("Explanation"):\n gr.Markdown("this is where the explination will go")\nRun Code Online (Sandbox Code Playgroud)\n这是我尝试在其中使用这些变量的函数:
\ndef generate_workout(age, weight, sex, goal, location, style, days, workout_time, warm_up, stretching): # all 10 "variables"\n\n age = int(age)\n \n if warm_up:\n warm_up = "that includes a warmup"\n else:\n warm_up = ""\n if stretching:\n stretching = "than includes stretching"\n else:\n stretching = ""\n \n 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}"\n \n return prompt\nRun Code Online (Sandbox Code Playgroud)\n以下是用于创建提示的“submit_btn”的代码:
\nsubmit_btn.click(\n generate_workout, \n inputs=[\n age,\n weight,\n sex,\n goal,\n location,\n style,\n days,\n workout_time,\n warm_up,\n stretching\n ],\n outputs=[\n plan\n ]\n )\nRun Code Online (Sandbox Code Playgroud)\nline 78, in <module>\n submit_btn.click(\n File "../lib/python3.10/site-packages/gradio/events.py", line 145, in click\n dep = self.set_event_trigger(\n File "../lib/python3.10/site-packages/gradio/blocks.py", line 227, in set_event_trigger\n "inputs": [block._id for block in inputs],\n File "../lib/python3.10/site-packages/gradio/blocks.py", line 227, in <listcomp>\n "inputs": [block._id for block in inputs],\nAttributeError: \'tuple\' object has no attribute \'_id\'\nRun Code Online (Sandbox Code Playgroud)\n我期望变量能够被保存(它们看起来是这样),然后能够在其他函数中使用它们,以便稍后链接到 Gradio 按钮或事件。
\n我不确定这是否是 Gradio 的固有缺陷,或者我是否遗漏了Gradio Blocks Docs中的某些内容。
\n提前致谢,
\n诺姆
您的代码的问题是组件定义后面的逗号,例如:
age = gr.Number(label="Age"), #1 Age
Run Code Online (Sandbox Code Playgroud)
由于这些是正常的 python 变量声明,因此不应该存在逗号,在必要的地方删除它们可以使您的代码正常工作:
age = gr.Number(label="Age") #1 Age
Run Code Online (Sandbox Code Playgroud)
原因是逗号自动创建一个元组:
a = 3,
print(type(a))
>>> <class 'tuple'>
Run Code Online (Sandbox Code Playgroud)
与之相反
a = 3
print(type(a))
>>> <class 'int'>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3720 次 |
| 最近记录: |