import tensorflow as tf import gradio as gr inception_net = tf.keras.applications.MobileNetV2() # load the model # Download human-readable labels for ImageNet. response = requests.get("https://git.io/JJkYN") labels = response.text.split("\n") def classify_image(inp): inp = inp.reshape((-1, 224, 224, 3)) inp = tf.keras.applications.mobilenet_v2.preprocess_input(inp) prediction = inception_net.predict(inp).flatten() return {labels[i]: float(prediction[i]) for i in range(1000)} image = gr.Image(shape=(224, 224)) label = gr.Label(num_top_classes=3) gr.Interface( fn=classify_image, inputs=image, outputs=label, examples=[ os.path.join(os.path.dirname(__file__), "images/cheetah1.jpg"), os.path.join(os.path.dirname(__file__), "images/lion.jpg") ] ).launch()
import requests import torch from PIL import Image from torchvision import transforms import gradio as gr model = torch.hub.load("pytorch/vision:v0.6.0", "resnet18", pretrained=True).eval() # Download human-readable labels for ImageNet. response = requests.get("https://git.io/JJkYN") labels = response.text.split("\n") def predict(inp): inp = Image.fromarray(inp.astype("uint8"), "RGB") inp = transforms.ToTensor()(inp).unsqueeze(0) with torch.no_grad(): prediction = torch.nn.functional.softmax(model(inp)[0], dim=0) return {labels[i]: float(prediction[i]) for i in range(1000)} inputs = gr.Image() outputs = gr.Label(num_top_classes=3) demo = gr.Interface(fn=predict, inputs=inputs, outputs=outputs) if __name__ == "__main__": demo.launch()
if column not in categories ] if len(drop_columns): card_activity.drop(columns=drop_columns, inplace=True) return ( card_activity, card_activity, { "fraud": activity_range / 100.0, "not fraud": 1 - activity_range / 100.0 }, ) demo = gr.Interface( fraud_detector, [ gr.Timeseries(x="time", y=["retail", "food", "other"]), gr.CheckboxGroup(["retail", "food", "other"], default_selected=["retail", "food", "other"]), gr.Slider(minimum=1, maximum=3), ], [ "dataframe", gr.Timeseries(x="time", y=["retail", "food", "other"]), gr.Label(label="Fraud Level"), ], ) if __name__ == "__main__": demo.launch()
gr.Radio(label="Radio", choices=CHOICES, default_selected=CHOICES[2]), gr.Dropdown(label="Dropdown", choices=CHOICES), gr.Image(label="Image"), gr.Image(label="Image w/ Cropper", tool="select"), gr.Image(label="Sketchpad", source="canvas"), gr.Image(label="Webcam", source="webcam"), gr.Video(label="Video"), gr.Audio(label="Audio"), gr.Audio(label="Microphone", source="microphone"), gr.File(label="File"), gr.Dataframe(label="Dataframe", headers=["Name", "Age", "Gender"]), gr.Timeseries(x="time", y=["price", "value"]), ], outputs=[ gr.Textbox(label="Textbox"), gr.Label(label="Label"), gr.Audio(label="Audio"), gr.Image(label="Image"), gr.Video(label="Video"), gr.HighlightedText( label="HighlightedText", color_map={"punc": "pink", "test 0": "blue"} ), gr.HighlightedText(label="HighlightedText", show_legend=True), gr.JSON(label="JSON"), gr.HTML(label="HTML"), gr.File(label="File"), gr.Dataframe(label="Dataframe"), gr.Dataframe(label="Numpy"), gr.Carousel(components="image", label="Carousel"), gr.Timeseries(x="time", y=["price", "value"], label="Timeseries"), ],
import gradio as gr with gr.Blocks() as demo: txt = gr.Textbox(label="Small Textbox", lines=1) txt = gr.Textbox(label="Large Textbox", lines=5) num = gr.Number(label="Number") check = gr.Checkbox(label="Checkbox") check_g = gr.CheckboxGroup(label="Checkbox Group", choices=["One", "Two", "Three"]) radio = gr.Radio(label="Radio", choices=["One", "Two", "Three"]) drop = gr.Dropdown(label="Dropdown", choices=["One", "Two", "Three"]) slider = gr.Slider(label="Slider") audio = gr.Audio() video = gr.Video() image = gr.Image() ts = gr.Timeseries() df = gr.Dataframe() html = gr.HTML() json = gr.JSON() md = gr.Markdown() label = gr.Label() highlight = gr.HighlightedText() # layout components are static only # carousel doesn't work like other components # carousel = gr.Carousel() if __name__ == "__main__": demo.launch()
yf = fft(y) yf2 = 2.0 / N * np.abs(yf[0:N // 2]) xf = np.linspace(0.0, 1.0 / (2.0 * T), N // 2) volume_per_pitch = {} total_volume = np.sum(yf2) for freq, volume in zip(xf, yf2): if freq == 0: continue pitch = get_pitch(freq) if pitch not in volume_per_pitch: volume_per_pitch[pitch] = 0 volume_per_pitch[pitch] += 1.0 * volume / total_volume volume_per_pitch = {k: float(v) for k, v in volume_per_pitch.items()} return volume_per_pitch demo = gr.Interface( main_note, gr.Audio(source="microphone"), gr.Label(num_top_classes=4), examples=[ ["audio/recording1.wav"], ["audio/cantina.wav"], ], interpretation="default", ) if __name__ == "__main__": demo.launch()
import gradio import gradio as gr urlretrieve("https://gr-models.s3-us-west-2.amazonaws.com/mnist-model.h5", "mnist-model.h5") model = tf.keras.models.load_model("mnist-model.h5") def recognize_digit(image): image = image.reshape(1, -1) prediction = model.predict(image).tolist()[0] return {str(i): prediction[i] for i in range(10)} im = gradio.Image(shape=(28, 28), image_mode="L", invert_colors=False, source="canvas") demo = gr.Interface( recognize_digit, im, gradio.Label(num_top_classes=3), live=True, interpretation="default", capture_session=True, ) if __name__ == "__main__": demo.launch()
import gradio as gr examples = [[ "The Amazon rainforest is a moist broadleaf forest that covers most of the Amazon basin of South America", "Which continent is the Amazon rainforest in?", ]] demo = gr.Interface.load( "huggingface/deepset/roberta-base-squad2", inputs=[ gr.Textbox(lines=5, label="Context", placeholder="Type a sentence or paragraph here."), gr.Textbox( lines=2, label="Question", placeholder="Ask a question based on the context.", ), ], outputs=[gr.Textbox(label="Answer"), gr.Label(label="Probability")], examples=examples, ) if __name__ == "__main__": demo.launch()