{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "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", " warnings.warn(\n" ] } ], "source": [ "# Load model directly\n", "from transformers import AutoTokenizer, AutoModelForSeq2SeqLM\n", "\n", "tokenizer = AutoTokenizer.from_pretrained(\"facebook/bart-large-cnn\")\n", "model = AutoModelForSeq2SeqLM.from_pretrained(\"facebook/bart-large-cnn\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "BartForConditionalGeneration(\n", " (model): BartModel(\n", " (shared): BartScaledWordEmbedding(50264, 1024, padding_idx=1)\n", " (encoder): BartEncoder(\n", " (embed_tokens): BartScaledWordEmbedding(50264, 1024, padding_idx=1)\n", " (embed_positions): BartLearnedPositionalEmbedding(1026, 1024)\n", " (layers): ModuleList(\n", " (0-11): 12 x BartEncoderLayer(\n", " (self_attn): BartSdpaAttention(\n", " (k_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (v_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (q_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (out_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " )\n", " (self_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " (activation_fn): GELUActivation()\n", " (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n", " (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n", " (final_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " )\n", " )\n", " (layernorm_embedding): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " )\n", " (decoder): BartDecoder(\n", " (embed_tokens): BartScaledWordEmbedding(50264, 1024, padding_idx=1)\n", " (embed_positions): BartLearnedPositionalEmbedding(1026, 1024)\n", " (layers): ModuleList(\n", " (0-11): 12 x BartDecoderLayer(\n", " (self_attn): BartSdpaAttention(\n", " (k_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (v_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (q_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (out_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " )\n", " (activation_fn): GELUActivation()\n", " (self_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " (encoder_attn): BartSdpaAttention(\n", " (k_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (v_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (q_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " (out_proj): Linear(in_features=1024, out_features=1024, bias=True)\n", " )\n", " (encoder_attn_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " (fc1): Linear(in_features=1024, out_features=4096, bias=True)\n", " (fc2): Linear(in_features=4096, out_features=1024, bias=True)\n", " (final_layer_norm): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " )\n", " )\n", " (layernorm_embedding): LayerNorm((1024,), eps=1e-05, elementwise_affine=True)\n", " )\n", " )\n", " (lm_head): Linear(in_features=1024, out_features=50264, bias=False)\n", ")" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import torch\n", "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n", "model.to(device)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7861\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "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", "--------\n", " warnings.warn(\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Running on public URL: https://1fe44b84e4bdd88e83.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": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "\n", "def summarize(text):\n", " inputs = tokenizer([text], max_length=1024, return_tensors=\"pt\")\n", " summary_ids = model.generate(inputs[\"input_ids\"], num_beams=2, min_length=0, max_length=100)\n", " return tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]\n", "\n", "import gradio as gr\n", "\n", "iface = gr.Interface(\n", " fn=summarize,\n", " inputs=gr.Textbox(label=\"Text to summarize\"),\n", " outputs=[gr.Textbox(label=\"Summary\")],\n", " title='Summarize text'\n", ")\n", "\n", "iface.launch(share=True)" ] } ], "metadata": { "kernelspec": { "display_name": "SolutionsInPR", "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.12.3" } }, "nbformat": 4, "nbformat_minor": 2 }