Skip to content

Commit

Permalink
Fix style
Browse files Browse the repository at this point in the history
  • Loading branch information
lewtun committed Mar 18, 2022
1 parent e4fa241 commit 1dfedb9
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 36 deletions.
9 changes: 7 additions & 2 deletions chapters/en/chapter9/section2.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ Let's get started with a simple “Hello World” example to get familiar with t
```py
import gradio as gr


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


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

Expand All @@ -38,9 +40,11 @@ Take a look at the example below:
```py
import gradio as gr


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


# We instantiate the Textbox class
textbox = gr.inputs.Textbox(label="Type your name here:", placeholder="John Doe", lines=2)

Expand Down Expand Up @@ -68,10 +72,11 @@ First, we define a prediction function that takes in a text prompt and returns t
```py
from transformers import pipeline

model = pipeline('text-generation')
model = pipeline("text-generation")


def predict(prompt):
completion = model(prompt)[0]['generated_text']
completion = model(prompt)[0]["generated_text"]
return completion
```

Expand Down
32 changes: 20 additions & 12 deletions chapters/en/chapter9/section3.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ shortcut `"audio"`.
import numpy as np
import gradio as gr


def reverse_audio(audio):
sr, data = audio
reversed_audio = (sr, np.flipud(data))
return reversed_audio


mic = gr.inputs.Audio(source="microphone", type="numpy", label="Speak here...")
gr.Interface(reverse_audio, mic, "audio").launch()
```
Expand Down Expand Up @@ -82,6 +84,7 @@ 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 @@ -91,6 +94,7 @@ def generate_tone(note, octave, duration):
audio = (20000 * np.sin(audio * (2 * np.pi * frequency))).astype(np.int16)
return (sr, audio)


gr.Interface(
generate_tone,
[
Expand Down Expand Up @@ -136,21 +140,25 @@ import gradio as gr

model = pipeline("automatic-speech-recognition")


def transcribe_audio(mic=None, file=None):
if mic is not None:
audio = mic
elif file is not None:
audio = file
else:
return("You must either provide a mic recording or a file")
transcription = model(audio)["text"]
return transcription
if mic is not None:
audio = mic
elif file is not None:
audio = file
else:
return "You must either provide a mic recording or a file"
transcription = model(audio)["text"]
return transcription


gr.Interface(
fn=transcribe_audio,
inputs=[gr.inputs.Audio(source="microphone", type="filepath", optional=True),
gr.inputs.Audio(source ="upload", type="filepath", optional=True)],
outputs="text",
fn=transcribe_audio,
inputs=[
gr.inputs.Audio(source="microphone", type="filepath", optional=True),
gr.inputs.Audio(source="upload", type="filepath", optional=True),
],
outputs="text",
).launch()
```

Expand Down
30 changes: 16 additions & 14 deletions chapters/en/chapter9/section4.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -83,29 +83,30 @@ import torch
import gradio as gr
from torch import nn

LABELS = Path('class_names.txt').read_text().splitlines()
LABELS = Path("class_names.txt").read_text().splitlines()

model = nn.Sequential(
nn.Conv2d(1, 32, 3, padding='same'),
nn.Conv2d(1, 32, 3, padding="same"),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 3, padding='same'),
nn.Conv2d(32, 64, 3, padding="same"),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Conv2d(64, 128, 3, padding='same'),
nn.Conv2d(64, 128, 3, padding="same"),
nn.ReLU(),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(1152, 256),
nn.ReLU(),
nn.Linear(256, len(LABELS)),
)
state_dict = torch.load('pytorch_model.bin', map_location='cpu')
state_dict = torch.load("pytorch_model.bin", map_location="cpu")
model.load_state_dict(state_dict, strict=False)
model.eval()


def predict(im):
x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.
x = torch.tensor(im, dtype=torch.float32).unsqueeze(0).unsqueeze(0) / 255.0
with torch.no_grad():
out = model(x)
probabilities = torch.nn.functional.softmax(out[0], dim=0)
Expand All @@ -116,14 +117,15 @@ def predict(im):
Now that we have a `predict()` function. The next step is to define and launch our gradio interface:
```py
interface = gr.Interface(
predict,
inputs='sketchpad',
outputs='label',
theme="huggingface",
title="Sketch Recognition",
description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!",
article = "<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
live=True)
predict,
inputs="sketchpad",
outputs="label",
theme="huggingface",
title="Sketch Recognition",
description="Who wants to play Pictionary? Draw a common object like a shovel or a laptop, and the algorithm will guess in real time!",
article="<p style='text-align: center'>Sketch Recognition | Demo Model</p>",
live=True,
)
interface.launch(share=True)
```

Expand Down
18 changes: 14 additions & 4 deletions chapters/en/chapter9/section5.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@ title = "GPT-J-6B"
description = "Gradio Demo for GPT-J 6B, a transformer model trained using Ben Wang's Mesh Transformer JAX. 'GPT-J' refers to the class of model, while '6B' represents the number of trainable parameters. To use it, simply add your text, or click one of the examples to load them. Read more at the links below."
article = "<p style='text-align: center'><a href='https://github.com/kingoflolz/mesh-transformer-jax' target='_blank'>GPT-J-6B: A 6 Billion Parameter Autoregressive Language Model</a></p>"
examples = [
['The tower is 324 metres (1,063 ft) tall,'],
["The tower is 324 metres (1,063 ft) tall,"],
["The Moon's orbit around Earth has"],
["The smooth Borealis basin in the Northern Hemisphere covers 40%"]
["The smooth Borealis basin in the Northern Hemisphere covers 40%"],
]
gr.Interface.load("huggingface/EleutherAI/gpt-j-6B", inputs=gr.inputs.Textbox(lines=5, label="Input Text"),title=title,description=description,article=article, examples=examples,enable_queue=True).launch()
gr.Interface.load(
"huggingface/EleutherAI/gpt-j-6B",
inputs=gr.inputs.Textbox(lines=5, label="Input Text"),
title=title,
description=description,
article=article,
examples=examples,
enable_queue=True,
).launch()
```

The code above will produce the interface below:
Expand All @@ -46,7 +54,9 @@ by overriding any of the
parameters. Here, we add a title and get it to work with a webcam instead:

```py
gr.Interface.load("spaces/abidlabs/remove-bg", inputs="webcam", title="Remove your webcam background!").launch()
gr.Interface.load(
"spaces/abidlabs/remove-bg", inputs="webcam", title="Remove your webcam background!"
).launch()
```

<iframe src="https://hf.space/gradioiframe/dawood/Remove-bg/+" frameBorder="0" height="550" title="Gradio app" class="container p-0 flex-grow space-iframe" allow="accelerometer; ambient-light-sensor; autoplay; battery; camera; document-domain; encrypted-media; fullscreen; geolocation; gyroscope; layout-animations; legacy-image-formats; magnetometer; microphone; midi; oversized-images; payment; picture-in-picture; publickey-credentials-get; sync-xhr; usb; vr ; wake-lock; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-popups-to-escape-sandbox allow-same-origin allow-scripts allow-downloads"></iframe>
Expand Down
8 changes: 4 additions & 4 deletions chapters/en/chapter9/section6.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ def classify_image(inp):
image = gr.inputs.Image(shape=(224, 224))
label = gr.outputs.Label(num_top_classes=3)

title="Gradio Image Classifiction + Interpretation Example"
title = "Gradio Image Classifiction + Interpretation Example"
gr.Interface(
fn=classify_image, inputs=image, outputs=label, interpretation="default",title=title
fn=classify_image, inputs=image, outputs=label, interpretation="default", title=title
).launch()
```

Expand Down Expand Up @@ -126,9 +126,9 @@ def classify_image(inp):
image = gr.inputs.Image(shape=(224, 224))
label = gr.outputs.Label(num_top_classes=3)

title="Gradio Image Classifiction + Interpretation Example"
title = "Gradio Image Classifiction + Interpretation Example"
gr.Interface(
fn=classify_image, inputs=image, outputs=label, interpretation="default",title=title
fn=classify_image, inputs=image, outputs=label, interpretation="default", title=title
).launch(auth=("admin", "pass1234"))
```

Expand Down

0 comments on commit 1dfedb9

Please sign in to comment.