Skip to content

Restructure guides #1857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
262 changes: 53 additions & 209 deletions README.md

Large diffs are not rendered by default.

10 changes: 3 additions & 7 deletions demo/blocks_essay_update/run.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import gradio as gr


def change_textbox(choice):
if choice == "short":
return gr.update(lines=2, visible=True)
return gr.update(lines=2, visible=True, value="Short story: ")
elif choice == "long":
return gr.update(lines=8, visible=True)
return gr.update(lines=8, visible=True, value="Long story...")
else:
return gr.update(visible=False)


with gr.Blocks() as demo:
radio = gr.Radio(
["short", "long", "none"], label="What kind of essay would you like to write?"
["short", "long", "none"], label="Essay Length to Write?"
)
text = gr.Textbox(lines=2, interactive=True)

radio.change(fn=change_textbox, inputs=radio, outputs=text)


if __name__ == "__main__":
demo.launch()
6 changes: 1 addition & 5 deletions demo/blocks_flipper/run.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import numpy as np
import gradio as gr

demo = gr.Blocks()


def flip_text(x):
return x[::-1]

def flip_image(x):
return np.fliplr(x)


with demo:
with gr.Blocks() as demo:
gr.Markdown("Flip text or image files using this demo.")
with gr.Tabs():
with gr.TabItem("Flip Text"):
Expand Down
13 changes: 7 additions & 6 deletions demo/blocks_form/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,26 @@
symptoms_box = gr.CheckboxGroup(["Cough", "Fever", "Runny Nose"])
submit_btn = gr.Button("Submit")

diagnosis_box = gr.Textbox(label="Diagnosis")
patient_summary_box = gr.Textbox(label="Patient Summary", visible=False)
with gr.Column(visible=False) as output_col:
diagnosis_box = gr.Textbox(label="Diagnosis")
patient_summary_box = gr.Textbox(label="Patient Summary")

def submit(name, age, symptoms):
if len(name) == 0:
return {error_box: gr.update(value="Enter name", visible=True)}
if age < 0 or age > 200:
return {error_box: gr.update(value="Enter valid age", visible=True)}
return {
output_col: gr.update(visible=True),
diagnosis_box: "covid" if "Cough" in symptoms else "flu",
patient_summary_box: gr.update(value=f"{name}, {age} y/o", visible=True)
patient_summary_box: f"{name}, {age} y/o"
}

submit_btn.click(
submit,
[name_box, age_box, symptoms_box],
[error_box, diagnosis_box, patient_summary_box],
[error_box, diagnosis_box, patient_summary_box, output_col],
)


if __name__ == "__main__":
demo.launch()
demo.launch()
15 changes: 5 additions & 10 deletions demo/blocks_hello/run.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
import gradio as gr


def update(name):
def welcome(name):
return f"Welcome to Gradio, {name}!"

demo = gr.Blocks()

with demo:
with gr.Blocks() as demo:
gr.Markdown(
"""
# Hello World!
Start typing below to see the output.
""")
inp = gr.Textbox(placeholder="What is your name?")
out = gr.Textbox()

inp.change(fn=update,
inputs=inp,
outputs=out)
inp.change(welcome, inp, out)

demo.launch()
if __name__ == "__main__":
demo.launch()
12 changes: 7 additions & 5 deletions demo/calculator/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ def calculator(num1, operation, num2):
elif operation == "divide":
return num1 / num2


demo = gr.Interface(
calculator,
[gr.Number(value=4), gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
[
"number",
gr.Radio(["add", "subtract", "multiply", "divide"]),
"number"
],
"number",
examples=[
[5, "add", 3],
[4, "divide", 2],
[-4, "multiply", 2.5],
[0, "subtract", 1.2],
],
title="test calculator",
description="heres a sample toy calculator. enjoy!",
title="Toy Calculator",
description="Here's a sample toy calculator. Enjoy!",
)

if __name__ == "__main__":
demo.launch()
9 changes: 5 additions & 4 deletions demo/calculator_live/run.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import gradio as gr


def calculator(num1, operation, num2):
if operation == "add":
return num1 + num2
Expand All @@ -11,13 +10,15 @@ def calculator(num1, operation, num2):
elif operation == "divide":
return num1 / num2


demo = gr.Interface(
calculator,
["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
[
"number",
gr.Radio(["add", "subtract", "multiply", "divide"]),
"number"
],
"number",
live=True,
)

if __name__ == "__main__":
demo.launch()
10 changes: 4 additions & 6 deletions demo/chatbot_demo/run.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
import random

import gradio as gr


def chat(message, history):
history = history or []
if message.startswith("How many"):
message = message.lower()
if message.startswith("how many"):
response = random.randint(1, 10)
elif message.startswith("How"):
elif message.startswith("how"):
response = random.choice(["Great", "Good", "Okay", "Bad"])
elif message.startswith("Where"):
elif message.startswith("where"):
response = random.choice(["Here", "There", "Somewhere"])
else:
response = "I don't know"
history.append((message, response))
return history, history


chatbot = gr.Chatbot().style(color_map=("green", "pink"))
demo = gr.Interface(
chat,
Expand Down
4 changes: 0 additions & 4 deletions demo/generate_tone/run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import numpy as np

import gradio as gr

notes = ["C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"]


def generate_tone(note, octave, duration):
sr = 48000
a4_freq, tones_from_a4 = 440, 12 * (octave - 4) + (note - 9)
Expand All @@ -14,7 +12,6 @@ def generate_tone(note, octave, duration):
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
return sr, audio


demo = gr.Interface(
generate_tone,
[
Expand All @@ -24,6 +21,5 @@ def generate_tone(note, octave, duration):
],
"audio",
)

if __name__ == "__main__":
demo.launch()
36 changes: 36 additions & 0 deletions demo/hangman/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import gradio as gr
import random

secret_word = "gradio"

with gr.Blocks() as demo:
used_letters_var = gr.Variable([])
with gr.Row() as row:
with gr.Column():
input_letter = gr.Textbox(label="Enter letter")
btn = gr.Button("Guess Letter")
with gr.Column():
hangman = gr.Textbox(
label="Hangman",
value="_"*len(secret_word)
)
used_letters_box = gr.Textbox(label="Used Letters")

def guess_letter(letter, used_letters):
used_letters.append(letter)
answer = "".join([
(letter if letter in used_letters else "_")
for letter in secret_word
])
return {
used_letters_var: used_letters,
used_letters_box: ", ".join(used_letters),
hangman: answer
}
btn.click(
guess_letter,
[input_letter, used_letters_var],
[used_letters_var, used_letters_box, hangman]
)
if __name__ == "__main__":
demo.launch()
13 changes: 13 additions & 0 deletions demo/hello_blocks/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import gradio as gr

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

with gr.Blocks() as demo:
name = gr.Textbox(label="Name")
output = gr.Textbox(label="Output Box")
greet_btn = gr.Button("Greet")
greet_btn.click(fn=greet, inputs=name, outputs=output)

if __name__ == "__main__":
demo.launch()
Binary file added demo/hello_blocks/screenshot.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 1 addition & 3 deletions demo/hello_world/run.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import gradio as gr


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

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

if __name__ == "__main__":
demo.launch()
3 changes: 0 additions & 3 deletions demo/hello_world_2/run.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import gradio as gr


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


demo = gr.Interface(
fn=greet,
inputs=gr.Textbox(lines=2, placeholder="Name Here..."),
outputs="text",
)

if __name__ == "__main__":
demo.launch()
4 changes: 1 addition & 3 deletions demo/hello_world_3/run.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import gradio as gr


def greet(name, is_morning, temperature):
salutation = "Good morning" if is_morning else "Good evening"
greeting = "%s %s. It is %s degrees today" % (salutation, name, temperature)
greeting = f"{salutation} {name}. It is {temperature} degrees today"
celsius = (temperature - 32) * 5 / 9
return greeting, round(celsius, 2)


demo = gr.Interface(
fn=greet,
inputs=["text", "checkbox", gr.Slider(0, 100)],
Expand Down
15 changes: 15 additions & 0 deletions demo/reversible_flow/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import gradio as gr

def increase(num):
return num + 1

with gr.Blocks() as demo:
a = gr.Number(label="a")
b = gr.Number(label="b")
btoa = gr.Button("a > b")
atob = gr.Button("b > a")
atob.click(increase, a, b)
btoa.click(increase, b, a)

if __name__ == "__main__":
demo.launch()
Binary file added demo/rows_and_columns/images/cheetah.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions demo/rows_and_columns/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import gradio as gr

with gr.Blocks() as demo:
with gr.Row():
with gr.Column():
text1 = gr.Textbox(label="prompt 1")
text2 = gr.Textbox(label="prompt 2")
with gr.Column():
img1 = gr.Image("images/cheetah.jpg")
btn = gr.Button("Go").style(full_width=True)
if __name__ == "__main__":
demo.launch()
16 changes: 16 additions & 0 deletions demo/score_tracker/run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import gradio as gr

scores = []

def track_score(score):
scores.append(score)
top_scores = sorted(scores, reverse=True)[:3]
return top_scores

demo = gr.Interface(
track_score,
gr.Number(label="Score"),
gr.JSON(label="Top Scores")
)
if __name__ == "__main__":
demo.launch()
12 changes: 5 additions & 7 deletions demo/sepia_filter/run.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import numpy as np

import gradio as gr


def sepia(input_img):
sepia_filter = np.array(
[[0.393, 0.769, 0.189], [0.349, 0.686, 0.168], [0.272, 0.534, 0.131]]
)
sepia_filter = np.array([
[0.393, 0.769, 0.189],
[0.349, 0.686, 0.168],
[0.272, 0.534, 0.131]
])
sepia_img = input_img.dot(sepia_filter.T)
sepia_img /= sepia_img.max()
return sepia_img


demo = gr.Interface(sepia, gr.Image(shape=(200, 200)), "image")

if __name__ == "__main__":
demo.launch()
Loading