common.title

Docs
Quantum Circuit
TYTAN CLOUD

QUANTUM GAMING


autoQAOA
Desktop RAG

Overview
Terms of service

Privacy policy

Contact
Research

Sign in
Sign up
common.title

Let's try using Gradio

Yuichiro Minato

2024/04/29 04:49

Gradio is useful as an interface for machine learning. Normally, various forms are used with HTML/CSS/JavaScript, but with Gradio, you can set up a web server based on Python. Let's take a look at how to use it.

For installation,

pip install gradio

Official website is,

https://www.gradio.app/

Let's start with a simple tutorial.

import gradio as gr

def greet(name, intensity):
return "Hello, " + name + "!" * int(intensity)

demo = gr.Interface(
fn=greet,
inputs=["text", "slider"],
outputs=["text"],
)

demo.launch()

When you enter 'minato' in the name on the left and increase the intensity, on the right,

Hello, minato!!!!!!!!!!!!!!!!!!!!

It appeared. I'm not used to the input and output yet, but it seems impressive once you get the hang of it.

The input and output are adjustable. You can also set up multiple inputs and outputs, which is quite easy.

import gradio as gr

def greet(name):
  return "Hello, " + name, len(name)

demo = gr.Interface(
  fn=greet,
  inputs=["text"],
  outputs=["text", "text"],
)

demo.launch()

It turned out that way. It seems easy as long as you don't get the number of inputs and outputs wrong. I wanted to create something with multiple functions on one page,

import gradio as gr

def greet(name):
  return "Hello " + name + "!"

demo1 = gr.Interface(
  fn=greet,
  inputs=["text"],
  outputs=["text"],
)

demo2 = gr.Interface(
  fn=greet,
  inputs=["text"],
  outputs=["text"],
)

gr.TabbedInterface(
  [demo1, demo2], ["demo1", "demo2"]
).launch(share=True)

I was able to solve it by creating tabs. It's quite convenient. It seems like all that's left is to make some fine adjustments, but it's quite easy to create a professional interface using only Python, which I think is very useful for development.

© 2025, blueqat Inc. All rights reserved