Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aliabid94 committed Jul 25, 2022
1 parent 595357e commit 3a93202
Show file tree
Hide file tree
Showing 54 changed files with 491 additions and 874 deletions.
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
3 changes: 1 addition & 2 deletions demo/blocks_form/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@ def submit(name, age, symptoms):
[error_box, diagnosis_box, patient_summary_box],
)


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()
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()
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()
15 changes: 8 additions & 7 deletions demo/stream_frames/run.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import gradio as gr
import numpy as np

with gr.Blocks() as demo:
inp = gr.Image(source="webcam")
out = gr.Image()

def flip(im):
return np.flipud(im)
inp.stream(flip, [inp], [out])
def flip(im):
return np.flipud(im)

demo = gr.Interface(
flip,
gr.Image(source="webcam", streaming=True),
"image",
live=True
)
if __name__ == "__main__":
demo.launch()
Loading

0 comments on commit 3a93202

Please sign in to comment.