Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system("pip install transformers")
|
| 3 |
+
os.system("pip install gradio")
|
| 4 |
+
os.system("pip install tensorflow")
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
from transformers import GPT2LMHeadModel, GPT2Tokenizer, set_seed
|
| 9 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2-large")
|
| 10 |
+
model = GPT2LMHeadModel.from_pretrained("gpt2-large", pad_token_id=tokenizer.eos_token_id)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def generate(prompt,textCount):
|
| 14 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')
|
| 15 |
+
# generate text until the output length (which includes the context length) reaches 50
|
| 16 |
+
output = model.generate(input_ids, max_length=textCount, num_beams=5, no_repeat_ngram_size=2, early_stopping=True)
|
| 17 |
+
out = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 18 |
+
return out
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
demo = gr.Interface(
|
| 23 |
+
fn=generate,
|
| 24 |
+
inputs=[gr.Textbox(lines=8, placeholder="Paragraph Here..."),"number"],
|
| 25 |
+
outputs="text",title="Text generation app with GPT2",
|
| 26 |
+
description="This is a text generation app, it can prove useful when you want to generate texts. All you need to do is copy and paste a short prompt . ",
|
| 27 |
+
examples=[
|
| 28 |
+
["During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in"
|
| 29 |
+
],["The bald eagle is"]
|
| 30 |
+
],
|
| 31 |
+
)
|
| 32 |
+
demo.launch()
|