Merge branch 'blocks-tests' of https://github.com/gradio-app/gradio into blocks-tests

This commit is contained in:
Ömer Faruk Özdemir 2022-03-29 19:09:47 +03:00
commit 839dcd6bb4
44 changed files with 255 additions and 222 deletions

View File

@ -17,7 +17,7 @@ def chat(message, history):
return history, history
iface = gr.Interface(
demo = gr.Interface(
chat,
["text", "state"],
["chatbot", "state"],
@ -25,4 +25,4 @@ iface = gr.Interface(
allow_flagging="never",
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -11,15 +11,15 @@ def diff_texts(text1, text2):
]
iface = gr.Interface(
demo = gr.Interface(
diff_texts,
[
gr.inputs.Textbox(
gr.Textbox(
lines=3, default="The quick brown fox jumped over the lazy dogs."
),
gr.inputs.Textbox(lines=3, default="The fast brown fox jumps over lazy dogs."),
gr.Textbox(lines=3, default="The fast brown fox jumps over lazy dogs."),
],
gr.outputs.HighlightedText(color_map={"+": "green", "-": "pink"}),
gr.HighlightedText(),
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -18,20 +18,18 @@ def recognize_digit(image):
return {str(i): prediction[i] for i in range(10)}
im = gradio.inputs.Image(
im = gradio.Image(
shape=(28, 28), image_mode="L", invert_colors=False, source="canvas"
)
iface = gr.Interface(
demo = gr.Interface(
recognize_digit,
im,
gradio.outputs.Label(num_top_classes=3),
gradio.Label(num_top_classes=3),
live=True,
interpretation="default",
capture_session=True,
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -25,18 +25,18 @@ def disease_report(img, scan_for, generate_report):
return results, report if generate_report else None
iface = gr.Interface(
demo = gr.Interface(
disease_report,
[
"image",
gr.inputs.CheckboxGroup(
gr.CheckboxGroup(
["Cancer", "Rash", "Heart Failure", "Stroke", "Diabetes", "Pneumonia"]
),
"checkbox",
],
[
gr.outputs.Carousel(["text", "image"], label="Disease"),
gr.outputs.File(label="Report"),
gr.Carousel(["text", "image"], label="Disease"),
gr.File(label="Report"),
],
title="Disease Report",
description="Upload an Xray and select the diseases to scan for.",
@ -46,4 +46,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -5,21 +5,19 @@ def filter_records(records, gender):
return records[records["gender"] == gender]
iface = gr.Interface(
demo = gr.Interface(
filter_records,
[
gr.inputs.Dataframe(
gr.Dataframe(
headers=["name", "age", "gender"],
datatype=["str", "number", "str"],
row_count=5,
),
gr.inputs.Dropdown(["M", "F", "O"]),
gr.Dropdown(["M", "F", "O"]),
],
"dataframe",
description="Enter gender as 'M', 'F', or 'O' for other.",
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -1,5 +1,3 @@
import random
import matplotlib.pyplot as plt
import numpy as np
@ -24,19 +22,19 @@ def plot_forecast(final_year, companies, noise, show_legend, point_style):
return fig
iface = gr.Interface(
demo = gr.Interface(
plot_forecast,
[
gr.inputs.Radio([2025, 2030, 2035, 2040], label="Project to:"),
gr.inputs.CheckboxGroup(
gr.Radio([2025, 2030, 2035, 2040], label="Project to:"),
gr.CheckboxGroup(
["Google", "Microsoft", "Gradio"], label="Company Selection"
),
gr.inputs.Slider(1, 100, label="Noise Level"),
gr.inputs.Checkbox(label="Show Legend"),
gr.inputs.Dropdown(["cross", "line", "circle"], label="Style"),
gr.Slider(minimum=1, maximum=100, label="Noise Level"),
gr.Checkbox(label="Show Legend"),
gr.Dropdown(["cross", "line", "circle"], label="Style"),
],
gr.outputs.Image(plot=True, label="forecast"),
gr.Image(plot=True, label="forecast"),
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -1,7 +1,5 @@
import random
import pandas as pd
import gradio as gr
@ -19,20 +17,20 @@ def fraud_detector(card_activity, categories, sensitivity):
)
iface = gr.Interface(
demo = gr.Interface(
fraud_detector,
[
gr.inputs.Timeseries(x="time", y=["retail", "food", "other"]),
gr.inputs.CheckboxGroup(
["retail", "food", "other"], default=["retail", "food", "other"]
gr.Timeseries(x="time", y=["retail", "food", "other"]),
gr.CheckboxGroup(
["retail", "food", "other"], default_selected=["retail", "food", "other"]
),
gr.inputs.Slider(1, 3),
gr.Slider(minimum=1, maximum=3),
],
[
"dataframe",
gr.outputs.Timeseries(x="time", y=["retail", "food", "other"]),
gr.outputs.Label(label="Fraud Level"),
gr.Timeseries(x="time", y=["retail", "food", "other"]),
gr.Label(label="Fraud Level"),
],
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -31,12 +31,12 @@ def interpret_gender(sentence):
return interpretation
iface = gr.Interface(
demo = gr.Interface(
fn=gender_of_sentence,
inputs=gr.inputs.Textbox(default="She went to his house to get her keys."),
inputs=gr.Textbox(default="She went to his house to get her keys."),
outputs="label",
interpretation=interpret_gender,
enable_queue=True,
interpretation=interpret_gender
)
if __name__ == "__main__":
iface.launch()
demo.launch(auth=("a", "b"))

View File

@ -1,5 +1,3 @@
import re
import gradio as gr
male_words, female_words = ["he", "his", "him"], ["she", "hers", "her"]
@ -14,11 +12,12 @@ def gender_of_sentence(sentence):
return {"male": male_count / total, "female": female_count / total}
iface = gr.Interface(
demo = gr.Interface(
fn=gender_of_sentence,
inputs=gr.inputs.Textbox(default="She went to his house to get her keys."),
inputs=gr.Textbox(default="She went to his house to get her keys."),
outputs="label",
interpretation="default",
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -15,7 +15,7 @@ def generate_tone(note, octave, duration):
return sr, audio
iface = gr.Interface(
demo = gr.Interface(
generate_tone,
[
gr.inputs.Dropdown(notes, type="index"),
@ -26,4 +26,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -8,9 +8,12 @@ examples = [
["The smooth Borealis basin in the Northern Hemisphere covers 40%"],
]
gr.Interface.load(
demo = gr.Interface.load(
"huggingface/EleutherAI/gpt-j-6B",
inputs=gr.inputs.Textbox(lines=5, label="Input Text"),
inputs=gr.Textbox(lines=5, label="Input Text"),
title=title,
examples=examples,
).launch()
)
if __name__ == "__main__":
demo.launch()

View File

@ -7,6 +7,6 @@ def greet(name):
return "Hello " + name + "!!"
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
iface.launch(auth=lambda u, p: user_db.get(u) == p)
demo.launch(auth=lambda u, p: user_db.get(u) == p)

View File

@ -5,6 +5,7 @@ def greet(name):
return "Hello " + name + "!!"
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -5,10 +5,11 @@ def greet(name):
return "Hello " + name + "!"
iface = gr.Interface(
demo = gr.Interface(
fn=greet,
inputs=gr.inputs.Textbox(lines=2, placeholder="Name Here..."),
inputs=gr.Textbox(lines=2, placeholder="Name Here..."),
outputs="text",
)
if __name__ == "__main__":
app, local_url, share_url = iface.launch()
app, local_url, share_url = demo.launch()

View File

@ -8,10 +8,10 @@ def greet(name, is_morning, temperature):
return greeting, round(celsius, 2)
iface = gr.Interface(
demo = gr.Interface(
fn=greet,
inputs=["text", "checkbox", gr.inputs.Slider(0, 100)],
inputs=["text", "checkbox", gr.Slider(0, 100)],
outputs=["text", "number"],
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -17,12 +17,15 @@ def classify_image(inp):
return {labels[i]: float(prediction[i]) for i in range(1000)}
image = gr.inputs.Image(shape=(224, 224))
label = gr.outputs.Label(num_top_classes=3)
image = gr.Image(shape=(224, 224))
label = gr.Label(num_top_classes=3)
gr.Interface(
demo = gr.Interface(
fn=classify_image,
inputs=image,
outputs=label,
examples=[["images/cheetah1.jpg"], ["images/lion.jpg"]],
).launch()
)
if __name__ == "__main__":
demo.launch()

View File

@ -20,6 +20,10 @@ def predict(inp):
return {labels[i]: float(prediction[i]) for i in range(1000)}
inputs = gr.inputs.Image()
outputs = gr.outputs.Label(num_top_classes=3)
gr.Interface(fn=predict, inputs=inputs, outputs=outputs).launch()
inputs = gr.Image()
outputs = gr.Label(num_top_classes=3)
demo = gr.Interface(fn=predict, inputs=inputs, outputs=outputs)
if __name__ == "__main__":
demo.launch()

View File

@ -17,9 +17,11 @@ def classify_image(inp):
return {labels[i]: float(prediction[i]) for i in range(1000)}
image = gr.inputs.Image(shape=(224, 224))
label = gr.outputs.Label(num_top_classes=3)
image = gr.Image(shape=(224, 224))
label = gr.Label(num_top_classes=3)
gr.Interface(
fn=classify_image, inputs=image, outputs=label, interpretation="default"
).launch()
demo = gr.Interface(fn=classify_image, inputs=image, outputs=label,
interpretation="default")
if __name__ == "__main__":
demo.launch()

View File

@ -5,6 +5,7 @@ def image_mod(image):
return image.rotate(45)
iface = gr.Interface(image_mod, gr.inputs.Image(type="pil"), "image")
demo = gr.Interface(image_mod, gr.inputs.Image(type="pil"), "image")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -4,13 +4,14 @@ import gradio as gr
def image_mod(text):
return text[::-1]
block = gr.Blocks()
demo = gr.Blocks()
with block:
with demo:
text = gr.Textbox()
btn = gr.Button("Run")
btn.click(image_mod, text, text)
print(block.get_config_file())
print(demo.get_config_file())
if __name__ == "__main__":
block.launch()
demo.launch()

View File

@ -82,48 +82,48 @@ def fn(
)
iface = gr.Interface(
demo = gr.Interface(
fn,
inputs=[
gr.inputs.Textbox(default="Lorem ipsum", label="Textbox"),
gr.inputs.Textbox(lines=3, placeholder="Type here..", label="Textbox 2"),
gr.inputs.Number(label="Number", default=42),
gr.inputs.Slider(minimum=10, maximum=20, default=15, label="Slider: 10 - 20"),
gr.inputs.Slider(maximum=20, step=0.04, label="Slider: step @ 0.04"),
gr.inputs.Checkbox(label="Checkbox"),
gr.inputs.CheckboxGroup(
label="CheckboxGroup", choices=CHOICES, default=CHOICES[0:2]
gr.Textbox(default_value="Lorem ipsum", label="Textbox"),
gr.Textbox(lines=3, placeholder="Type here..", label="Textbox 2"),
gr.Number(label="Number", default=42),
gr.Slider(minimum=10, maximum=20, default_value=15, label="Slider: 10 - 20"),
gr.Slider(maximum=20, step=0.04, label="Slider: step @ 0.04"),
gr.Checkbox(label="Checkbox"),
gr.CheckboxGroup(
label="CheckboxGroup", choices=CHOICES, default_selected=CHOICES[0:2]
),
gr.inputs.Radio(label="Radio", choices=CHOICES, default=CHOICES[2]),
gr.inputs.Dropdown(label="Dropdown", choices=CHOICES),
gr.inputs.Image(label="Image", optional=True),
gr.inputs.Image(label="Image w/ Cropper", tool="select", optional=True),
gr.inputs.Image(label="Sketchpad", source="canvas", optional=True),
gr.inputs.Image(label="Webcam", source="webcam", optional=True),
gr.inputs.Video(label="Video", optional=True),
gr.inputs.Audio(label="Audio", optional=True),
gr.inputs.Audio(label="Microphone", source="microphone", optional=True),
gr.inputs.File(label="File", optional=True),
gr.inputs.Dataframe(label="Dataframe", headers=["Name", "Age", "Gender"]),
gr.inputs.Timeseries(x="time", y=["price", "value"], optional=True),
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.outputs.Textbox(label="Textbox"),
gr.outputs.Label(label="Label"),
gr.outputs.Audio(label="Audio"),
gr.outputs.Image(label="Image"),
gr.outputs.Video(label="Video"),
gr.outputs.HighlightedText(
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.outputs.HighlightedText(label="HighlightedText", show_legend=True),
gr.outputs.JSON(label="JSON"),
gr.outputs.HTML(label="HTML"),
gr.outputs.File(label="File"),
gr.outputs.Dataframe(label="Dataframe"),
gr.outputs.Dataframe(label="Numpy", type="numpy"),
gr.outputs.Carousel("image", label="Carousel"),
gr.outputs.Timeseries(x="time", y=["price", "value"], label="Timeseries"),
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"),
],
examples=[
[
@ -156,4 +156,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -9,11 +9,10 @@ def longest_word(text):
ex = "The quick brown fox jumped over the lazy dog."
iface = gr.Interface(
demo = gr.Interface(
longest_word, "textbox", "label", interpretation="default", examples=[[ex]]
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -37,13 +37,14 @@ def main_note(audio):
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
iface = gr.Interface(
demo = gr.Interface(
main_note,
"audio",
gr.outputs.Label(num_top_classes=4),
gr.Audio(source="microphone"),
gr.Label(num_top_classes=4),
examples=[
["audio/recording1.wav"],
["audio/cantina.wav"],
@ -52,4 +53,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -7,9 +7,9 @@ def transpose(matrix):
return matrix.T
iface = gr.Interface(
demo = gr.Interface(
transpose,
gr.inputs.Dataframe(type="numpy", datatype="number", row_count=5, col_count=3),
gr.Dataframe(type="numpy", datatype="number", row_count=5, col_count=3),
"numpy",
examples=[
[np.zeros((3, 3)).tolist()],
@ -20,7 +20,5 @@ iface = gr.Interface(
],
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -27,17 +27,17 @@ def outbreak(r, month, countries, social_distancing):
return plt
iface = gr.Interface(
demo = gr.Interface(
outbreak,
[
gr.inputs.Slider(1, 4, default=3.2, label="R"),
gr.inputs.Dropdown(
gr.Slider(minimum=1, maximum=4, default_value=3.2, label="R"),
gr.Dropdown(
["January", "February", "March", "April", "May"], label="Month"
),
gr.inputs.CheckboxGroup(["USA", "Canada", "Mexico", "UK"], label="Countries"),
gr.inputs.Checkbox(label="Social Distancing?"),
gr.CheckboxGroup(["USA", "Canada", "Mexico", "UK"], label="Countries"),
gr.Checkbox(label="Social Distancing?"),
],
"plot",
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -7,18 +7,21 @@ examples = [
]
]
gr.Interface.load(
demo = gr.Interface.load(
"huggingface/deepset/roberta-base-squad2",
inputs=[
gr.inputs.Textbox(
gr.Textbox(
lines=5, label="Context", placeholder="Type a sentence or paragraph here."
),
gr.inputs.Textbox(
gr.Textbox(
lines=2,
label="Question",
placeholder="Ask a question based on the context.",
),
],
outputs=[gr.outputs.Textbox(label="Answer"), gr.outputs.Label(label="Probability")],
outputs=[gr.Textbox(label="Answer"), gr.Label(label="Probability")],
examples=examples,
).launch()
)
if __name__ == "__main__":
demo.launch()

View File

@ -8,7 +8,7 @@ def reverse_audio(audio):
return (sr, np.flipud(data))
iface = gr.Interface(reverse_audio, "microphone", "audio", examples="audio")
demo = gr.Interface(reverse_audio, "microphone", "audio", examples="audio")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -24,9 +24,9 @@ def sales_projections(employee_data):
return employee_data, plt.gcf(), regression_values
iface = gr.Interface(
demo = gr.Interface(
sales_projections,
gr.inputs.Dataframe(
gr.Dataframe(
headers=["Name", "Jan Sales", "Feb Sales", "Mar Sales"],
default=[["Jon", 12, 14, 18], ["Alice", 14, 17, 2], ["Sana", 8, 9.5, 12]],
),
@ -34,4 +34,4 @@ iface = gr.Interface(
description="Enter sales figures for employees to predict sales trajectory over year.",
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -5,14 +5,14 @@ def sentence_builder(quantity, animal, place, activity_list, morning):
return f"""The {quantity} {animal}s went to the {place} where they {" and ".join(activity_list)} until the {"morning" if morning else "night"}"""
iface = gr.Interface(
demo = gr.Interface(
sentence_builder,
[
gr.inputs.Slider(2, 20),
gr.inputs.Dropdown(["cat", "dog", "bird"]),
gr.inputs.Radio(["park", "zoo", "road"]),
gr.inputs.CheckboxGroup(["ran", "swam", "ate", "slept"]),
gr.inputs.Checkbox(label="Is it the morning?"),
gr.Slider(minimum=2, maximum=20),
gr.Dropdown(["cat", "dog", "bird"]),
gr.Radio(["park", "zoo", "road"]),
gr.CheckboxGroup(["ran", "swam", "ate", "slept"]),
gr.Checkbox(label="Is it the morning?"),
],
"text",
examples=[
@ -24,4 +24,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -13,8 +13,7 @@ def sentiment_analysis(text):
return scores
iface = gr.Interface(sentiment_analysis, "textbox", "label", interpretation="default")
demo = gr.Interface(sentiment_analysis, "textbox", "label", interpretation="default")
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -12,7 +12,7 @@ def sepia(input_img):
return sepia_img
iface = gr.Interface(sepia, gr.inputs.Image(shape=(200, 200)), "image")
demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -16,8 +16,7 @@ def spectrogram(audio):
return plt
iface = gr.Interface(spectrogram, "audio", "plot")
demo = gr.Interface(spectrogram, "audio", "plot")
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -0,0 +1,29 @@
from transformers import pipeline
import gradio as gr
asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
classifier = pipeline("text-classification")
def speech_to_text(speech):
text = asr(speech)["text"]
return text
def text_to_sentiment(text):
return classifier(text)[0]["label"]
demo = gr.Blocks()
with demo:
m = gr.Audio(type="filepath")
t = gr.Textbox()
l = gr.Label()
b1 = gr.Button("Recognize Speech")
b2 = gr.Button("Classify Sentiment")
b1.click(speech_to_text, inputs=m, outputs=t)
b2.click(text_to_sentiment, inputs=t, outputs=l)
if __name__ == "__main__":
demo.launch()

View File

@ -22,18 +22,17 @@ def stock_forecast(final_year, companies, noise, show_legend, point_style):
return fig
iface = gr.Interface(
demo = gr.Interface(
stock_forecast,
[
gr.inputs.Radio([2025, 2030, 2035, 2040], label="Project to:"),
gr.inputs.CheckboxGroup(["Google", "Microsoft", "Gradio"]),
gr.inputs.Slider(1, 100),
gr.Radio([2025, 2030, 2035, 2040], label="Project to:"),
gr.CheckboxGroup(["Google", "Microsoft", "Gradio"]),
gr.Slider(minimum=1, maximum=100),
"checkbox",
gr.inputs.Dropdown(["cross", "line", "circle"], label="Style"),
gr.Dropdown(["cross", "line", "circle"], label="Style"),
],
gr.outputs.Image(plot=True, label="forecast"),
gr.Image(plot=True, label="forecast"),
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -19,19 +19,18 @@ def tax_calculator(income, marital_status, assets):
return round(total_tax)
iface = gr.Interface(
demo = gr.Interface(
tax_calculator,
[
"number",
gr.inputs.Radio(["Single", "Married", "Divorced"]),
gr.inputs.Dataframe(
gr.Radio(["Single", "Married", "Divorced"]),
gr.Dataframe(
headers=["Item", "Cost", "Deduct"],
datatype=["str", "number", "bool"],
label="Assets Purchased this Year",
),
],
"number",
# interpretation="default", # Removed interpretation for dataframes
examples=[
[10000, "Married", [["Car", 5000, False], ["Laptop", 800, True]]],
[80000, "Single", [["Suit", 800, True], ["Watch", 1800, False]]],
@ -39,4 +38,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -26,9 +26,9 @@ def text_analysis(text):
return pos_tokens, pos_count, html
iface = gr.Interface(
demo = gr.Interface(
text_analysis,
gr.inputs.Textbox(placeholder="Enter sentence here..."),
gr.Textbox(placeholder="Enter sentence here..."),
["highlight", "key_values", "html"],
examples=[
["What a beautiful morning for a walk!"],
@ -36,6 +36,5 @@ iface = gr.Interface(
],
)
iface.test_launch()
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -1,11 +1,7 @@
import os
import numpy as np
import pandas as pd
import sklearn
from sklearn import preprocessing
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
import gradio as gr
@ -88,20 +84,20 @@ def predict_survival(passenger_class, is_male, age, company, fare, embark_point)
df = encode_age(df)
df = encode_fare(df)
pred = clf.predict_proba(df)[0]
return {"Perishes": pred[0], "Survives": pred[1]}
return {"Perishes": float(pred[0]), "Survives": float(pred[1])}
iface = gr.Interface(
demo = gr.Interface(
predict_survival,
[
gr.inputs.Dropdown(["first", "second", "third"], type="index"),
gr.Dropdown(["first", "second", "third"], type="index"),
"checkbox",
gr.inputs.Slider(0, 80),
gr.inputs.CheckboxGroup(
gr.Slider(minimum=0, maximum=80),
gr.CheckboxGroup(
["Sibling", "Child"], label="Travelling with (select all)"
),
gr.inputs.Number(),
gr.inputs.Radio(["S", "C", "Q"], type="index"),
gr.Number(),
gr.Radio(["S", "C", "Q"], type="index"),
],
"label",
examples=[
@ -113,4 +109,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -5,7 +5,7 @@ def video_flip(video):
return video
iface = gr.Interface(video_flip, gr.inputs.Video(source="webcam"), "playable_video")
demo = gr.Interface(video_flip, gr.Video(source="webcam"), "playable_video")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -7,6 +7,7 @@ def snap(image):
return np.flipud(image)
iface = gr.Interface(snap, gr.inputs.Image(source="webcam", tool=None), "image")
demo = gr.Interface(snap, gr.Image(source="webcam", tool=None), "image")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -6,7 +6,7 @@ Example: python write_config.py calculator output.json
Assumes:
- The demo_name is a folder in this directory
- The demo_name folder contains a run.py file
- The run.py which defines a Gradio Interface/Blocks instance called demo
- The run.py file defines a Gradio Interface/Blocks instance called `demo`
"""
from __future__ import annotations

View File

@ -1,4 +1,3 @@
from textwrap import indent
import gradio as gr
import random
@ -6,26 +5,26 @@ import random
xray_model = lambda diseases, img: {disease: random.random() for disease in diseases}
ct_model = lambda diseases, img: {disease: 0.1 for disease in diseases}
xray_blocks = gr.Blocks()
demo = gr.Blocks()
with xray_blocks:
gr.components.Markdown(
"""
# Detect Disease From Scan
With this model you can lorem ipsum
- ipsum 1
- ipsum 2
"""
with demo:
gr.Markdown(
"""
# Detect Disease From Scan
With this model you can lorem ipsum
- ipsum 1
- ipsum 2
"""
)
disease = gr.components.CheckboxGroup(
disease = gr.CheckboxGroup(
choices=["Covid", "Malaria", "Lung Cancer"], label="Disease to Scan For"
)
with gr.Tabs():
with gr.TabItem("X-ray"):
with gr.Row():
xray_scan = gr.components.Image()
xray_results = gr.components.JSON()
xray_scan = gr.Image()
xray_results = gr.JSON()
xray_run = gr.Button(
"Run", css={"background-color": "red", "--hover-color": "orange"}
)
@ -35,12 +34,12 @@ with xray_blocks:
with gr.TabItem("CT Scan"):
with gr.Row():
ct_scan = gr.components.Image()
ct_results = gr.components.JSON()
ct_scan = gr.Image()
ct_results = gr.JSON()
ct_run = gr.Button("Run")
ct_run.click(ct_model, inputs=[disease, ct_scan], outputs=ct_results)
overall_probability = gr.components.Textbox()
overall_probability = gr.Textbox()
print(xray_blocks.get_config_file())
xray_blocks.launch()
if __name__ == "__main__":
demo.launch()

View File

@ -17,6 +17,7 @@ def zip_to_json(file_obj):
return files
iface = gr.Interface(zip_to_json, "file", "json")
demo = gr.Interface(zip_to_json, "file", "json")
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -10,7 +10,7 @@ def zip_two_files(file1, file2):
return "tmp.zip"
iface = gr.Interface(
demo = gr.Interface(
zip_two_files,
["file", "file"],
"file",
@ -20,4 +20,4 @@ iface = gr.Interface(
)
if __name__ == "__main__":
iface.launch()
demo.launch()

View File

@ -658,6 +658,10 @@ class Interface(Blocks):
return interpretation.run_interpret(self, raw_input)
def test_launch(self) -> None:
"""
Passes a few samples through the function to test if the inputs/outputs
components are consistent with the function parameter and return values.
"""
for predict_fn in self.predict:
print("Test launch: {}()...".format(predict_fn.__name__), end=" ")
raw_input = []