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

Upload sentence_generation.py

Browse files
Files changed (1) hide show
  1. sentence_generation.py +30 -0
sentence_generation.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Sentence Generation.ipynb
3
+
4
+ Automatically generated by Colaboratory.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1CCrgBdR2KiN0N7L8RXBaLrEZ7QJjFP4k
8
+ """
9
+
10
+ !pip install -q gradio
11
+ !pip install -q git+https://github.com/huggingface/transformers.git
12
+
13
+ import gradio as gr
14
+ import tensorflow as tf
15
+ from transformers import TFGPT2LMHeadModel,GPT2Tokenizer
16
+
17
+ tokenizer = GPT2Tokenizer.from_pretrained ("gpt2")
18
+ model = TFGPT2LMHeadModel.from_pretrained ("gpt2" ,pad_token_id=tokenizer.eos_token_id)
19
+
20
+ def generate_text(input_Prompt):
21
+ input_ids = tokenizer.encode(input_Prompt, return_tensors='tf')
22
+ beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=False)
23
+ output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
24
+ return ".".join(output.split(".")[:-1]) + "."
25
+
26
+ output_text = gr.Textbox()
27
+
28
+ gr. Interface(generate_text,"textbox", output_text, title="GPT-2",
29
+
30
+ 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()