<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Gradio and HuggingFace
In this demo, we show how to build ready to deploy or use deep learning models. 
Hugging Face hosts thousands of pre-trained models in Model Hub. They also built high-level APIs so we can easily use and deploy pre-trained models using Pipeline. 
gradio provides APIs so we can easily build web applications that use our pre-trained models from Hugging Face. gradio also provides APIs so we can easily incorporate input and output web UIs.
After building the gradio, we can have permanent hosting using Hugging Face Spaces. 
Let us first install Hugging Face transformers and gradio.
Note
Step1:  Hello world in gradio
As a tradition, let us build the simplest gradio app. It accepts a text input and calls the greet() function to process this input and convert into another text. The output of greet() becomes the output of the gradio app.
To see our application, we call launch() after constructing our gradio Interface.
Step2:  Object Recognition using ResNet18
In our discussion about PyTorch, we used a pre-trained ResNet18 model from torchvision. We use jupyter notebook to show the results. The jupyter notebook is not an application that we can deploy and other people use with ease. The same with Google's colab. 
In this example, we use gradio to build a simple app that an end user can easily interact with. We reuse the code from our previous example.
Step3:  Using HuggingFace and Gradio
Loading a pre-trained model from torchvision, pre-processing the input, and post processing the output are all messy. Sometimes, we just want to load and use a machine learning model. Hugging Face provides a shortcut for all these steps through the use of pipeline. In pipeline, we supply the task name and the pre-trained model that is stored in Hugging Face Model Hub.
In this example, we use a much better model compared to ResNet18. It is called BEIT and can classify objects up to about 22k categories. We construct the gradio app by calling from_pipeline().
Step4:  Automatic Speech Recognition (ASR)
Let us shift to audio or speech domain. In this example, we demonstrate an Automatic Speech Recognition (ASR) system. We will use our microphone to record audio which is then converted to text using ASR. In this example, best to open the application in another browser tab by setting inbrowser=True.
Before running the gradio app, this ASR requires sentencepice module. Let us install it first.
Step5:  In this ASR, we use Facebook S2T, a transformer-based speech to text model that is trained on librispeech dataset.
Step6:  Text to Speech (TTS)
Let us do the reverse of ASR or Text to Speech (TTS). In this example, we supply text and this text is converted to speech using the voice of Linda Johnson. We use a pre-trained model of FastSpeech2 that is provided by Facebook in Model Hub.
In this example, we use load() method to load the pre-trained model.
Step7:  Text Generation using GPT2
In this example, we use a large language model (LLM) by OpenAI to generate text. It is called GPT2. Text generation is one of the tasks where a language model can help us. Basically, we provide the initial text made of few words. Then, the model will continue it.
Step8:  Using gr.Series() to automatically chain the I/O of multiple models
In this example, we use the previous text generator as input to our TTS. We can for example use to generate a podcast on a certain topic. In this case, we replace GPT2 with a much better model called GPT Neo.
<ASSISTANT_TASK:>
Python Code:
!pip install transformers
!pip install gradio
Explanation: Gradio and HuggingFace
In this demo, we show how to build ready to deploy or use deep learning models. 
Hugging Face hosts thousands of pre-trained models in Model Hub. They also built high-level APIs so we can easily use and deploy pre-trained models using Pipeline. 
gradio provides APIs so we can easily build web applications that use our pre-trained models from Hugging Face. gradio also provides APIs so we can easily incorporate input and output web UIs.
After building the gradio, we can have permanent hosting using Hugging Face Spaces. 
Let us first install Hugging Face transformers and gradio.
Note: For some examples, it is best to launch the app in another tab to enable access to the required inputs such as microphone or webcam. Running the app may also lock the python kernel and the notebook becomes unresponsive. In that case, please restart the kernel and clear the output.
End of explanation
import gradio as gr
def greet(name):
    return "Hello " + name + "!!"
gr.Interface(fn=greet, inputs="text", outputs="text").launch()
Explanation: Hello world in gradio
As a tradition, let us build the simplest gradio app. It accepts a text input and calls the greet() function to process this input and convert into another text. The output of greet() becomes the output of the gradio app.
To see our application, we call launch() after constructing our gradio Interface.
End of explanation
import gradio as gr
import torch 
import torchvision
import torchvision.transforms as transforms
import requests
from einops import rearrange
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406],
                                 std=[0.229, 0.224, 0.225])
resnet = torchvision.models.resnet18(pretrained=True)
resnet.eval()
# Download human-readable labels for ImageNet.
response = requests.get("https://git.io/JJkYN")
labels = response.text.split("\n")
def classify(img):
    # By default, gradio image is numpy
    img = torch.from_numpy(img)
    # Numpy image is channel last. PyTorch is channel 1st.
    img = rearrange(img, 'h w c -> c h w')
    
    # The transforms before prediction
    img = torchvision.transforms.Resize(256)(img)
    img = torchvision.transforms.CenterCrop(224)(img).float()/255.
    img = normalize(img)
    
    # We insert batch size of 1
    img = rearrange(img, 'c h w  -> 1 c h w')
    
    # The actual prediction
    with torch.no_grad():
        pred = resnet(img)
    
    # Convert the prediction to probabilities
    pred = torch.nn.functional.softmax(pred, dim=1)
    # Remove the batch dim. torch.squeeze() can also be used.
    pred = rearrange(pred, "1 j->j")
    
    # torch to numpy space
    pred = pred.cpu().numpy()
    
    return {labels[i]: float(pred[i]) for i in range(1000)}
    
gr.Interface(fn=classify, 
             inputs=gr.inputs.Image(shape=(224, 224)),
             outputs=gr.outputs.Label(num_top_classes=5),
             title="1k Object Recognition",
             examples=['wonder_cat.jpg', 'aki_dog.jpg',],
             description="Demonstrates a pre-trained model from torchvision for image classification.",
             allow_flagging="never").launch(inbrowser=True)
Explanation: Object Recognition using ResNet18
In our discussion about PyTorch, we used a pre-trained ResNet18 model from torchvision. We use jupyter notebook to show the results. The jupyter notebook is not an application that we can deploy and other people use with ease. The same with Google's colab. 
In this example, we use gradio to build a simple app that an end user can easily interact with. We reuse the code from our previous example.
End of explanation
import gradio as gr
from transformers import pipeline
pipe = pipeline(task="image-classification", 
                 # model that can do 22k-category classification
                 model="microsoft/beit-base-patch16-224-pt22k-ft22k")
gr.Interface.from_pipeline(pipe, 
                           title="22k Image Classification",
                           description="Object Recognition using Microsoft BEIT",
                           examples = ['wonder_cat.jpg', 'aki_dog.jpg',],
                           allow_flagging="never").launch(inbrowser=True)
Explanation: Using HuggingFace and Gradio
Loading a pre-trained model from torchvision, pre-processing the input, and post processing the output are all messy. Sometimes, we just want to load and use a machine learning model. Hugging Face provides a shortcut for all these steps through the use of pipeline. In pipeline, we supply the task name and the pre-trained model that is stored in Hugging Face Model Hub.
In this example, we use a much better model compared to ResNet18. It is called BEIT and can classify objects up to about 22k categories. We construct the gradio app by calling from_pipeline().
End of explanation
!pip install sentencepiece
Explanation: Automatic Speech Recognition (ASR)
Let us shift to audio or speech domain. In this example, we demonstrate an Automatic Speech Recognition (ASR) system. We will use our microphone to record audio which is then converted to text using ASR. In this example, best to open the application in another browser tab by setting inbrowser=True.
Before running the gradio app, this ASR requires sentencepice module. Let us install it first.
End of explanation
import gradio as gr
from transformers import pipeline
pipe = pipeline(task="automatic-speech-recognition", 
                model="facebook/s2t-medium-librispeech-asr")
gr.Interface.from_pipeline(pipe,
                           title="Automatic Speech Recognition (ASR)",
                           description="Using pipeline with Facebook S2T for ASR.",
                           examples=['data/ljspeech.wav',],
                           ).launch(inbrowser=True)
Explanation: In this ASR, we use Facebook S2T, a transformer-based speech to text model that is trained on librispeech dataset.
End of explanation
import gradio as gr
gr.Interface.load("huggingface/facebook/fastspeech2-en-ljspeech", 
                  description="TTS using FastSpeech2",
                  title="Text to Speech (TTS)",
                  examples=[["The quick brown fox jumps over the lazy dog."]]
                  ).launch()
Explanation: Text to Speech (TTS)
Let us do the reverse of ASR or Text to Speech (TTS). In this example, we supply text and this text is converted to speech using the voice of Linda Johnson. We use a pre-trained model of FastSpeech2 that is provided by Facebook in Model Hub.
In this example, we use load() method to load the pre-trained model.
End of explanation
import gradio as gr
gr.Interface.load("huggingface/gpt2",
                  title="Text Generation",
                  description="Using GPT2.",
                  allow_flagging="never").launch(inbrowser=True)
Explanation: Text Generation using GPT2
In this example, we use a large language model (LLM) by OpenAI to generate text. It is called GPT2. Text generation is one of the tasks where a language model can help us. Basically, we provide the initial text made of few words. Then, the model will continue it.
End of explanation
import gradio as gr
textgen = gr.Interface.load("huggingface/EleutherAI/gpt-neo-2.7B”)
tts = gr.Interface.load("huggingface/facebook/fastspeech2-en-ljspeech”)
iface = gr.Series(textgen, tts)
iface.title = "Generated Text to Speech"
iface.allow_flagging = "never"
iface.launch(inbrowser=True)
Explanation: Using gr.Series() to automatically chain the I/O of multiple models
In this example, we use the previous text generator as input to our TTS. We can for example use to generate a podcast on a certain topic. In this case, we replace GPT2 with a much better model called GPT Neo.
End of explanation
<END_TASK>