Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tensorflow as tf
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import GPT2Tokenizer, TFGPT2LMHeadModel
|
4 |
+
|
5 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
6 |
+
model = TFGPT2LMHeadModel.from_pretrained("gpt2",pad_token_id=tokenizer.eos_token_id)
|
7 |
+
|
8 |
+
def generate_text(inp):
|
9 |
+
input_ids = tokenizer.encode(inp, return_tensors='tf')
|
10 |
+
beam_output = model.generate(input_ids, max_length=100, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
|
11 |
+
output = tokenizer.decode(beam_output[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
12 |
+
return ".".join(output.split(".")[:-1]) + "."
|
13 |
+
|
14 |
+
input_text = gr.components.Textbox(lines=5, label="Input Text")
|
15 |
+
output_text = gr.components.Textbox(label="Output Text")
|
16 |
+
|
17 |
+
gr.Interface(generate_text, inputs=input_text, outputs=output_text, title="GPT-2",
|
18 |
+
description="OpenAI's GPT-2 is an unsupervised model that can generate coherent text.\
|
19 |
+
Go ahead and input a sentence and see what it completes it with! Takes around 20s to run").launch(share=True, inline = False)
|