{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "dfabe234-2b6b-4164-a1be-1a48598d0ec3", "metadata": {}, "outputs": [], "source": [ "from transformers import pipeline" ] }, { "cell_type": "code", "execution_count": 4, "id": "6477dded-2991-4c8e-8eab-5f74f2ed9315", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'summary_text': ' Apple trees are cultivated worldwide and are the most widely grown species in the genus Malus . The tree originated in Central Asia, where its'}]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "summarization=pipeline(task=\"summarization\", model=\"sshleifer/distilbart-cnn-12-6\")\n", "text=\"An apple is a round, edible fruit produced by an apple tree (Malus spp., among them the domestic or orchard apple; Malus domestica). Apple trees are cultivated worldwide and are the most widely grown species in the genus Malus. The tree originated in Central Asia, where its wild ancestor, Malus sieversii, is still found. Apples have been grown for thousands of years in Eurasia and were introduced to North America by European colonists. Apples have religious and mythological significance in many cultures, including Norse, Greek, and European Christian tradition.\"\n", "summarization(text,min_length=5, max_length=30)" ] }, { "cell_type": "code", "execution_count": 20, "id": "535a7d70-c469-4472-af1a-e0eab7221285", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7871\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n", "To disable this warning, you can either:\n", "\t- Avoid using `tokenizers` before the fork if possible\n", "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on public URL: https://58f21d56efaf52342f.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import gradio as gr\n", "def summarize(text:str, max_length:int):\n", " if max_length<10:\n", " raise gr.Error()\n", " if text==\"\":\n", " raise gr.Error(\"Text field is empty. Please enter some text\")\n", " summary=summarization(text,min_length=10,max_length=max_length)\n", " return summary[0]['summary_text']\n", "\n", "demo = gr.Interface(fn=summarize, inputs=[gr.TextArea(label=\"Text\",placeholder=\"Enter any long text to be summarized\"),\n", " gr.Number(label=\"Words\",info=\"Enter the max word of the summary. Min 10\",minimum=10)], \n", " outputs=gr.TextArea(label=\"Summary\"), title=\"Summarization App using AI\", description=\"This app uses AI to summarize long text into your given number of words.\")\n", "demo.launch(share=True)" ] }, { "cell_type": "code", "execution_count": null, "id": "e2523f88-bdf0-49b3-a66c-910f2394ebde", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.4" } }, "nbformat": 4, "nbformat_minor": 5 }