Open In App

Glucose Test Values Interpreter GUI Using Gradio in Python

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

This article is going to create a basic GUI using the Python package Gradio to interpret Glucose Test Values to infer appropriate results.

Gradio is an open-source library that helps programmers effortlessly create User Interface(UI) components for functions with just a few lines of code.
Given the glucose test values of a patient in mg/dl, the task is to check whether the patient has a normal blood glucose level or not. To accomplish this task, one of Python’s latest libraries named Gradio is used. 

Understanding the Problem

Two parameters have to be passed to a function that checks the normalcy of blood glucose levels:

  1. are_you_diabetic stores the input from the user whether he/she is diabetic or not in String type.
  2. glucoselevel stores the glucose level input from the user (assumed to be given in mg/dl for convenience)

Now, based on these two parameters, the following conclusions are reached by the function:
(let d be initialized with are_you_diabetic for ease of understanding)

  1. f d is ”no” and if the glucoselevel ranges from 70-130 mg/dl then the following message is displayed:
    “As a non-diabetic patient, you have a normal blood glucose level”
  2. If d is “no” and if the glucoselevel does not lie in the range of 70-130 mg/dl then the following message is displayed:
    “As a non-diabetic patient, you have an abnormal blood glucose level”
  3. If d is “yes” and if the glucoselevel ranges from 90-130 mg/dl then the following message is displayed:
    “As a diabetic patient, you have a normal blood glucose level”
  4. If d is “yes” and if the glucoselevel does not lie between 90-30 mg/dl then display the message:
    “As a diabetic patient, you have an abnormal blood glucose level”

Install the required Packages

First and foremost we need to add the gradio package to our system. To install the gradio run the following command in the terminal:

pip install gradio

Algorithm

In order to check whether the patient has a normal blood glucose level or not, the succeeding steps are followed:

  1. Import Gradio as gr
  2. Create a function glucosetest that accepts are_you_diabetic and glucoselevel as parameters
  3. within glucosetest(), follow the interpretation of inputs given above by creating nested if-else and return the messages accordingly
  4. Create Gradio Interface using the ‘interface’ variable and specify the input and output types.
  5. Launch the above interface to obtain the “Interpreting Glucose Test Values” UI(User Interface).

The following code implements the above strategy:

Python




# importing gradio
import gradio as gr
  
# creating glucosetest() with are_you_diabetic and 
# glucoselevel as parameters
def glucosetest(are_you_diabetic, glucoselevel):
    # assigning value of are_you_diabetic to d
    d = are_you_diabetic
  
    # variable to store the result to be returned
    res = ''
  
    # checking the various conditions and 
    # returning the results accordingly
    if d == "no":
        res += 'As a non-diabetic patient, '
        if glucoselevel >= 70 and glucoselevel <= 130:
            res += 'you have a normal blood glucose level.'
        else:
            res += 'you have a abnormal blood glucose level.'
    else:
        res += 'As a diabetic patient, '
        if glucoselevel >= 90 and glucoselevel <= 130:
            res += 'you have a normal blood glucose level.'
        else:
            res += 'you have a abnormal blood glucose level.'
  
    return res
  
  
# creating an interface
interface = gr.Interface(fn=glucosetest,
inputs=[gr.Dropdown(["no", "yes"], value='no'), 
"number"], outputs="text"
title="GLUCOSE TEST VALUE INTERPRETER")
  
# launching an interface
interface.launch()


Output:

When interface.launch() is executed then the following link appears:

the output of the interface.launch() method of Gradio

the output of the interface.launch() method of Gradio

When this link is clicked, then the interface is displayed and the input values are provided accordingly as can be seen below:

Resulting Gradio GUI

Resulting Gradio GUI



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads