#!/usr/bin/env python # coding: utf-8 # # L1: NLP tasks with a simple interface 🗞️ # Load your HF API key and relevant Python libraries. # In[1]: import os import io from IPython.display import Image, display, HTML from PIL import Image import base64 # removed dotenv and hf key requirements to see how HF Spaces handles it # In[2]: # Helper function import requests, json #Summarization endpoint from transformers import pipeline get_completion = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") def summarize(input): output = get_completion(input) return output[0]['summary_text'] # ## Building a text summarization app # Here we are using an [Inference Endpoint](https://huggingface.co/inference-endpoints) for the `shleifer/distilbart-cnn-12-6`, a 306M parameter distilled model from `facebook/bart-large-cnn`. # ### How about running it locally? # The code would look very similar if you were running it locally instead of from an API. The same is true for all the models in the rest of the course, make sure to check the [Pipelines](https://huggingface.co/docs/transformers/main_classes/pipelines) documentation page # # ```py # from transformers import pipeline # # get_completion = pipeline("summarization", model="shleifer/distilbart-cnn-12-6") # # def summarize(input): # output = get_completion(input) # return output[0]['summary_text'] # # ``` # In[3]: text = ('''The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct.''') get_completion(text) # ### Getting started with Gradio `gr.Interface` # # #### How about running it locally? # The code would look very similar if you were running it locally. Simply remove all the paramters in the launch method # # ```py # demo.launch() # ``` import gradio as gr def summarize(input): output = get_completion(input) return output[0]['summary_text'] gr.close_all() demo = gr.Interface(fn=summarize, inputs=[gr.Textbox(label="Text to summarize", lines=6)], outputs=[gr.Textbox(label="Result", lines=3)], title="Text summarization with distilbart-cnn", description="Summarize any text using the `shleifer/distilbart-cnn-12-6` model under the hood!" ) demo.launch() gr.close_all()