import gradio as gr from transformers import pipeline get_completion = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6") def translate(input_text, source, target): # source_readable = source # if source == "Auto Detect" or source.startswith("Detected"): # source, _ = auto_detect_language_code(input_text) # if source in source_lang_dict.keys(): # source = source_lang_dict[source] # target_lang_dict, _ = get_target_languages(source) try: # target = target_lang_dict[target] model = f"Helsinki-NLP/opus-mt-{source}-{target}" pipe = pipeline("translation", model=model) translation = pipe(input_text) return translation[0]['translation_text'], "" except KeyError: return "", f"Error: Translation direction {source_readable} to {target} is not supported by Helsinki Translation Models" def summarize(input): output = get_completion(input) summary_origin = output[0]['summary_text'] summary_translated = translate(summary_origin,'en','zh') return summary_origin, summary_translated demo = gr.Interface(fn=summarize, inputs=[gr.Textbox(label="Text to summarize", lines=6)], outputs=[gr.Textbox(label="Result", lines=3),gr.Textbox(label="Translate Result", lines=3)], title="Text summarization with distilbart-cnn", description="Summarize any text using the `sshleifer/distilbart-cnn-12-6` model under the hood!", examples=[ "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.", "Tower Bridge is a Grade I listed combined bascule, suspension, and, until 1960, cantilever bridge[1] in London, built between 1886 and 1894, designed by Horace Jones and engineered by John Wolfe Barry with the help of Henry Marc Brunel.[2] It crosses the River Thames close to the Tower of London and is one of five London bridges owned and maintained by the City Bridge Foundation, a charitable trust founded in 1282. The bridge was constructed to connect the 39 per cent of London's population that lived east of London Bridge, while allowing shipping to access the Pool of London between the Tower of London and London Bridge. The bridge was opened by Edward, Prince of Wales and Alexandra, Princess of Wales on 30 June 1894." ] ) demo.launch()