musharafnasim commited on
Commit
6f59917
·
verified ·
1 Parent(s): 905aa1f

Colab notebook

Browse files

you can use this in your own virtual enviroment

Files changed (1) hide show
  1. Sentence_Generation.ipynb +152 -0
Sentence_Generation.ipynb ADDED
@@ -0,0 +1,152 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 18,
20
+ "metadata": {
21
+ "colab": {
22
+ "base_uri": "https://localhost:8080/"
23
+ },
24
+ "id": "yqiXEj_uL8kv",
25
+ "outputId": "b3591701-bb63-4496-f90d-50d958b32b32"
26
+ },
27
+ "outputs": [
28
+ {
29
+ "output_type": "stream",
30
+ "name": "stdout",
31
+ "text": [
32
+ " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n",
33
+ " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n",
34
+ " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n"
35
+ ]
36
+ }
37
+ ],
38
+ "source": [
39
+ "!pip install -q gradio\n",
40
+ "!pip install -q git+https://github.com/huggingface/transformers.git\n"
41
+ ]
42
+ },
43
+ {
44
+ "cell_type": "code",
45
+ "source": [
46
+ "import gradio as gr\n",
47
+ "import tensorflow as tf\n",
48
+ "from transformers import TFGPT2LMHeadModel,GPT2Tokenizer"
49
+ ],
50
+ "metadata": {
51
+ "id": "NWyCNUJIMp58"
52
+ },
53
+ "execution_count": 19,
54
+ "outputs": []
55
+ },
56
+ {
57
+ "cell_type": "code",
58
+ "source": [
59
+ "tokenizer = GPT2Tokenizer.from_pretrained (\"gpt2\")\n",
60
+ "model = TFGPT2LMHeadModel.from_pretrained (\"gpt2\" ,pad_token_id=tokenizer.eos_token_id)"
61
+ ],
62
+ "metadata": {
63
+ "colab": {
64
+ "base_uri": "https://localhost:8080/"
65
+ },
66
+ "id": "uGE4z27oMuZx",
67
+ "outputId": "a26407d4-2628-44d4-be16-f3826a043eb8"
68
+ },
69
+ "execution_count": 10,
70
+ "outputs": [
71
+ {
72
+ "output_type": "stream",
73
+ "name": "stderr",
74
+ "text": [
75
+ "All PyTorch model weights were used when initializing TFGPT2LMHeadModel.\n",
76
+ "\n",
77
+ "All the weights of TFGPT2LMHeadModel were initialized from the PyTorch model.\n",
78
+ "If your task is similar to the task the model of the checkpoint was trained on, you can already use TFGPT2LMHeadModel for predictions without further training.\n"
79
+ ]
80
+ }
81
+ ]
82
+ },
83
+ {
84
+ "cell_type": "code",
85
+ "source": [
86
+ "def generate_text(input_Prompt):\n",
87
+ " input_ids = tokenizer.encode(input_Prompt, return_tensors='tf')\n",
88
+ " beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=False)\n",
89
+ " output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)\n",
90
+ " return \".\".join(output.split(\".\")[:-1]) + \".\"\n"
91
+ ],
92
+ "metadata": {
93
+ "id": "hoKSOw9eMvQt"
94
+ },
95
+ "execution_count": 16,
96
+ "outputs": []
97
+ },
98
+ {
99
+ "cell_type": "code",
100
+ "source": [
101
+ "output_text = gr.Textbox()\n",
102
+ "\n",
103
+ "gr. Interface(generate_text,\"textbox\", output_text, title=\"GPT-2\",\n",
104
+ "\n",
105
+ "description=\"OpenAI's GPT-2 is an unsupervised language model that \\ can generate coherent text. Go ahead and input a sentence and see what it completes \\ it with! Takes around 20s to run.\").launch()"
106
+ ],
107
+ "metadata": {
108
+ "colab": {
109
+ "base_uri": "https://localhost:8080/",
110
+ "height": 648
111
+ },
112
+ "id": "cM-5NqQ-M1dn",
113
+ "outputId": "c52b7b5d-43b1-4bc7-aebe-761ddb8be371"
114
+ },
115
+ "execution_count": 17,
116
+ "outputs": [
117
+ {
118
+ "output_type": "stream",
119
+ "name": "stdout",
120
+ "text": [
121
+ "Setting queue=True in a Colab notebook requires sharing enabled. Setting `share=True` (you can turn this off by setting `share=False` in `launch()` explicitly).\n",
122
+ "\n",
123
+ "Colab notebook detected. To show errors in colab notebook, set debug=True in launch()\n",
124
+ "Running on public URL: https://ac6c205dbfaa7333aa.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
+ "output_type": "display_data",
131
+ "data": {
132
+ "text/plain": [
133
+ "<IPython.core.display.HTML object>"
134
+ ],
135
+ "text/html": [
136
+ "<div><iframe src=\"https://ac6c205dbfaa7333aa.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
137
+ ]
138
+ },
139
+ "metadata": {}
140
+ },
141
+ {
142
+ "output_type": "execute_result",
143
+ "data": {
144
+ "text/plain": []
145
+ },
146
+ "metadata": {},
147
+ "execution_count": 17
148
+ }
149
+ ]
150
+ }
151
+ ]
152
+ }