Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
from transformers import TFGPT2LMHeadModel, GPT2Tokenizer
|
| 4 |
+
|
| 5 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 6 |
+
model = TFGPT2LMHeadModel.from_pretrained("gpt2", pad_token_id=tokenizer.eos_token_id)
|
| 7 |
+
|
| 8 |
+
#uncomment the lines below if you want to use GPT2-XL instead of GPT2 which would give faster outputs with greater accuracy.
|
| 9 |
+
#you will have to recomment the above 2 lines if you wanna use GPT2-XL.
|
| 10 |
+
#tokenizer = GPT2Tokenizer.from_pretrained("gpt2-xl")
|
| 11 |
+
#model = TFGPT2LMHeadModel.from_pretrained("gpt2-xl", pad_token_id=tokenizer.eos_token_id)
|
| 12 |
+
|
| 13 |
+
def generate_text(inp):
|
| 14 |
+
input_ids = tokenizer.encode(inp, return_tensors='tf')
|
| 15 |
+
beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
|
| 16 |
+
output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 17 |
+
return ".".join(output.split(".")[:-1]) + "."
|
| 18 |
+
|
| 19 |
+
output_text = gr.outputs.Textbox()
|
| 20 |
+
gr.Interface(generate_text,"textbox", output_text, title="GPT-2",
|
| 21 |
+
description="OpenAI's GPT-2 is an unsupervised language model that \
|
| 22 |
+
can generate coherent text. Go ahead and input a sentence and see what it completes \
|
| 23 |
+
it with! Takes around 30s to run.").launch()
|