Team Ai
Apppublic

RemotelyBest/RemotelyBest_Development_of_AI_Applications

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
roberta-summarization.ipynb192 linesDownload Raw Back to Other Codes
1{2 "cells": [3  {4   "cell_type": "code",5   "execution_count": 1,6   "metadata": {},7   "outputs": [8    {9     "name": "stderr",10     "output_type": "stream",11     "text": [12      "c:\\Users\\panuk\\anaconda3\\envs\\SolutionsInPR\\Lib\\site-packages\\transformers\\tokenization_utils_base.py:1617: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be deprecated in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884\n",13      "  warnings.warn(\n"14     ]15    }16   ],17   "source": [18    "# Load model directly\n",19    "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n",20    "\n",21    "tokenizer = AutoTokenizer.from_pretrained(\"facebook/bart-large-cnn\")\n",22    "model = AutoModelForSeq2SeqLM.from_pretrained(\"facebook/bart-large-cnn\")"23   ]24  },25  {26   "cell_type": "code",27   "execution_count": 2,28   "metadata": {},29   "outputs": [30    {31     "data": {32      "text/plain": [33       "BartForConditionalGeneration(\n",34       "  (model): BartModel(\n",35       "    (shared): BartScaledWordEmbedding(50264, 1024, padding_idx=1)\n",36       "    (encoder): BartEncoder(\n",37       "      (embed_tokens): BartScaledWordEmbedding(50264, 1024, padding_idx=1)\n",38       "      (embed_positions): BartLearnedPositionalEmbedding(1026, 1024)\n",39       "      (layers): ModuleList(\n",40       "        (0-11): 12 x BartEncoderLayer(\n",41       "          (self_attn): BartSdpaAttention(\n",42       "            (k_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",43       "            (v_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",44       "            (q_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",45       "            (out_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",46       "          )\n",47       "          (self_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",48       "          (activation_fn): GELUActivation()\n",49       "          (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",50       "          (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",51       "          (final_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",52       "        )\n",53       "      )\n",54       "      (layernorm_embedding): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",55       "    )\n",56       "    (decoder): BartDecoder(\n",57       "      (embed_tokens): BartScaledWordEmbedding(50264, 1024, padding_idx=1)\n",58       "      (embed_positions): BartLearnedPositionalEmbedding(1026, 1024)\n",59       "      (layers): ModuleList(\n",60       "        (0-11): 12 x BartDecoderLayer(\n",61       "          (self_attn): BartSdpaAttention(\n",62       "            (k_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",63       "            (v_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",64       "            (q_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",65       "            (out_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",66       "          )\n",67       "          (activation_fn): GELUActivation()\n",68       "          (self_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",69       "          (encoder_attn): BartSdpaAttention(\n",70       "            (k_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",71       "            (v_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",72       "            (q_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",73       "            (out_proj): Linear(in_features=1024, out_features=1024, bias=True)\n",74       "          )\n",75       "          (encoder_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",76       "          (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n",77       "          (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n",78       "          (final_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",79       "        )\n",80       "      )\n",81       "      (layernorm_embedding): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n",82       "    )\n",83       "  )\n",84       "  (lm_head): Linear(in_features=1024, out_features=50264, bias=False)\n",85       ")"86      ]87     },88     "execution_count": 2,89     "metadata": {},90     "output_type": "execute_result"91    }92   ],93   "source": [94    "import torch\n",95    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",96    "model.to(device)"97   ]98  },99  {100   "cell_type": "code",101   "execution_count": 3,102   "metadata": {},103   "outputs": [104    {105     "name": "stdout",106     "output_type": "stream",107     "text": [108      "Running on local URL:  http://127.0.0.1:7861\n"109     ]110    },111    {112     "name": "stderr",113     "output_type": "stream",114     "text": [115      "c:\\Users\\panuk\\anaconda3\\envs\\SolutionsInPR\\Lib\\site-packages\\gradio\\analytics.py:106: UserWarning: IMPORTANT: You are using gradio version 4.44.1, however version 5.0.1 is available, please upgrade. \n",116      "--------\n",117      "  warnings.warn(\n"118     ]119    },120    {121     "name": "stdout",122     "output_type": "stream",123     "text": [124      "Running on public URL: https://1fe44b84e4bdd88e83.gradio.live\n",125      "\n",126      "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"127     ]128    },129    {130     "data": {131      "text/html": [132       "<div><iframe src=\"https://1fe44b84e4bdd88e83.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"133      ],134      "text/plain": [135       "<IPython.core.display.HTML object>"136      ]137     },138     "metadata": {},139     "output_type": "display_data"140    },141    {142     "data": {143      "text/plain": []144     },145     "execution_count": 3,146     "metadata": {},147     "output_type": "execute_result"148    }149   ],150   "source": [151    "\n",152    "def summarize(text):\n",153    "    inputs = tokenizer([text], max_length=1024, return_tensors=\"pt\")\n",154    "    summary_ids = model.generate(inputs[\"input_ids\"], num_beams=2, min_length=0, max_length=100)\n",155    "    return tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]\n",156    "\n",157    "import gradio as gr\n",158    "\n",159    "iface = gr.Interface(\n",160    "    fn=summarize,\n",161    "    inputs=gr.Textbox(label=\"Text to summarize\"),\n",162    "    outputs=[gr.Textbox(label=\"Summary\")],\n",163    "    title='Summarize text'\n",164    ")\n",165    "\n",166    "iface.launch(share=True)"167   ]168  }169 ],170 "metadata": {171  "kernelspec": {172   "display_name": "SolutionsInPR",173   "language": "python",174   "name": "python3"175  },176  "language_info": {177   "codemirror_mode": {178    "name": "ipython",179    "version": 3180   },181   "file_extension": ".py",182   "mimetype": "text/x-python",183   "name": "python",184   "nbconvert_exporter": "python",185   "pygments_lexer": "ipython3",186   "version": "3.12.3"187  }188 },189 "nbformat": 4,190 "nbformat_minor": 2191}192