In [1]:
import gradio as gr
from transformers import pipeline
import ffmpeg

Gradio is an open-source Python library that is used to build machine learning and data science demos and web applications.

In [2]:
### Text generation

#Firstly, define a function
def predict(prompt):
    model = pipeline("text-generation")
    completion = model(prompt)[0]["generated_text"]
    return completion

#Secondly, create an 'interface'
textbox = gr.Textbox(label="Type your sentence here:", placeholder="Hi, my name is John Doe.", lines=2)
demo = gr.Interface(fn=predict, inputs=textbox, outputs="text")

#Then launch
Running on local URL:

To create a public link, set `share=True` in `launch()`.
(<gradio.routes.App at 0x7fad38913fd0>, '', None)
In [3]:
### Audio transcription

#Firstly, define a funtion
def transcribe_audio(mic=None, file=None):
    model = pipeline("automatic-speech-recognition")
    if mic is not None:
        audio = mic
    elif file is not None:
        audio = file
        return "You must either provide a mic recording or a file"
    transcription = model(audio)["text"]
    return transcription

#Secondly, create an 'interface'
demo2 = gr.Interface(
        gr.Audio(source="microphone", type="filepath"),
        gr.Audio(source="upload", type="filepath"),

#Then launch
Running on local URL:

To create a public link, set `share=True` in `launch()`.
(<gradio.routes.App at 0x7fad38566a90>, '', None)
In [4]:
### Prediction of bioassay with Random Forest example
# Input: Cell Painting features extracted by CellProfiler
# Output: Activity ("Active", or "Inactive") of ChEMBL assay CHEMBL1741322
# Assay description: Cytochrome panel assay. Compounds with AC50 equal or less than 10 uM are considered active

import pandas as pd
import numpy as np
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split

dataset = pd.read_csv("toy_dataset_Cytochrome.csv")
X_train, X_test, y_train, y_test = train_test_split(dataset.iloc[:,0:-1], dataset["CHEMBL1741322"], test_size=0.1)

clf = RandomForestClassifier(max_depth=2, random_state=0), y_train.values)

np.savez("CP_predict.npz", X=X_test.iloc[[1,11,67,109,222],:].values, y=y_test.iloc[[1,11,67,109,222]].values)
In [5]:

def predict(cp_profile):
    features = np.load(cp_profile)["X"]
    true_label = np.load(cp_profile)["y"]
    pred = clf.predict(features)
    pred = ["Active" if i==1 else "Inactive" for i in pred]
    return pred#, true_label
textbox = gr.Textbox(label="Path to Cell Painting features here:", lines=2, placeholder="CP_predict.npz")
demo3 = gr.Interface(fn=predict, inputs=textbox, outputs="text")

Running on local URL:

To create a public link, set `share=True` in `launch()`.
(<gradio.routes.App at 0x7fac87e27d90>, '', None)
No model was supplied, defaulted to gpt2 (
The attention mask and the pad token id were not set. As a consequence, you may observe unexpected behavior. Please pass your input's `attention_mask` to obtain reliable results.
Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.